recrouse.js
var res = {
pt1_url:"res/pt_1.jpg",
};
var g_resources = [];
for (var i in res) {
g_resources.push(res[i]);
}
app.js
var H_NUM = 3;
var W_NUM = 3;
var CLICK_CELL_EVENT = "click_cell_event";
var HelloWorldLayer = cc.Layer.extend({
arrCellSpr:[],
basePicUrl:null,
baseSpr:null,
hei:null,
wid:null,
winLab:null,
isStart:false,
ctor:function () {
this._super();
this._init();
return true;
},
_init:function()
{
var _winsize = cc.winSize;
this.winLab = new cc.LabelTTF("You Win","Arial", 88);
this.winLab.attr({
anchorX:0.5,
anchorY:0.5,
x:_winsize.width/2,
y:_winsize.height/2,
})
this.addChild(this.winLab,3);
this.winLab.setVisible(false);
this.basePicUrl = res. pt1_url;
this.baseSpr = new cc.Sprite(this.basePicUrl);
this.wid = this.baseSpr.width/W_NUM.toFixed(5) - 0.00001;
this.hei = this.baseSpr.height/H_NUM.toFixed(5) - 0.00001;
var basePosition = cc.p((_winsize.width - this.baseSpr.width + this.wid)/2, (_winsize.height + this.baseSpr.height - this.hei)/2);
var cell;
for(var i = 0;i<H_NUM;i++)
{
this.arrCellSpr[i] = [];
for(var j = 0;j<W_NUM;j++)
{
var pos = cc.p(basePosition.x + j*this.wid,basePosition.y - i*this.hei)
cell = new CellSpr(this.basePicUrl, cc.rect(j*this.wid, i*this.hei, this.wid, this.hei),pos );
cell.setPosition(pos);
this.addChild(cell);
this.arrCellSpr[i][j] = cell;
}
}
this.arrCellSpr[i - 1][j - 1].opacity = 0;
this._addCustomEvent();
this.scheduleOnce(this._startGame,0);
},
_addCustomEvent:function()
{
var that = this;
cc.eventManager.addCustomListener(CLICK_CELL_EVENT,function(event){
var cell = event.getUserData();
var arr = that._getIndexInArr(cell);
that._checkNeighbor(arr[0],arr[1]);
})
},
_startGame:function()
{
var arrTurn = [];
for(var i = 0;i<H_NUM*W_NUM;i++)
{
arrTurn.push(i);
}
arrTurn.sort(function(){return (Math.random()>0.5) ? -1 : 1;});
cc.log(arrTurn);
for(var i = 0;i<H_NUM;i++)
{
for (var j = 0; j < W_NUM; j++)
{
var value = arrTurn[i*W_NUM + j];
var p1 = [parseInt(value/W_NUM),parseInt(value%W_NUM)];
var p2 = [i,j];
this._redisplay(p1,p2);
}
}
this.isStart = true;
},
_redisplay:function(arr1,arr2)
{
var tempPosition = this.arrCellSpr[arr1[0]][arr1[1]].getPosition();
this.arrCellSpr[arr1[0]][arr1[1]].setPosition(this.arrCellSpr[arr2[0]][arr2[1]].getPosition());
this.arrCellSpr[arr2[0]][arr2[1]].setPosition (tempPosition);
var shred = this.arrCellSpr[arr1[0]][arr1[1]];
this.arrCellSpr[arr1[0]][arr1[1]] = this.arrCellSpr[arr2[0]][arr2[1]];
this.arrCellSpr[arr2[0]][arr2[1]] = shred;
if(this.isStart)
{
this._checkAllFinish();
}
},
_checkNeighbor:function(i,j)
{
if(i - 1 >= 0 && this.arrCellSpr[i-1][j].opacity == 0)
{
this._redisplay([i,j],[i-1,j]);
}
else if(i+1 < H_NUM && this.arrCellSpr[i+1][j].opacity == 0)
{
this._redisplay([i,j],[i+1,j]);
}
else if(j-1 >= 0 && this.arrCellSpr[i][j-1].opacity == 0)
{
this._redisplay([i,j],[i,j-1]);
}
else if(j+1 < W_NUM && this.arrCellSpr[i][j+1].opacity == 0)
{
this._redisplay([i,j],[i,j+1]);
}
},
_getIndexInArr:function(cell)
{
for(var i = 0;i<H_NUM;i++)
{
for (var j = 0; j < W_NUM; j++)
{
if(this.arrCellSpr[i][j] == cell) return [i,j];
}
}
},
_checkAllFinish:function()
{
for(var i = 0;i<H_NUM;i++)
{
for (var j = 0; j < W_NUM; j++)
{
var p1 = this.arrCellSpr[i][j].getPosition();
var p2 = this.arrCellSpr[i][j] ._tagetPositon;
if(p1.x != p2.x || p1.y != p2.y)
{
this.winLab.setVisible(false);
return;
}
}
}
this.winLab.setVisible(true);
}
});
var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new HelloWorldLayer();
this.addChild(layer);
}
});
CellSpr.js
var CellSpr = cc.Sprite.extend({
_tagetPositon:null,
ctor:function(url,rect,p)
{
this._super(url,rect);
this._tagetPositon = p;
},
onEnter:function()
{
this._super();
var that = this;
var listener = cc.EventListener.create({
event:cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches:true,
onTouchBegan:function(touch,event)
{
if(that.getParent._isGameOver == true) return;
var target = event.getCurrentTarget();
var locationInNode = target.convertToNodeSpace(touch.getLocation());
var s = target.getContentSize();
var rect = cc.rect(0,0, s.width, s.height);
if(cc.rectContainsPoint(rect,locationInNode))
{
cc.eventManager.dispatchCustomEvent(CLICK_CELL_EVENT, that);
return true;
}
return false;
},
onTouchMove:function(touch,event){},
onTouchEnded:function(touch,event)
{
var target = event.getCurrentTarget();
}
})
cc.eventManager.addListener(listener, this);
},
})
![这里写图片描述](https://img-blog.csdn.net/20170811125511387?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGluZ1hpX19Z/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![这里写图片描述](https://img-blog.csdn.net/20170811125524061?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGluZ1hpX19Z/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)