[cocos2dx学习笔记]用cocos2dx3.X完成塔防游戏王国保卫战--地图二

接上文,添加地图,在地图层直接添加即可,我是将AnchorPoint设置在(0,0),方便计算坐标

mapSprite = Sprite::createWithSpriteFrameName(String::createWithFormat("Stage_%d.png",level+1)->getCString());
mapSprite->setAnchorPoint(Point(0,0));
mapSprite->setPosition(Point(0,0));
addChild(mapSprite);

本章节主要介绍两个固定技能和商店技能的实现


首先是两个固定技能,以陨石为例

首先添加按键图片精灵

stonesprite = Sprite::createWithSpriteFrameName("power_portrait_fireball_0001.png");
stonesprite->setAnchorPoint(Point(0,0));
stonesprite->setPosition(Point(10,-20));
stonesprite->setName("inactive");
//判断倒计时是否完毕
completeStone = false;
addChild(stonesprite,1);     

然后是倒计时遮盖层,采用的是Progresstimer实现,放在按键图片精灵上面
stoneTimer = Progresstimer::create(Sprite::createWithSpriteFrameName("power_loading.png"));
stoneTimer->setAnchorPoint(Point(0,0));
//顺时针转动
stoneTimer->setReverseDirection(true);
stoneTimer->setPosition(Point(10,-20));
stoneTimer->setPercentage(100);//显示原形的百分比
this->addChild(stoneTimer,1);


添加定时器,更新Progresstimer状态

void PlayerStateMenu::updateStoneProgress(float Dt){  
	stoneTimer->setPercentage(stoneTimer->getPercentage() - Dt*2);//更新进度2
	if (stoneTimer->getPercentage()==0) {
		this->unschedule(schedule_selector(PlayerStateMenu::updateStoneProgress));//取消定时器
		completeStone = true;
	}
	return;
}

在你想要开始的时候schedule它比如第一波敌人出现之后
添加触摸响应
auto stoneListener = EventListenerTouchOneByOne::create();
stoneListener->onTouchBegan = [&](Touch* touch,Event* event){
		
	auto target = static_cast<Sprite*>(event->getCurrentTarget());
	Point locationInNode = target->convertTouchToNodeSpace(touch);
	Size size = target->getContentSize();
	Rect rect = Rect(0,size.width,size.height);
	//若第一次点击点击
	if(rect.containsPoint(locationInNode)){
		//若冷却结束
		if(completeStone == true){
			//移出其他技能触摸监听
			mTouchLayer->removeAllListener();
			if(stonesprite->getName() == "inactive"){
			//设置为点击状态
			stonesprite->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("power_portrait_fireball_0002.png"));
			//改变状态TAG
			stonesprite->setName("active");
			//改变其他2个按键状态
			/****
			****/
			//触摸层设置陨石技能监听
			mTouchLayer->setFireBallTouchShield();
		//第二次点击,即取消
		}else{
			mTouchLayer->removeFireBallTouchShield();
			stonesprite->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("power_portrait_fireball_0001.png"));
			stonesprite->setName("inactive");
		<span style="white-space:pre">	</span>}
<span style="white-space:pre">		</span>}
		return true;
	}
		return false;  
};
stoneListener->onTouchEnded = [&](Touch* touch,Event* event){
};
//触摸吞噬
stoneListener->setSwallowtouches(true);
_eventdispatcher->addEventListenerWithSceneGraPHPriority(stoneListener,stonesprite);

当倒计时结束的时候,将completeStone置为true,只有此时点击按键才会触发。

点击技能,在触摸层添加一个EventListenerTouchOneByOne,覆盖整个触摸层,此时点击地图时,会执行这个触摸事件

下面来看看触摸层


class TouchLayer :public Layer
{
public:
	virtual bool init();
    CREATE_FUNC(TouchLayer);
	EventListenerTouchOneByOne* touchlistener;
	EventListenerTouchOneByOne* FiereBalllistener;

	void setFireBallTouchShield();
	void removeFireBallTouchShield();
	bool onFireBallTouchBegan(Touch* touch,Event* event);
	void onFireBallTouchEnded(Touch* touch,Event* event);

	bool isFlag;
	bool onTouchBegan(Touch* touch,Event* event);
	void onTouchEnded(Touch* touch,Event* event);
	void onTouchMoved(Touch* touch,Event* event);
	Size winSize;
	bool isMoved;

	void removeAllListener();
};
这里我只截取了和陨石有关以及移动地图的部分

在BaseMap里添加触摸监听层

void BaseMap::initTouchLayer()
{
	mTouchLayer = TouchLayer::create();
	mTouchLayer->setContentSize(mapSprite->getContentSize());
	mTouchLayer->setAnchorPoint(Point(0,0));
	mTouchLayer->setPosition(Point(0,0));
	addChild(mTouchLayer,99);
}

