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

Get SID from object-type, get object-type from SID

Get SID from object-type
var sid = objtype.sid

Get object-type from sid
var obktype = this.runtime.getObjectTypeBySid(sid);

It will check the object-types list to find the matched target. Keeping the index of the matched target might increase the performance.
var objtypes = this.runtime.types_by_index;
var t = objtypes[index];
if ((t != null) && (t.sid === sid))   // find the target
    return t;
var i, len=objtypes.length;
for (i=0; i<len; i++)
{
    t = objtypes[i];
    if (t.sid === sid)      // find the target
    {
        index = i;          // save the index
        return t;
    }
}

Reference

Get angle and distance between two points

Distance
var distance = cr.distanceTo(px0, py0, px1, py1);


Angle
var angle = cr.angleTo(px0, py0, px1, py1);      // in radian
angle = cr.to_clamped_degrees(angle);     // radian to degree

Reuse system action

cr.system_object.prototype.acts.SetLayerEffectParam.call(
    this.runtime.system, // this
    param0,
    ....
);

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 );

Array shallow copy

cr.shallowAssignArray(to_arr, from_arr);

Function declaration string for eval

....
param = param.replace(re, "\\n"); // replace "\n" to "\\n"
var code_string = "function(scenario)\{\
    var MEM = scenario.Mem;\
    var Call = scenario._getvalue_from_c2fn;\
    return "+param+"\
    }";
_thisArg = this;
var fn = eval("("+code_string+")");
param = fn(this);
....

Reference


The variable space will be cleaned in this eval , in CocoonJS exporter.

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.

Reuse ACE

Reuse action/condition/expression from other plugin.

Action cr.plugins_.WebStorage.prototype.acts.StoreLocal.call(webstorage_obj, key, value);


Condition
cr.plugins_.WebStorage.prototype.cnds.LocalStorageExists.call(webstorage_obj, key);



Expression 

var fake_ret = {
    value:0,
    set_any: function(value){this.value=value;},
    set_int: function(value){this.value=value;},
    set_float: function(value){this.value=value;}, 
    set_string: function(value) {this.value=value;},
};
cr.plugins_.WebStorage.prototype.exps.LocalValue.call(webstorage_obj, fake_ret, key);
return fake_ret.value



Get instance 
var plugins = this.runtime.types; 
var name, inst; 
for (name in plugins)
{
    inst = plugins[name].instances[0]; 
    if (inst instanceof cr.plugins_.WebStorage.prototype.Instance)
    {
        // get target instance 
    } 
 
 
 
Reference - webstorage extension plugin