Changing sprite images on the fly
Let's now find out how to change sprite images.
Showing the tile picture
Once a tile is picked, it has to show its picture. Pictures are just a graphical representation of a tile value, which you initially store in an array called gameArray
declared at the very beginning of a gamescript.js
file:
var gameArray = [0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7];
vargameScene = cc.Scene.extend({
onEnter:function () {
// same as before
}
});
Then, once you create a new tile, you can assign it a custom attribute called pictureValue
with the value of the i-th element of gameArray
:
var game = cc.Layer.extend({
init:function () {
this._super();
var gradient = cc.LayerGradient.create(cc.color(0,0,0,255), cc.color(0x46,0x82,0xB4,255));
this.addChild(gradient);
for(i=0;i<16;i++){
var tile = new MemoryTile();
tile.pictureValue = gameArray[i];
this.addChild(tile,0);
tile.setPosition(49+i%4*74,400-Math.floor(i/4)*74);
}
}
...