void GameScene::onTouchEnded(Touch *touch, Event *unused_event){
int w=abs(touch->getLocation().x-startX);//移动的宽度
int h=abs(touch->getLocation().y-startY);//移动的高度
if(w>h)//左右移动
{
if(touch->getLocation().x>startX)
{//向右
moveAllCard(CARD_DIR::DIR_RIGHT);
}else
{
moveAllCard(CARD_DIR::DIR_LEFT);
}
}else //上下移动
{
if(touch->getLocation().y>startY)
{
moveAllCard(CARD_DIR::DIR_UP);
}else
{
moveAllCard(CARD_DIR::DIR_DOWN);
}
}
}
void GameScene::moveAllCard(CARD_DIR dir){
//按照方向移动这个卡片
switch (dir) {
case CARD_DIR::DIR_LEFT:
//一共要移动4行
for (int row=0; row<4; row++) {
for (int col=1; col<=3; col++) {
int tocol=0;
for (int ncol=col; ncol>0; ncol--) {
if(map[row][ncol]>0)//如果row ncol位置有块
{ if(map[row][ncol-1]==0)// //前面可以移动
{ //修改地图数据
map[row][ncol-1]=map[row][ncol];
map[row][ncol]=0;
//修改卡片数据
allCard.at(map[row][ncol-1]-1)
->moveCard(row, ncol-1);
}
else //map[row][ncol]有块 map[row][ncol-1]也又块
{ //判断2个块的值是否相等
int num1=allCard.at(map[row][ncol-1]-1)->number;
int num2=allCard.at(map[row][ncol]-1)->number;
if(num1==num2)
{
allCard.at(map[row][ncol-1]-1)->doubleMe();
addScore(num1*2);
int backindex=map[row][ncol]-1;
map[row][ncol]=0;
allCard.at(backindex)->removeFromParentAndCleanup(true);
allCard.erase(backindex);//移除后面的块
//调整索引值
for (int i=0; i<4; i++) {
for (int j=0; j<4; j++) {
if(map[i][j]>backindex)
{
map[i][j]--;
}
}
}
}
}
}
}
}
}
break;