展会信息港展会大全

cocos2d-x 3.2 之 三消类游戏——万圣大作战 (第三篇),cocos2d-x三消
来源:互联网   发布日期:2015-09-28 13:39:20   浏览:3568次  

导读: cocos2d-x 3.2 之 三消类游戏——万圣大作战 (第三篇),cocos2d-x三消 ***************************************转载请注明出处:ht...

cocos2d-x 3.2 之 三消类游戏——万圣大作战 (第三篇),cocos2d-x三消

***************************************转载请注明出处:http://blog.csdn.net/lttree********************************************

这是第三篇了,

前面已经建立了界面,建立的精灵,并且能自动消除精灵。

现在就要做我们的触摸事件了

本文内容:

> 实现触摸事件

> 实现交换的功能

1.触摸事件

我们玩三消游戏,就要对屏幕进行滑动,所以需要做一个触摸事件来处理对屏幕的触摸。

这里主要是要获得 开始触摸 和 触摸方向两个,

因为只需要知道移动的起始精灵 和 移动的终止精灵。

所以,在游戏界面的初始函数,进行触摸事件的设置(绑定函数 和 添加监听器)

// 触摸事件处理

auto touchListener = EventListenerTouchOneByOne::create();

touchListener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this);

touchListener->onTouchMoved = CC_CALLBACK_2(GameScene::onTouchMoved, this);

_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

PS:关于这个监听器,共有五种触摸事件、键盘响应事件、加速记录事件、鼠标响应事件 和 自定义事件。而且 触摸事件的监听器 还分为两种,单点触摸 和 多点触摸。

而且,在3.0版本以前,之前所用的 CCTouchBegan、CCTouchMoved、CCTouchEnd这些都已经被OUT了,现在是让监听器自己绑定函数。

再回到本文,之前说过,触摸事件最重要的是得到 起始精灵 和 终止精灵,所以要定义这两个变量,然后在onTouchBegan和onTouchMoved进行操作。

先是 onTouchBegan函数:

bool GameScene::onTouchBegan(Touch *touch, Event *unused)

{

staSprite = NULL;

endSprite = NULL;

if ( isTouchEna ) {

auto location = touch->getLocation();

staSprite = spriteOfPoint(&location);

}

return isTouchEna;

}

这里出现了一个新变量——isTouchEna,从名字中可以看出它的作用,是否可以触摸。

什么时候不可以呢? 检测是否有可消除精灵的时候,精灵正在下落的时候,均不可以触摸。

这个变量还需要在前面设置一下,

要在构造函数中,设置变量为 true,

然后在 update函数中isTouchEna = !isAction;(如果精灵正在移动中,就应该忽视触摸事件)

在这里,又出现了一个工具函数——spriteOfPoint(根据触摸点的位置,返回是地图中哪个精灵)

// 根据触摸的点位置,返回是地图中哪个精灵

SpriteShape *GameScene::spriteOfPoint(Point *point)

{

SpriteShape *spr = NULL;

Rect rect = Rect(0, 0, 0, 0);

Size sz;

sz.height=SPRITE_WIDTH;

sz.width=SPRITE_WIDTH;

for( int r = 0 ; r < ROWS ; ++r ) {

for( int c = 0 ; c < COLS ; ++c ) {

spr = map[r][c];

if( spr ) {

rect.origin.x = spr->getPositionX() - ( SPRITE_WIDTH / 2);

rect.origin.y = spr->getPositionY() - ( SPRITE_WIDTH / 2);

rect.size = sz;

if (rect.containsPoint(*point)) {

return spr;

}

}

}

}

return NULL;

}

然后是 onTouchMoved函数:

// 触摸后移动的方向

void GameScene::onTouchMoved(Touch *touch, Event *unused) {

// 如果没有初始精灵 或者 触摸事件不可行,直接返回

if (!staSprite || !isTouchEna) {

return;

}

// 获取 初始精灵 的行列

int row = staSprite->getRow();

int col = staSprite->getCol();

// 获取移动到的 点 的位置

auto location = touch->getLocation();

auto halfSpriteWidth = SPRITE_WIDTH / 2;

auto halfSpriteHeight = SPRITE_WIDTH / 2;

autoupRect = Rect(staSprite->getPositionX() - halfSpriteWidth,

staSprite->getPositionY() + halfSpriteHeight,

SPRITE_WIDTH,

SPRITE_WIDTH);

// 判断是在向哪个方向移动,

if (upRect.containsPoint(location)) {

++row;

if ( row < ROWS ) {

endSprite = map[row][col];

}

swapSprite();

return;

}

autodownRect = Rect(staSprite->getPositionX() - halfSpriteWidth,

staSprite->getPositionY() - (halfSpriteHeight * 3),

SPRITE_WIDTH,

SPRITE_WIDTH);

if (downRect.containsPoint(location)) {

--row;

if ( row >= 0 ) {

endSprite = map[row][col];

}

swapSprite();

return;

}

autoleftRect = Rect(staSprite->getPositionX() - (halfSpriteWidth * 3),

staSprite->getPositionY() - halfSpriteHeight,

SPRITE_WIDTH,

SPRITE_WIDTH);

if (leftRect.containsPoint(location)) {

--col;

if ( col >= 0 ) {

endSprite = map[row][col];

}

swapSprite();

return;

}

autorightRect = Rect(staSprite->getPositionX() + halfSpriteWidth,

staSprite->getPositionY() - halfSpriteHeight,

SPRITE_WIDTH,

SPRITE_WIDTH);

if (rightRect.containsPoint(location)) {

++col;

if ( col < COLS ) {

endSprite = map[row][col];

}

swapSprite();

return;

}

// 否则,并非一个有效的移动

}

