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

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

概述***************************************转载请注明出处:http://blog.csdn.net/lttree******************************************** 这是第三篇了, 前面已经建立了界面,建立的精灵,并且能自动消除精灵。 现在就要做我们的触摸事件了 本文内容: > 实现触摸事件 > 实现交换的功能 1.触摸事件

***************************************转载请注明出处: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);	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;        auto  upRect = 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;    }        auto  downRect = Rect(staSprite->getpositionX() - halfSpriteWIDth,staSprite->getpositionY() - (halfSpriteHeight * 3),SPRITE_WIDTH);        if (downRect.containsPoint(location)) {        --row;        if ( row >= 0 ) {            endSprite = map[row][col];        }        swapSprite();        return;    }        auto  leftRect = Rect(staSprite->getpositionX() - (halfSpriteWIDth * 3),staSprite->getpositionY() - halfSpriteHeight,SPRITE_WIDTH);        if (leftRect.containsPoint(location)) {        --col;        if ( col >= 0 ) {            endSprite = map[row][col];        }        swapSprite();        return;    }        auto  rightRect = Rect(staSprite->getpositionX() + halfSpriteWIDth,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,NulL));}



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

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

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

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

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

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

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


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

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



本文的代码: > 这里 <


********************************************

总结

以上是内存溢出为你收集整理的cocos2d-x 3.2 之 三消类游戏——万圣大作战 (第三篇)全部内容,希望文章能够帮你解决cocos2d-x 3.2 之 三消类游戏——万圣大作战 (第三篇)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1036130.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存