resource.js
var res = {
pt1_url:
"res/pt_1.jpg",
};
var g_resources = [];
for (
var i
in res) {
g_resources.push(res[i]);
}
main.js
cc
.game.onStart = function(){
cc
.view.adjustViewPort(true)
cc
.view.setDesignResolutionSize(
800,
800, cc
.ResolutionPolicy.SHOW_ALL)
cc
.view.resizeWithBrowserSize(true)
//load resources
cc
.LoaderScene.preload(g_resources, function () {
cc
.director.runScene(new HelloWorldScene())
}, this)
}
cc
.game.run()
app.js
var H_NUM =
3;
var W_NUM =
4;
var HelloWorldLayer = cc.Layer.extend({
baseSpr:
null,
basePic_url:
null,
hei:
null,
wid:
null,
arrCell:
null,
arrClickedCell:
null,
ctor:
function () {
this._super();
this._init();
return true;
},
_init:
function()
{
this.arrCell = [];
this.arrClickedCell = [];
this.basePic_url = res.pt1_url;
this.baseSpr =
new cc.Sprite(
this.basePic_url);
this.wid =
this.baseSpr.width/W_NUM.toFixed(
5) -
0.00001;
this.hei =
this.baseSpr.height/H_NUM.toFixed(
5) -
0.00001;
this._spawnCell();
cc.log(
this.wid+
" "+
this.hei);
this.scheduleOnce(
this._startGame,
3);
},
_spawnCell:
function()
{
var _winSize = cc.director.getWinSize();
var cell;
for(
var i =
0;i<H_NUM;i++)
{
this.arrCell[i] = [];
for(
var j =
0;j<W_NUM;j++)
{
cell =
new CellSpr(
this.basePic_url,cc.rect(j*
this.wid, i*
this.hei,
this.wid,
this.hei),
this);
cell._setDate((_winSize.width -
this.baseSpr.width +
this.wid)/
2 + j *
this.wid,
(_winSize.height +
this.baseSpr.height -
this.hei)/
2 - i*
this.hei, j,i);
this.addChild(cell);
this.arrCell[i][j] = cell;
}
}
},
_startGame:
function()
{
var arrTurn = [];
for(
var i =
0;i<W_NUM * H_NUM;i++)
{
arrTurn.push(i);
}
var policy =
function()
{
var randNum =
Math.random();
return (randNum>
0.5) ?
1: -
1;
}
arrTurn.sort(policy);
for(
var i =
0;i<H_NUM;i++)
{
for(
var j=
0;j<W_NUM;j++)
{
this. _redisplayCell(arrTurn[i * W_NUM + j],i,j);
}
}
},
_redisplayCell:
function(value,i,j)
{
cc.log(
"++++++"+value);
this.arrCell[i][j].mValue = value;
var v = cc.p(
parseInt(value % W_NUM),
parseInt(value / W_NUM));
this.arrCell[i][j].initWithFile(
this.basePic_url,cc.rect(v.x*
this.wid, v.y*
this.hei,
this.wid,
this.hei));
},
_callTouchHandler:
function(obj)
{
if(
this.arrClickedCell.length ==
1 &&
this.arrClickedCell[
0].value == obj.value)
{
this.arrCell[obj.i][obj.j]._resetAphle();
this.arrClickedCell = [];
return;
}
this.arrClickedCell.push(obj);
if(
this.arrClickedCell.length ==
2)
{
this.arrCell[
this.arrClickedCell[
1].i][
this.arrClickedCell[
1].j]._resetAphle();
this.arrCell[
this.arrClickedCell[
0].i][
this.arrClickedCell[
0].j]._resetAphle();
this._redisplayCell(
this.arrClickedCell[
0].value,
this.arrClickedCell[
1].i,
this.arrClickedCell[
1].j);
this._redisplayCell(
this.arrClickedCell[
1].value,
this.arrClickedCell[
0].i,
this.arrClickedCell[
0].j);
this.arrClickedCell = [];
this._checkResult();
}
},
_checkResult:
function () {
for (
var i =
0; i < H_NUM; i++) {
for (
var j =
0; j < W_NUM; j++)
if (
this.arrCell[i][j].mValue != i * W_NUM + j)
return;
}
this.unschedule(
this._startGame);
cc.log(
"game complete");
},
});
var HelloWorldScene = cc.Scene.extend({
onEnter:
function () {
this._super();
var layer =
new HelloWorldLayer();
this.addChild(layer);
}
});
CellSpr.js
var CellSpr = cc.Sprite.extend({
mValue:
null,
ix:
null,
iy:
null,
ctor:
function(url,rect,par)
{
this._super(url,rect);
},
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))
{
return true;
}
return false;
},
onTouchMove:
function(touch,event){},
onTouchEnded:
function(touch,event)
{
var target = event.getCurrentTarget();
that._changeAphle();
}
})
cc.eventManager.addListener(listener,
this);
},
_changeAphle:
function()
{
this.setOpacity(
150);
this.parent._callTouchHandler({
value:
this.mValue,
i:
this.iy,
j:
this.ix});
},
_resetAphle:
function()
{
this.setOpacity(
255);
},
_setDate:
function(x,y,ix,iy)
{
this.x = x;
this.y = y;
this.ix = ix;
this.iy = iy;
}
})