利用创建一个精灵来随机显示图片.
var GameData ={
W_NUM:8,
H_NUM:8,
}
var HelloWorldLayer = cc.Layer.extend({
sp:null,
ctor:function(){
this._super();
this._init()
},
_init:function(){
var arr = []
for(var i =0 ;i<GameData.W_NUM;i++){
arr [i] = []
for(var j =0 ;j<GameData.H_NUM;j++){
this.sp =new Cell()
this.sp.setPosition(200+(40*i),cc.winSize.height-20-(40*j))
this.addChild(this.sp)
this.sp.setArrIndex(i,j)
}
}
}
});
var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new HelloWorldLayer();
this.addChild(layer);
}
});
var Cell= cc.Sprite.extend({
type:0,
ctor:function(){
this._super();
this._init()
},
_init:function(){
this.type =parseInt(1+Math.random()*4);
this.initWithFile("res/pic"+this.type+".png")
},
setArrIndex:function(i,j){
var labei =new cc.LabelTTF(i+","+j,"",10)
labei.enableStroke(cc.color(0,0,0,255),1)
labei.setPosition(10,10)
this.addChild(labei)
},
})