现在来让我们的英雄发射子弹,主要需要监听屏幕触摸事件,根据触摸点的坐标和英雄的位置决定子弹行走路线。然后对子弹和屏幕上的敌人进行碰撞检测,若击中敌人则把子弹和敌人都从该层清除。
在头文件GameScene.h中添加如下代码:

        void update(cocos2d::ccTime dt);

        void ccTouchesEnded(cocos2d::CCSet *touches,cocos2d::CCEvent *event);

private:
	cocos2d::CCMutableArray<cocos2d::CCSprite *> *enemies;
	cocos2d::CCMutableArray<cocos2d::CCSprite *> *projectiles;

添加了两个成员变量:enemies和projectiles,这两个容器是用来存储屏幕上的敌人和子弹的,为后面的碰撞检测做准备。
修改源文件GameScene.cpp:
在init函数中添加:

enemies = new CCMutableArray<CCSprite *>;
projectiles = new CCMutableArray<CCSprite *>;
this->setIsTouchEnabled(true);
this->schedule(schedule_selector(SimpleGame::update));

定时器不指定时间表示每帧都调用。

在addEnemy函数中添加:

enemy->setTag(1);
enemies->addObject(enemy);

设置敌人精灵tag为1,子弹精灵tag为2。

在析构函数~SimpleGame中添加:

	if(enemies)
	{
		enemies->release();	
		enemies = NULL;
	}

	if(projectiles)
	{
		projectiles->release();	
		projectiles = NULL;
	}

在spriteMoveFinished函数中添加:

	if(sprite->getTag() == 1)
	{
		enemies->removeObject(sprite);		
	}
	else if(sprite->getTag() == 2)
	{
		projectiles->removeObject(sprite);	
	}

添加update和ccTouchesEnded函数:监听触屏事件和实现碰撞检测。

void SimpleGame::ccTouchesEnded(CCSet *touches,CCEvent *event)
{
	//CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav");
	CCTouch *touch = (CCTouch *)(touches->anyObject());
	CCPoint location = touch->locationInView(touch->view());
	location = CCDirector::sharedDirector()->convertToGL(location);

	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	CCSprite *projectile = CCSprite::spriteWithFile("Projectile.png",CCRectMake(0,0,20,20));
	projectile->setPosition(ccp(20,winSize.height / 2));

	int offX = location.x - projectile->getPosition().x;
	int offY = location.y - projectile->getPosition().y;
	if(offX <= 0) return;

	this->addChild(projectile);

	int realX = winSize.width + projectile->getPosition().x / 2;
	float ratio = (float)offY / (float)offX;
	int realY = realX * ratio + projectile->getPosition().y;

	int realOffX = realX - projectile->getPosition().x;
	int realOffY = realY - projectile->getPosition().y;
	float distance = sqrtf(realOffX * realOffX + realOffY * realOffY);
	float velocity = 480 / 1;
	float realMoveDuration = distance / velocity;
	projectile->runAction(CCSequence::actions(CCMoveTo::actionWithDuration(realMoveDuration,ccp(realX,realY)),
		                                                           CCCallFuncN::actionWithTarget(this,callfuncN_selector(SimpleGame::spriteMoveFinished))
							             ,NULL));

	projectile->setTag(2);
	projectiles->addObject(projectile);

}

void SimpleGame::update(ccTime dt)
{
	CCMutableArray<CCSprite *>::CCMutableArrayIterator it,jt;
    CCMutableArray<CCSprite *> *projectileToDelete = new CCMutableArray<CCSprite *>;
	//遍历所有子弹,然后根据子弹遍历所有敌人,检查子弹跟敌人是否发生碰撞
	for(jt = projectiles->begin(); jt != projectiles->end(); jt ++)
	{
		CCSprite *projectile = *jt;
		CCRect projectileRect = CCRectMake(projectile->getPosition().x - projectile->getContentSize().width/2,
									   projectile->getPosition().y - projectile->getContentSize().height/2,
									   projectile->getContentSize().width,
									   projectile->getContentSize().height);
		CCMutableArray<CCSprite *> *enemyToDelete = new CCMutableArray<CCSprite *>;
		for(it = enemies->begin(); it != enemies->end(); it ++)
		{
			CCSprite *enemy = *it;
			CCRect enemyRect = CCRectMake(enemy->getPosition().x - enemy->getContentSize().width/2,
									   enemy->getPosition().y - enemy->getContentSize().height/2,
									   enemy->getContentSize().width,
									   enemy->getContentSize().height);
			//如果碰撞,则把被击中的敌人放进要删除的列表中
			if(CCRect::CCRectIntersectsRect(projectileRect,enemyRect))
			{
				enemyToDelete->addObject(enemy);	
			}
		}

		for(it = enemyToDelete->begin(); it != enemyToDelete->end(); it ++)
		{
			CCSprite *enemy = *it;
			enemies->removeObject(enemy);
			this->removeChild(enemy,true);
		}

		//无论该子弹击中多少敌人,都只添加一次
		if(enemyToDelete->count() > 0)
		{
			projectileToDelete->addObject(projectile);
		}

		enemyToDelete->release();
	}

	for(jt = projectileToDelete->begin(); jt != projectileToDelete->end(); jt ++)
	{
		CCSprite *projectile = *jt;
		projectiles->removeObject(projectile);
		this->removeChild(projectile,true);
	}
	projectileToDelete->release();

}

编译运行程序,现在可以把敌人一个一个的干掉了。

cocos2d-x手游性能优化总结

近段时间在使用cocos2d-x开发2D手游,技术方案使用的是cocos2d-x+lua,因为游戏使用的是cocos2d-x 2.1.5版本,有些优化方案在最新版的cocos2d-x版本已经实现...

阅读全文

cocos2dx-html5 实现网页版flappy bird游戏

我也是第一次使用cocos2d_html5,对js和html5也不熟,看引擎自带的例子和引擎源码,边学边做,如果使用过cocos2d-x的话,完成这个游戏还是十分简单的。游戏体...

阅读全文

【cocos2d-x开发实战 特训99-终结篇】移植到android平台和添加admob广告

上一篇已经完成特性99在win32平台下的开发,现在把它移植到android上,首先修改Android.mk文件,内容如下: LOCAL_PATH := $(call my-dir) include $(CLEAR_...

阅读全文

欢迎留言