这里还有一个函数 swapSprite,顾名思义,就是判断好向哪个方向移动后,直接交换这两个精灵。

2. 交换精灵

关于交换精灵,有两种情况

> 交换后,满足消除条件,消除

> 交换后,不满足消除条件,返回原样

所以,函数是这样的:

// 交换精灵

void GameScene::swapSprite() {

// 移动中,不允许再次触摸,执行动作设置为true

isAction = true;

isTouchEna = false;

// 初始精灵 和 终止精灵 均不能为空

if (!staSprite || !endSprite) {

return;

}

Point posOfSrc = staSprite->getPosition();

Point posOfDest = endSprite->getPosition();

float time = 0.2;

// 在数组中交换位置

map[ staSprite -> getRow() ][staSprite -> getCol() ] = endSprite;

map[ endSprite -> getRow() ][endSprite -> getCol() ] = staSprite;

int tmpRow = staSprite->getRow();

int tmpCol = staSprite->getCol();

staSprite->setRow(endSprite->getRow());

staSprite->setCol(endSprite->getCol());

endSprite->setRow(tmpRow);

endSprite->setCol(tmpCol);

// 检查是否能消除

std::list<SpriteShape *> colChainListOfFirst;

getColChain(staSprite, colChainListOfFirst);

std::list<SpriteShape *> rowChainListOfFirst;

getRowChain(staSprite, rowChainListOfFirst);

std::list<SpriteShape *> colChainListOfSecond;

getColChain(endSprite, colChainListOfSecond);

std::list<SpriteShape *> rowChainListOfSecond;

getRowChain(endSprite, rowChainListOfSecond);

if (colChainListOfFirst.size() >= 3

|| rowChainListOfFirst.size() >= 3

|| colChainListOfSecond.size() >= 3

|| rowChainListOfSecond.size() >= 3) {

// 如果能够消除,仅仅进行移动(不会移动回来)

staSprite->runAction(MoveTo::create(time, posOfDest));

endSprite->runAction(MoveTo::create(time, posOfSrc));

return;

}

// 不能消除,则移动过去还要返回

map[ staSprite -> getRow()][staSprite -> getCol() ] = endSprite;

map[ endSprite -> getRow()][endSprite -> getCol() ] = staSprite;

tmpRow = staSprite->getRow();

tmpCol = staSprite->getCol();

staSprite->setRow(endSprite->getRow());

staSprite->setCol(endSprite->getCol());

endSprite->setRow(tmpRow);

endSprite->setCol(tmpCol);

staSprite->runAction(Sequence::create(

MoveTo::create(time, posOfDest),

MoveTo::create(time, posOfSrc),

NULL));

endSprite->runAction(Sequence::create(

MoveTo::create(time, posOfSrc),

MoveTo::create(time, posOfDest),

NULL));

}

这里,在看一下这个函数,

就是先获取初始精灵 和 终止精灵 的位置,

然后,只是单纯在地图中交换(后台交换,数组变动,在游戏界面中还是没有变动的)

判断是否可以消除,如果可以消除了,那就执行动作 交换两个精灵位置。

如果不可以消除,不要忘了把后台交换过的两个精灵再交换过来,然后在执行两个动作(就是交换一次,再交换回来)。

这里,并不是先交换,再判断,再执行接下来的动作;

而是,先后台数组交换,然后判断,然后执行 交换一次加消除 还是 交换两次动作。

好啦,编写到这里,我们可以运行一下,看看效果。

到这,精灵的交换已经完成了。

本文的代码:>

这里 <

***************************************转载请注明出处:http://blog.csdn.net/lttree********************************************

http://www.bkjia.com/Androidjc/950035.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/950035.htmlTechArticlecocos2d-x 3.2 之 三消类游戏——万圣大作战 (第三篇),cocos2d-x三消 ***************************************转载请注明出处:http://blog.csdn.net/lttree ****...

赞助本站

人工智能实验室

相关热词: android开发 android教程

相关内容
AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港