Showing posts with label Instance. Show all posts
Showing posts with label Instance. Show all posts

Official function to create an instance

Official function to create an instance, see system.js, line 1284.

SysActs.prototype.CreateObject = function (obj, layer, x, y)
{
    if (!layer || !obj)
        return;

    var inst = this.runtime.createInstance(obj, layer, x, y);
 
    if (!inst)
        return;
 
    this.runtime.isInOnDestroy++;
 
    var i, len, s;
    this.runtime.trigger(Object.getPrototypeOf(obj.plugin).cnds.OnCreated, inst);
 
    if (inst.is_contained)
    {
        for (i = 0, len = inst.siblings.length; i < len; i++)
        {
            s = inst.siblings[i];
            this.runtime.trigger(Object.getPrototypeOf(s.type.plugin).cnds.OnCreated, s);
        }
    }
 
    this.runtime.isInOnDestroy--;

    // Pick just this instance
    var sol = obj.getCurrentSol();
    sol.select_all = false;
    sol.instances.length = 1;
    sol.instances[0] = inst;
 
    // Siblings aren't in instance lists yet, pick them manually
    if (inst.is_contained)
    {
        for (i = 0, len = inst.siblings.length; i < len; i++)
        {
            s = inst.siblings[i];
            sol = s.type.getCurrentSol();
            sol.select_all = false;
            sol.instances.length = 1;
            sol.instances[0] = s;
        }
    }
};

Reorder z order of layer

Step1. Get layer
var layer = inst.layer;
or
var layer = (typeof layerparam == "number")?
            this.runtime.getLayerByNumber(layerparam):
            this.runtime.getLayerByName(layerparam);


Step2. Sort the list layer.instances
layer.instances(SORTFN);
for example
function sortInstanceByZIndex(a, b)
{

    return a.zindex - b.zindex;
}

Step3. reassign zindex of instances in this layer.
layer.zindices_stale = true;


Reference

Get timeline instance

instanceProto._timeline_get = function ()
{
    if (this.timeline != null)
        return this.timeline;

    assert2(cr.plugins_.Rex_TimeLine, "Scenario: Can not find timeline oject.");
    var plugins = this.runtime.types;
    var name, inst;
    for (name in plugins)
    {
        inst = plugins[name].instances[0];
        if (inst instanceof cr.plugins_.Rex_TimeLine.prototype.Instance)
        {
            this.timeline = inst;
            return this.timeline;
        }
    }
    assert2(this.timeline, "Scenario: Can not find timeline oject.");
    return null; 

};

Reference

Create instance

instanceProto.CreateInst = function (objtype,x,y,_layer)
{

    var layer = (typeof _layer == "number")?
                this.runtime.getLayerByNumber(_layer):
                (typeof _layer == "string")?

                this.runtime.getLayerByName(_layer):
                _layer;
 

    // call system action: Create instance
    cr.system_object.prototype.acts.CreateObject.call(
        this.runtime.system,
        objtype,
        layer,
        x,
        y
    );

   
    return objtype.getFirstPicked();  // return the created instance
};


Reference

Destroy instance

this.runtime.DestroyInstance(inst);


Callback
This method will trigger a callback, add the callback by
this.myDestroyCallback = function (inst)
{
....
}
this.runtime.addDestroyCallback( this.myDestroyCallback ) 


Remove the callback by
this.runtime.removeDestroyCallback( this.myDestroyCallback );

Get UID from instance, get instance from UID

Get UID from instance
var uid = inst.uid;

Get instance from UID
var uid2inst = function (uid)
{
    if (uid == null)
        return null
    return this.runtime.getObjectByUID(uid);  
}

It will return an instance or null.