添加地图移动时间触摸

touchlistener = EventListenerTouchOneByOne::create();
touchlistener->onTouchBegan = CC_CALLBACK_2(TouchLayer::onTouchBegan,this);
touchlistener->onTouchEnded = CC_CALLBACK_2(TouchLayer::onTouchEnded,this);
touchlistener->onTouchMoved = CC_CALLBACK_2(TouchLayer::onTouchMoved,this);
touchlistener->setSwallowtouches(true);
_eventdispatcher->addEventListenerWithFixedPriority(touchlistener,-1);
_eventdispatcher->addEventListenerWithSceneGraPHPriority(touchlistener,this);


这里设置将FiexPriority设置为-1为了确保触摸事件先与技能等其他触摸时间触发

void TouchLayer::onTouchEnded(Touch* touch,Event* event)
{
<span style="white-space:pre">	</span>touchlistener->setSwallowtouches(isMoved);
	isMoved = false;
}

void TouchLayer::onTouchMoved(Touch* touch,Event* event)
{
        // 计算滑动过程中的滑动增量
        auto diff = touch->getDelta(); 

		//手指移动修正,因为手指触摸不像鼠标触摸那么固定
		if(abs(diff.x) >5|| abs(diff.y) >5){
			isMoved = true;
		}
			// 得到当前bgSprite的位置
			auto currentPos = this->getParent()->getPosition();
			// 得到滑动后bgSprite应该所在的位置
			auto pos = currentPos + diff;

			//边界控制,约束pos的位置
			pos.x = MIN(pos.x,0);
			pos.x = MAX(pos.x,-1200 + winSize.width);
			pos.y = MIN(pos.y,0);
			pos.y = MAX(pos.y,-1000 + winSize.height);
			// 重设地图层位置

			this->getParent()->setPosition(pos);
}

当手指在触摸层上移动的时候,isMoved会为true,这是setSwallowtouches(isMoved)会将其他触摸事件吞噬
这样是为了确保移动的时候经过或者移动结束的时候碰巧在某触摸点时,不会触发其他触摸事件(比如说移动完手指正好在某个防御塔上,这样就不会弹出防御塔升级层)
另外当移动的时候也不会触发技能事件监听,可以移动完再选择技能释放地点


void TouchLayer::setFireBallTouchShield()
{
	//调用方法创建陨石技能触摸时间
	FiereBalllistener = EventListenerTouchOneByOne::create();
	FiereBalllistener->onTouchBegan = CC_CALLBACK_2(TouchLayer::onFireBallTouchBegan,this);
	FiereBalllistener->onTouchEnded = CC_CALLBACK_2(TouchLayer::onFireBallTouchEnded,this);
	FiereBalllistener->setSwallowtouches(true);
	//设置比移动触摸事件高即可
	_eventdispatcher->addEventListenerWithFixedPriority(FiereBalllistener,1);
	_eventdispatcher->addEventListenerWithSceneGraPHPriority(FiereBalllistener,this);
}

void TouchLayer::removeFireBallTouchShield()
{
	//使用技能完毕去除此监听时间
	if(FiereBalllistener!=NULL)
		_eventdispatcher->removeEventListener(FiereBalllistener);
}

bool TouchLayer::onFireBallTouchBegan(Touch* touch,Event* event)
{
	//直接返回TRUE,拦截其他时间
	return true;
}

void TouchLayer::onFireBallTouchEnded(Touch* touch,Event* event)
{
	//播放音效
	SoundManager::playFireballUnleash();
	//创建3个陨石
	auto fireBall1 = FireBall::create();
	addChild(fireBall1);
	fireBall1->shoot(static_cast<TouchLayer*>(event->getCurrentTarget())->convertTouchToNodeSpace(touch)+Point(-30,300));
	auto fireBall2 = FireBall::create();
	addChild(fireBall2);
	fireBall2->shoot(static_cast<TouchLayer*>(event->getCurrentTarget())->convertTouchToNodeSpace(touch)+Point(0,350));
	auto fireBall3 = FireBall::create();
	addChild(fireBall3);
	fireBall3->shoot(static_cast<TouchLayer*>(event->getCurrentTarget())->convertTouchToNodeSpace(touch)+Point(30,280));
	//陨石坠落之后获取父类的玩家状态层,调用startStone,重新开始计时并且重置Progresstimer遮盖层
	static_cast<BaseMaP*>(this->getParent())->playerState->startStone();
	//移除此监听事件
	removeFireBallTouchShield();
}

我实现整个技能监听加上触摸移动差不多就是这样

商店技能,召唤士兵等其他技能也是同样的思路,只是使用的技能不同罢了~其他的例如冰冻敌人、召唤士兵等将在对应的模块中一一介绍

今天先暂时写到这样,还有苦逼的实习等着我去找

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...