这次我们来完成整个2048的数字相加逻辑其实2048玩起来简单,做起来也简单,复杂就复杂在这整个游戏的逻辑。
1.分析向左滑动
第一轮相加步骤:1. 单1+单2 单1=2 单2赋值为0 (单1为空,可以加任何数字)
2. 单1+单3 单1=4 单3赋值为0 (单1不为空,只能加和他相同的数字)
3. 单1+单4 单1=4 单4不变 (单1不为空,只能加和他相同的数字)
第二轮相加步骤: 1. 单2+单3 单2=0 单3不变 (单2为0可以加任何数字,但是单3也是0,,其实加不加都是一个效果) 2. 单2+单4 单2=2 单4赋值0 (单2为0可以加任何数字)
接着就是单3加单4了,这两个都为空,什么都不做,最终的效果也就是,第二轮相加后的效果了
这个其实就是用的冒泡算法,好了,理解了之后,那么我们来写代码。
首先,我们在初始化矩阵中给所有的数字方块初始化为2,暂时先注释掉随机创建的方法
01.
void
HelloWorld::initMatrix(CCSize size)
02.
{
03.
//两个方块之间的空隙
04.
int
space=
10
;
05.
int
SquareSize=(size.width-space*
5
)/
4
;
06.
for
(
int
x =
0
; x <
4
; x++)
07.
{
08.
for
(
int
y =
0
; y <
4
; y++)
09.
{
10.
float
pointx=space+x*SquareSize+x*space;
11.
float
pointy=
40
+y*SquareSize+y*space;
12.
SquareSprite* pSquareSprite=SquareSprite::createSquareSprite(
2
,pointx,pointy,SquareSize,SquareSize);
13.
this
->addChild(pSquareSprite,
1
);
14.
m_squarearray[x][y]=pSquareSprite;
15.
}
16.
}
17.
/*
18.
//刚开始游戏,创建两个方块
19.
autoSquare();
20.
autoSquare();
21.
*/
22.
}
OK,向左滑动,那么此时我们需要在doLeft方法中写代码咯
01.
bool HelloWorld::doLeft()
02.
{
03.
for
(
int
y =
0
; y <
4
; y++)
04.
{
05.
for
(
int
x =
0
; x <
4
; x++)
06.
{
07.
for
(
int
afterx = x+
1
; afterx <
4
; afterx++)
08.
{
09.
10.
}
11.
}
12.
}
13.
return
true
;
14.
}
大概的框架就是这样子吧??最外层的y我们可以忽视,这是左加不是上下加
里面两个循环表示什么呢??x=0时 afterx=1 即单1和单2 依次类推
OK,我们在循环中写代码,
01.
bool HelloWorld::doLeft()
02.
{
03.
for
(
int
y =
0
; y <
4
; y++)
04.
{
05.
for
(
int
x =
0
; x <
4
; x++)
06.
{
07.
for
(
int
afterx = x+
1
; afterx <
4
; afterx++)
08.
{
09.
if
(m_squarearray[x][y]->getNumber()==
0
)
10.
{
11.
m_squarearray[x][y]->setNumber(m_squarearray[afterx][y]->getNumber());
12.
m_squarearray[afterx][y]->setNumber(
0
);
13.
}
14.
else
if
(m_squarearray[afterx][y]->getNumber()==m_squarearray[x][y]->getNumber())
15.
{
16.
m_squarearray[x][y]->setNumber(m_squarearray[x][y]->getNumber()*
2
);
17.
m_squarearray[afterx][y]->setNumber(
0
);
18.
}
19.
}
20.
}
21.
}
22.
return
true
;
23.
}
这里有两种情况,
1.单1等于0时,不管单2是什么,都要和单2调换位置
2.单1不等于0时,那么只有单1和单2相等,才能相加
大家可以运行看看效果。
2.分析向右滑动
这里和向左滑动差不多,其实就是一个是顺着来一个是倒着来,我就不画图了,直接上代码01.
bool HelloWorld::doReight()
02.
{
03.
for
(
int
y =
0
; y <
4
; y++)
04.
{
05.
for
(
int
x =
3
; x >=
0
; x--)
06.
{
07.
for
(
int
afterx = x-
1
; afterx >=
0
; afterx--)
08.
{
09.
if
(m_squarearray[x][y]->getNumber()==
0
)
10.
{
11.
m_squarearray[x][y]->setNumber(m_squarearray[afterx][y]->getNumber());
12.
m_squarearray[afterx][y]->setNumber(
0
);
13.
}
14.
else
if
(m_squarearray[afterx][y]->getNumber()==m_squarearray[x][y]->getNumber())
15.
{
16.
m_squarearray[x][y]->setNumber(m_squarearray[x][y]->getNumber()*
2
);
17.
m_squarearray[afterx][y]->setNumber(
0
);
18.
}
19.
}
20.
}
21.
}
22.
return
true
;
23.
}
其他地方都没改,就把两个循环改了一下
3.分析向上滑动
01.
bool HelloWorld::doTop()
02.
{
03.
for
(
int
x =
0
; x <
4
; x++)
04.
{
05.
for
(
int
y =
3
; y >=
0
; y--)
06.
{
07.
for
(
int
aftery = y-
1
; aftery >=
0
; aftery--)
08.
{
09.
if
(m_squarearray[x][y]->getNumber()==
0
)
10.
{
11.
m_squarearray[x][y]->setNumber(m_squarearray[x][aftery]->getNumber());
12.
m_squarearray[x][aftery]->setNumber(
0
);
13.
}
14.
else
if
(m_squarearray[x][aftery]->getNumber()==m_squarearray[x][y]->getNumber())
15.
{
16.
m_squarearray[x][y]->setNumber(m_squarearray[x][y]->getNumber()*
2
);
17.
m_squarearray[x][aftery]->setNumber(
0
);
18.
}
19.
}
20.
}
21.
}
22.
return
true
;
23.
}
4.分析向下滑动
01.
bool HelloWorld::doDown()
02.
{
03.
for
(
int
x =
0
; x <
4
; x++)
04.
{
05.
for
(
int
y =
0
; y <
4
; y++)
06.
{
07.
for
(
int
aftery = y+
1
; aftery <
4
; aftery++)
08.
{
09.
if
(m_squarearray[x][y]->getNumber()==
0
)
10.
{
11.
m_squarearray[x][y]->setNumber(m_squarearray[x][aftery]->getNumber());
12.
m_squarearray[x][aftery]->setNumber(
0
);
13.
}
14.
else
if
(m_squarearray[x][aftery]->getNumber()==m_squarearray[x][y]->getNumber())
15.
{
16.
m_squarearray[x][y]->setNumber(m_squarearray[x][y]->getNumber()*
2
);
17.
m_squarearray[x][aftery]->setNumber(
0
);
18.
}
19.
}
20.
}
21.
}
22.
return
true
;
23.
}
现在基本上成了,那么我再修改其他几个函数,
首先把初始化矩阵的方法还原
01.
void
HelloWorld::initMatrix(CCSize size)
02.
{
03.
//两个方块之间的空隙
04.
int
space=
10
;
05.
int
SquareSize=(size.width-space*
5
)/
4
;
06.
for
(
int
x =
0
; x <
4
; x++)
07.
{
08.
for
(
int
y =
0
; y <
4
; y++)
09.
{
10.
float
pointx=space+x*SquareSize+x*space;
11.
float
pointy=
40
+y*SquareSize+y*space;
12.
SquareSprite* pSquareSprite=SquareSprite::createSquareSprite(
0
,pointx,pointy,SquareSize,SquareSize);
13.
this
->addChild(pSquareSprite,
1
);
14.
m_squarearray[x][y]=pSquareSprite;
15.
}
16.
}
17.
//刚开始游戏,创建两个方块
18.
autoSquare();
19.
autoSquare();
20.
}
然后,不管用户向那个方向滑动,只要是滑动了,就都得产生一个数字方块
01.
void
HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
02.
{
03.
bool isTouch=
false
;
04.
CCPoint point= pTouch->getLocation();
05.
m_RangeX=m_StartX-point.x;
06.
m_RangeY=m_StartY-point.y;
07.
08.
if
(abs(m_RangeX)>abs(m_RangeY))
09.
{
10.
//X大于Y 左右移动
11.
if
(m_RangeX>
5
)
12.
{
13.
//正数 向左
14.
isTouch=doLeft();
15.
}
16.
else
if
(m_RangeX<-
5
)
17.
{
18.
//负数 向右
19.
isTouch=doReight();
20.
}
21.
}
22.
else
23.
{
24.
//否则 上下移动
25.
if
(m_RangeY>
5
)
26.
{
27.
//正数 向下
28.
isTouch=doDown();
29.
}
30.
else
if
(m_RangeY<-
5
)
31.
{
32.
//负数 向上
33.
isTouch=doTop();
34.
}
35.
}
36.
if
(isTouch)
37.
{
38.
autoSquare();
39.
}
40.
}
测试一下,貌似我有发现了一个BUG
结合我们的代码分析一下也确实是这种情况,那么怎么破呢??
应该在两个同等数字相加后,就Brack,不能再继续加下去了
1.
else
if
(m_squarearray[x][aftery]->getNumber()==m_squarearray[x][y]->getNumber())
2.
{
3.
m_squarearray[x][y]->setNumber(m_squarearray[x][y]->getNumber()*
2
);
4.
m_squarearray[x][aftery]->setNumber(
0
);
5.
break
;
6.
}
刚测试,又发现一个BUG
本来是 4 2 4 0
向右滑动应该是 0 4 2 4
结果是: 0 0 2 8
大家结合doReight,在看看,好像也是这么回事
那么又应该怎么修复呢??
这里还不能再第一个IF加break
不然 2 2 2 0 就会变为 0 2 2 2了 而不是 0 0 2 4
这是一个很纠结的问题
我们应该要循环遍历一下 两个数字的中间是不是还有数字隔着,有就不加,没有就加
这里贴上所有逻辑代码
001.
bool HelloWorld::doTop()
002.
{
003.
for
(
int
x =
0
; x <
4
; x++)
004.
{
005.
for
(
int
y =
3
; y >=
0
; y--)
006.
{
007.
for
(
int
aftery = y-
1
; aftery >=
0
; aftery--)
008.
{
009.
if
(m_squarearray[x][y]->getNumber()==
0
)
010.
{
011.
m_squarearray[x][y]->setNumber(m_squarearray[x][aftery]->getNumber());
012.
m_squarearray[x][aftery]->setNumber(
0
);
013.
}
014.
else
if
(m_squarearray[x][aftery]->getNumber()==m_squarearray[x][y]->getNumber())
015.
{
016.
bool isAdd=
false
;
017.
for
(
int
temp = aftery+
1
; temp < y; temp++)
018.
{
019.
if
(m_squarearray[x][temp]->getNumber()!=
0
)
020.
{
021.
isAdd=
true
;
022.
break
;
023.
}
024.
}
025.
if
(isAdd)
026.
{
027.
continue
;
028.
}
029.
m_squarearray[x][y]->setNumber(m_squarearray[x][y]->getNumber()*
2
);
030.
m_squarearray[x][aftery]->setNumber(
0
);
031.
break
;
032.
}
033.
}
034.
}
035.
}
036.
return
true
;
037.
}
038.
bool HelloWorld::doDown()
039.
{
040.
for
(
int
x =
0
; x <
4
; x++)
041.
{
042.
for
(
int
y =
0
; y <
4
; y++)
043.
{
044.
for
(
int
aftery = y+
1
; aftery <
4
; aftery++)
045.
{
046.
if
(m_squarearray[x][y]->getNumber()==
0
)
047.
{
048.
m_squarearray[x][y]->setNumber(m_squarearray[x][aftery]->getNumber());
049.
m_squarearray[x][aftery]->setNumber(
0
);
050.
}
051.
else
if
(m_squarearray[x][aftery]->getNumber()==m_squarearray[x][y]->getNumber())
052.
{
053.
bool isAdd=
false
;
054.
for
(
int
temp = y+
1
; temp < aftery; temp++)
055.
{
056.
if
(m_squarearray[x][temp]->getNumber()!=
0
)
057.
{
058.
isAdd=
true
;
059.
break
;
060.
}
061.
}
062.
if
(isAdd)
063.
{
064.
continue
;
065.
}
066.
m_squarearray[x][y]->setNumber(m_squarearray[x][y]->getNumber()*
2
);
067.
m_squarearray[x][aftery]->setNumber(
0
);
068.
break
;
069.
}
070.
}
071.
}
072.
}
073.
return
true
;
074.
}
075.
bool HelloWorld::doReight()
076.
{
077.
for
(
int
y =
0
; y <
4
; y++)
078.
{
079.
for
(
int
x =
3
; x >=
0
; x--)
080.
{
081.
for
(
int
afterx = x-
1
; afterx >=
0
; afterx--)
082.
{
083.
if
(m_squarearray[x][y]->getNumber()==
0
)
084.
{
085.
m_squarearray[x][y]->setNumber(m_squarearray[afterx][y]->getNumber());
086.
m_squarearray[afterx][y]->setNumber(
0
);
087.
}
088.
else
if
(m_squarearray[afterx][y]->getNumber()==m_squarearray[x][y]->getNumber())
089.
{
090.
bool isAdd=
false
;
091.
for
(
int
temp = afterx+
1
; temp < x; temp++)
092.
{
093.
if
(m_squarearray[temp][y]->getNumber()!=
0
)
094.
{
095.
isAdd=
true
;
096.
break
;
097.
}
098.
}
099.
if
(isAdd)
100.
{
101.
continue
;
102.
}
103.
m_squarearray[x][y]->setNumber(m_squarearray[x][y]->getNumber()*
2
);
104.
m_squarearray[afterx][y]->setNumber(
0
);
105.
break
;
106.
}
107.
}
108.
}
109.
}
110.
return
true
;
111.
}
112.
bool HelloWorld::doLeft()
113.
{
114.
for
(
int
y =
0
; y <
4
; y++)
115.
{
116.
for
(
int
x =
0
; x <
4
; x++)
117.
{
118.
for
(
int
afterx = x+
1
; afterx <
4
; afterx++)
119.
{
120.
if
(m_squarearray[x][y]->getNumber()==
0
)
121.
{
122.
m_squarearray[x][y]->setNumber(m_squarearray[afterx][y]->getNumber());
123.
m_squarearray[afterx][y]->setNumber(
0
);
124.
}
125.
else
if
(m_squarearray[afterx][y]->getNumber()==m_squarearray[x][y]->getNumber())
126.
{
127.
bool isAdd=
false
;
128.
for
(
int
temp = x+
1
; temp < afterx; temp++)
129.
{
130.
if
(m_squarearray[temp][y]->getNumber()!=
0
)
131.
{
132.
isAdd=
true
;
133.
break
;
134.
}
135.
}
136.
if
(isAdd)
137.
{
138.
continue
;
139.
}
140.
m_squarearray[x][y]->setNumber(m_squarearray[x][y]->getNumber()*
2
);
141.
m_squarearray[afterx][y]->setNumber(
0
);
142.
break
;
143.
}
144.
}
145.
}
146.
}
147.
return
true
;
148.
}
5.为游戏增加动画效果
最后在来点,动画效果这里,动画就是,在创建一个数字方块的时候他是慢慢放大的,
我们的数字方块是什么??CCLayerColor
我也不知道为什么,我们之间这样
1.
m_squarearray[x][y]->runAction(CCSequence::create(CCScaleTo::create(
0
,
0
,
0
),CCScaleTo::create(
0
.3f,
1
,
1
),NULL));
出现的效果并不理想。
那么我们就在SquareSprite中提供一个接口用来获得 m_layercolor
然后在用m_layercolor来执行动画
1.
CCLayerColor* SquareSprite::getCClayerColor()
2.
{
3.
return
this
->m_layercolor;
4.
}
01.
void
HelloWorld::autoSquare()
02.
{
03.
int
x=CCRANDOM_0_1()*
4
;
04.
int
y=CCRANDOM_0_1()*
4
;
05.
if
(m_squarearray[x][y]->getNumber()>
0
)
06.
{
07.
autoSquare();
08.
}
09.
else
10.
{
11.
m_squarearray[x][y]->setNumber(
2
);
12.
m_squarearray[x][y]->getCClayerColor()->runAction(CCSequence::create(CCScaleTo::create(
0
,
0
,
0
),CCScaleTo::create(
0
.3f,
1
,
1
),NULL));
13.
}
14.
}
6.附源码
SquareSprite.h01.
#ifndef _SQUARE_SPRITE_H_
02.
#define _SQUARE_SPRITE_H_
03.
#include
"cocos2d.h"
04.
using namespace cocos2d;
05.
class
SquareSprite:
public
CCSprite
06.
{
07.
public
:
08.
SquareSprite();
09.
static
SquareSprite* createSquareSprite(
int
number,
float
pointx,
float
pointy,
int
width,
int
height);
10.
int
getNumber();
11.
void
setNumber(
int
number);
12.
CCLayerColor* getCClayerColor();
13.
CREATE_FUNC(SquareSprite);
14.
private
:
15.
int
m_number;
16.
CCLabelTTF* m_lablenumber;
17.
CCLayerColor* m_layercolor;
18.
void
initSquareSprite(
int
number,
float
pointx,
float
pointy,
int
width,
int
height);
19.
};
20.
#endif
// !_SQUARE_SPRITE_H_
SquareSprite.cpp
01.
#include
"SquareSprite.h"
02.
SquareSprite::SquareSprite()
03.
:m_number(
0
)
04.
,m_lablenumber(NULL)
05.
,m_layercolor(NULL)
06.
{
07.
}
08.
SquareSprite* SquareSprite::createSquareSprite(
int
number,
float
pointx,
float
pointy,
int
width,
int
height)
09.
{
10.
SquareSprite *pSprite =
new
SquareSprite();
11.
if
(pSprite && pSprite->init())
12.
{
13.
pSprite->autorelease();
14.
pSprite->initSquareSprite( number, pointx, pointy, width, height);
15.
return
pSprite;
16.
}
17.
CC_SAFE_DELETE(pSprite);
18.
return
NULL;
19.
}
20.
int
SquareSprite::getNumber()
21.
{
22.
return
m_number;
23.
}
24.
void
SquareSprite::setNumber(
int
number)
25.
{
26.
m_number=number;
27.
m_lablenumber->setString(
""
);
28.
m_layercolor->setColor(ccc3(
200
,
190
,
180
));
29.
if
(m_number>
0
)
30.
{
31.
m_lablenumber->setString(CCString::createWithFormat(
"%i"
,m_number)->getCString());
32.
//设置卡片颜色
33.
switch
(m_number)
34.
{
35.
case
2
:
36.
m_layercolor->setColor(ccc3(
255
,
245
,
238
));
37.
break
;
38.
case
32
:
39.
case
4
:
40.
m_layercolor->setColor(ccc3(
255
,
0
,
0
));
41.
break
;
42.
case
8
:
43.
m_layercolor->setColor(ccc3(
244
,
164
,
96
));
44.
break
;
45.
case
16
:
46.
m_layercolor->setColor(ccc3(
255
,
69
,
0
));
47.
break
;
48.
case
64
:
49.
m_layercolor->setColor(ccc3(
250
,
128
,
114
));
50.
break
;
51.
case
128
:
52.
m_layercolor->setColor(ccc3(
255
,
228
,
181
));
53.
break
;
54.
case
256
:
55.
m_layercolor->setColor(ccc3(
240
,
230
,
140
));
56.
break
;
57.
case
512
:
58.
m_layercolor->setColor(ccc3(
255
,
250
,
205
));
59.
break
;
60.
case
1024
:
61.
m_layercolor->setColor(ccc3(
255
,
160
,
122
));
62.
break
;
63.
case
2048
:
64.
m_layercolor->setColor(ccc3(
250
,
240
,
230
));
65.
break
;
66.
case
4096
:
67.
m_layercolor->setColor(ccc3(
255
,
228
,
225
));
68.
break
;
69.
case
8192
:
70.
m_layercolor->setColor(ccc3(
188
,
143
,
143
));
71.
break
;
72.
case
16384
:
73.
m_layercolor->setColor(ccc3(
205
,
92
,
92
));
74.
break
;
75.
default
:
76.
break
;
77.
}
78.
}
79.
}
80.
void
SquareSprite::initSquareSprite(
int
number,
float
pointx,
float
pointy,
int
width,
int
height)
81.
{
82.
m_number=number;
83.
m_layercolor=CCLayerColor::create(ccc4(
200
,
190
,
180
,
255
),width,height);
84.
m_layercolor->setPosition(ccp(pointx,pointy));
85.
this
->addChild(m_layercolor);
86.
//创建字体
87.
m_lablenumber=CCLabelTTF::create(
""
,
"arial.ttf"
,
30
);
88.
m_lablenumber->setColor(ccc3(
0
,
0
,
0
));
89.
m_lablenumber->setPosition(ccp(m_layercolor->getContentSize().width/
2
,m_layercolor->getContentSize().height/
2
));
90.
m_layercolor->addChild(m_lablenumber);
91.
setNumber(number);
92.
}
93.
CCLayerColor* SquareSprite::getCClayerColor()
94.
{
95.
return
this
->m_layercolor;
96.
}
HelloWorldScene.h
01.
#ifndef __HELLOWORLD_SCENE_H__
02.
#define __HELLOWORLD_SCENE_H__
03.
04.
#include
"cocos2d.h"
05.
#include
"SimpleAudioEngine.h"
06.
#include
"SquareSprite.h"
07.
using namespace CocosDenshion;
08.
using namespace cocos2d;
09.
class
HelloWorld :
public
cocos2d::CCLayer
10.
{
11.
public
:
12.
HelloWorld();
13.
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
14.
virtual bool init();
15.
16.
// there's no 'id' in cpp, so we recommend returning the class instance pointer
17.
static
cocos2d::CCScene* scene();
18.
19.
// implement the "static node()" method manually
20.
CREATE_FUNC(HelloWorld);
21.
public
:
22.
virtual
void
onEnter();
23.
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
24.
virtual
void
ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
25.
//上下左右滑动时调用
26.
bool doTop();
27.
bool doDown();
28.
bool doReight();
29.
bool doLeft();
30.
//初始化矩阵
31.
void
initMatrix(CCSize size);
32.
//随机产生方块
33.
void
autoSquare();
34.
private
:
35.
//启点的X坐标,Y坐标
36.
//起点到终点X的距离,Y的距离
37.
int
m_StartX,m_StartY,m_RangeX,m_RangeY;
38.
//二维数组存储方块
39.
SquareSprite* m_squarearray[
4
][
4
];
40.
};
41.
42.
#endif
// __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
001.
#include
"HelloWorldScene.h"
002.
003.
USING_NS_CC;
004.
HelloWorld::HelloWorld()
005.
:m_StartX(
0
)
006.
,m_StartY(
0
)
007.
,m_RangeX(
0
)
008.
,m_RangeY(
0
)
009.
{
010.
}
011.
CCScene* HelloWorld::scene()
012.
{
013.
// 'scene' is an autorelease object
014.
CCScene *scene = CCScene::create();
015.
016.
// 'layer' is an autorelease object
017.
HelloWorld *layer = HelloWorld::create();
018.
019.
// add layer as a child to scene
020.
scene->addChild(layer);
021.
022.
// return the scene
023.
return
scene;
024.
}
025.
026.
// on "init" you need to initialize your instance
027.
bool HelloWorld::init()
028.
{
029.
//
030.
// 1. super init first
031.
if
( !CCLayer::init() )
032.
{
033.
return
false
;
034.
}
035.
036.
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
037.
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
038.
initMatrix(visibleSize);
039.
//搞点背景
040.
CCLayerColor* layerColorBG= CCLayerColor::create(ccc4(
180
,
170
,
160
,
255
));
041.
this
->addChild(layerColorBG,
0
);
042.
return
true
;
043.
}
044.
void
HelloWorld::initMatrix(CCSize size)
045.
{
046.
//两个方块之间的空隙
047.
int
space=
10
;
048.
int
SquareSize=(size.width-space*
5
)/
4
;
049.
for
(
int
x =
0
; x <
4
; x++)
050.
{
051.
for
(
int
y =
0
; y <
4
; y++)
052.
{
053.
float
pointx=space+x*SquareSize+x*space;
054.
float
pointy=
40
+y*SquareSize+y*space;
055.
SquareSprite* pSquareSprite=SquareSprite::createSquareSprite(
0
,pointx,pointy,SquareSize,SquareSize);
056.
this
->addChild(pSquareSprite,
1
);
057.
m_squarearray[x][y]=pSquareSprite;
058.
}
059.
}
060.
//刚开始游戏,创建两个方块
061.
autoSquare();
062.
autoSquare();
063.
}
064.
void
HelloWorld::autoSquare()
065.
{
066.
int
x=CCRANDOM_0_1()*
4
;
067.
int
y=CCRANDOM_0_1()*
4
;
068.
if
(m_squarearray[x][y]->getNumber()>
0
)
069.
{
070.
autoSquare();
071.
}
072.
else
073.
{
074.
m_squarearray[x][y]->setNumber(
2
);
075.
m_squarearray[x][y]->getCClayerColor()->runAction(CCSequence::create(CCScaleTo::create(
0
,
0
,
0
),CCScaleTo::create(
0
.3f,
1
,
1
),NULL));
076.
}
077.
}
078.
void
HelloWorld::onEnter()
079.
{
080.
CCLayer::onEnter();
081.
this
->setTouchMode(kCCTouchesOneByOne);
082.
this
->setTouchEnabled(
true
);
083.
}
084.
bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
085.
{
086.
m_StartX=pTouch->getLocation().x;
087.
m_StartY=pTouch->getLocation().y;
088.
return
true
;
089.
}
090.
void
HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
091.
{
092.
bool isTouch=
false
;
093.
CCPoint point= pTouch->getLocation();
094.
m_RangeX=m_StartX-point.x;
095.
m_RangeY=m_StartY-point.y;
096.
097.
if
(abs(m_RangeX)>abs(m_RangeY))
098.
{
099.
//X大于Y 左右移动
100.
if
(m_RangeX>
5
)
101.
{
102.
//正数 向左
103.
isTouch=doLeft();
104.
}
105.
else
if
(m_RangeX<-
5
)
106.
{
107.
//负数 向右
108.
isTouch=doReight();
109.
}
110.
}
111.
else
112.
{
113.
//否则 上下移动
114.
if
(m_RangeY>
5
)
115.
{
116.
//正数 向下
117.
isTouch=doDown();
118.
}
119.
else
if
(m_RangeY<-
5
)
120.
{
121.
//负数 向上
122.
isTouch=doTop();
123.
}
124.
}
125.
if
(isTouch)
126.
{
127.
autoSquare();
128.
}
129.
}
130.
bool HelloWorld::doTop()
131.
{
132.
for
(
int
x =
0
; x <
4
; x++)
133.
{
134.
for
(
int
y =
3
; y >=
0
; y--)
135.
{
136.
for
(
int
aftery = y-
1
; aftery >=
0
; aftery--)
137.
{
138.
if
(m_squarearray[x][y]->getNumber()==
0
)
139.
{
140.
m_squarearray[x][y]->setNumber(m_squarearray[x][aftery]->getNumber());
141.
m_squarearray[x][aftery]->setNumber(
0
);
142.
}
143.
else
if
(m_squarearray[x][aftery]->getNumber()==m_squarearray[x][y]->getNumber())
144.
{
145.
bool isAdd=
false
;
146.
for
(
int
temp = aftery+
1
; temp < y; temp++)
147.
{
148.
if
(m_squarearray[x][temp]->getNumber()!=
0
)
149.
{
150.
isAdd=
true
;
151.
break
;
152.
}
153.
}
154.
if
(isAdd)
155.
{
156.
continue
;
157.
}
158.
m_squarearray[x][y]->setNumber(m_squarearray[x][y]->getNumber()*
2
);
159.
m_squarearray[x][aftery]->setNumber(
0
);
160.
break
;
161.
}
162.
}
163.
}
164.
}
165.
return
true
;
166.
}
167.
bool HelloWorld::doDown()
168.
{
169.
for
(
int
x =
0
; x <
4
; x++)
170.
{
171.
for
(
int
y =
0
; y <
4
; y++)
172.
{
173.
for
(
int
aftery = y+
1
; aftery <
4
; aftery++)
174.
{
175.
if
(m_squarearray[x][y]->getNumber()==
0
)
176.
{
177.
m_squarearray[x][y]->setNumber(m_squarearray[x][aftery]->getNumber());
178.
m_squarearray[x][aftery]->setNumber(
0
);
179.
}
180.
else
if
(m_squarearray[x][aftery]->getNumber()==m_squarearray[x][y]->getNumber())
181.
{
182.
bool isAdd=
false
;
183.
for
(
int
temp = y+
1
; temp < aftery; temp++)
184.
{
185.
if
(m_squarearray[x][temp]->getNumber()!=
0
)
186.
{
187.
isAdd=
true
;
188.
break
;
189.
}
190.
}
191.
if
(isAdd)
192.
{
193.
continue
;
194.
}
195.
m_squarearray[x][y]->setNumber(m_squarearray[x][y]->getNumber()*
2
);
196.
m_squarearray[x][aftery]->setNumber(
0
);
197.
break
;
198.
}
199.
}
200.
}
201.
}
202.
return
true
;
203.
}
204.
bool HelloWorld::doReight()
205.
{
206.
for
(
int
y =
0
; y <
4
; y++)
207.
{
208.
for
(
int
x =
3
; x >=
0
; x--)
209.
{
210.
for
(
int
afterx = x-
1
; afterx >=
0
; afterx--)
211.
{
212.
if
(m_squarearray[x][y]->getNumber()==
0
)
213.
{
214.
m_squarearray[x][y]->setNumber(m_squarearray[afterx][y]->getNumber());
215.
m_squarearray[afterx][y]->setNumber(
0
);
216.
}
217.
else
if
(m_squarearray[afterx][y]->getNumber()==m_squarearray[x][y]->getNumber())
218.
{
219.
bool isAdd=
false
;
220.
for
(
int
temp = afterx+
1
; temp < x; temp++)
221.
{
222.
if
(m_squarearray[temp][y]->getNumber()!=
0
)
223.
{
224.
isAdd=
true
;
225.
break
;
226.
}
227.
}
228.
if
(isAdd)
229.
{
230.
continue
;
231.
}
232.
m_squarearray[x][y]->setNumber(m_squarearray[x][y]->getNumber()*
2
);
233.
m_squarearray[afterx][y]->setNumber(
0
);
234.
break
;
235.
}
236.
}
237.
}
238.
}
239.
return
true
;
240.
}
241.
bool HelloWorld::doLeft()
242.
{
243.
for
(
int
y =
0
; y <
4
; y++)
244.
{
245.
for
(
int
x =
0
; x <
4
; x++)
246.
{
247.
for
(
int
afterx = x+
1
; afterx <
4
; afterx++)
248.
{
249.
if
(m_squarearray[x][y]->getNumber()==
0
)
250.
{
251.
m_squarearray[x][y]->setNumber(m_squarearray[afterx][y]->getNumber());
252.
m_squarearray[afterx][y]->setNumber(
0
);
253.
}
254.
else
if
(m_squarearray[afterx][y]->getNumber()==m_squarearray[x][y]->getNumber())
255.
{
256.
bool isAdd=
false
;
257.
for
(
int
temp = x+
1
; temp < afterx; temp++)
258.
{
259.
if
(m_squarearray[temp][y]->getNumber()!=
0
)
260.
{
261.
isAdd=
true
;
262.
break
;
263.
}
264.
}
265.
if
(isAdd)
266.
{
267.
continue
;
268.
}
269.
m_squarearray[x][y]->setNumber(m_squarearray[x][y]->getNumber()*
2
);
270.
m_squarearray[afterx][y]->setNumber(
0
);
271.
break
;
272.
}
273.
}
274.
}
275.
}
276.
return
true
;
277.
}
最终效果图:
FROM: http://www.it165.net/pro/html/201406/16391.html