You can reach the timeline plugin example here.
Subsections
01 A Word on mixing.enchant.js
02 Mixing Example
enchant.js provides support for class inheritance, which you can read more about here.
On this note, the mixing.enchant.js
plugin provides support for creating classes with multiple inheritance. The mixing method enchant.Class.mixClasses()
takes in the parameters: firstClass
, secondClass
, useOnlyOwnPropertiesForSecondclass
, and initializeMethod
.
The firstClass
and secondClass
parameters are the two Classes you intend to mix.
The useOnlyOwnProperties
is an optional boolean parameter that, if true, does not include properties given to it by the second class’s superclass. Not setting it to true can lead to strange behavior of a resulting class such as twicefold execution of methods provided by a shared superclass. By default, the flag is set to false.
initializeMethod
is an optional method parameter that is used as the constructor for the resulting class.
In my simple example of the mixing plugin, I created two separate classes named A and B, with their code shown below.
Class A
A = Class.create(Sprite, {
initialize: function(){
Sprite.call(this, 16, 16);
this.image = game.assets['icon0.png'];
this.x = stgWidth/2 - this.width;
this.y = stgHeight/2 - this.height;
this.moveVert = 2.0;
this.frame = 22;
},
onenterframe: function(){
this.y += this.moveVert;
if(this.y >= stgHeight - 20){
this.moveVert *= -1.0;
}
else if(this.y <= 20){
this.moveVert *= -1.0;
}
}
});
B = Class.create(Sprite, {
initialize: function(){
Sprite.call(this, 16, 16);
this.image = game.assets['icon0.png'];
this.x = stgWidth/2 - this.width;
this.y = stgHeight/2 - this.height;
this.moveHorz = 2.0;
this.frame = 23;
},
onenterframe: function(){
this.x += this.moveHorz;
if(this.x >= stgWidth - 20){
this.moveHorz *= -1.0;
}
else if(this.x <= 20){
this.moveHorz *= -1.0;
}
}
});
var C = enchant.Class.mixClasses(A, B, true);
useOnlyOwnProperties
to true, and did not pass anything for initializeMethod
.