summaryrefslogtreecommitdiffstats
path: root/examples/declarative/dynamic/dynamic.js
blob: 66ec292456b705952a7a50ef667cd5c05c65f778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var sprite = null;
var component;
var started = false;
function make(p) {
    return evalQml('Rect { color: "lightsteelblue"; width: 100;'
            + 'height: 100; id: newRect}','DynPart.qml');
}

function death() {
    if(!(sprite==null)){
        sprite.destroy();
        sprite = null;
    }
}

function spawn() {//Like create, but assumes instant readyness
    if(sprite!=null)//Already made
        return null;
    component = createComponent("dynamic.qml");
    sprite = component.createObject();
    if(sprite == null){
        print("err");
    }else{
        sprite.parent = targetItem;
        return sprite;
    }
    return null;
}

function finishCreation(){
    if(component.isReady()){
        sprite = component.createObject();
        sprite.parent = targetItem;
    }else if(component.isError()){
        sprite = null;
    }
}

function create(){
    if(started!=false){
        finishCreation();//Remakes if destroyed
        return sprite;
    }
    started = true;
    component = createComponent("dynamic.qml");
    finishCreation();
    if(sprite != null){
        return sprite;
    }
    component.statusChanged.connect(finishCreation);
    return null;
}