飞机发射子弹:
弹幕,是雷电游戏的精华。这就需要对子弹的运动轨迹进行控制。
先做一个最简单的子弹发射,后面再添加各种弹幕(实际上主要是研究弹幕)
说是最简单,其实也是最普通的子弹发射,产生一个颗子弹移动就OK了。
方法:做一个子弹发射的计时器,产生子弹,子弹做MoveBy动作就OK了。
代码:
GameLayer.cpp
void GameLayer::fireSchedule(float dt) { Size screenSize = Director::getInstance()->getWinSize() ; Vec2 crePos = m_pPlayerPlane->getPosition() ;//子弹的初始位置 float flyTime = 1.0f ; Sprite * pBullet = Sprite::create("bullet1.png") ; pBullet->setPosition(crePos) ; addChild(pBullet,ZORDER_BULLET) ; flyTime = ((screenSize.height - crePos.y) / screenSize.height)*flyTime ;//移动的时间跟,发射的距离相关 pBullet->runAction( Sequence::create( MoveBy::create(flyTime,Vec2(0,screenSize.height - crePos.y)),RemoveSelf::create(true),nullptr) ) ; }
敌人新建了一个类,因为,到后面敌人可能还需要有一些自己的属性。
EnermySprite.h
#ifndef _ENERMY_SPRITE_H_ #define _ENERMY_SPRITE_H_ #include "cocos2d.h" USING_NS_CC ; typedef enum { kEnermyTypeNone = 0,kEnermyType1,kEnermyType2,kEnermyType3,kEnermyType4,}EnermyType; class EnermySprite : public Sprite { public: static EnermySprite * createByType(EnermyType type); static const char * getImageNameByType(EnermyType type); public: EnermySprite(); ~EnermySprite(); private: }; #endif
EnermySprite.cpp
#include "EnermySprite.h" EnermySprite * EnermySprite::createByType(EnermyType type) { const char * pImage = getImageNameByType(type) ; EnermySprite * pRet = new EnermySprite() ; pRet->initWithFile(pImage) ; pRet->autorelease() ; return pRet ; } const char * EnermySprite::getImageNameByType(EnermyType type) { const char* pImageName = nullptr; switch (type) { case kEnermyType1: pImageName = "enemy1.png" ; break; case kEnermyType2: pImageName = "enemy2.png" ; break; case kEnermyType3: pImageName = "enemy3.png" ; break; case kEnermyType4: pImageName = "enemy4.png" ; break; default: break; } return pImageName ; } EnermySprite::EnermySprite() { } EnermySprite::~EnermySprite() { }
添加到游戏中:
GameLayer.h
void GameLayer::createEneSchedule(float dt) { int ran = CCRANDOM_0_1() * 4 + 1 ; EnermyType type ; switch (ran) { case 1: type = kEnermyType1; break; case 2: type = kEnermyType2; break; case 3: type = kEnermyType3; break; case 4: type = kEnermyType4; break; default: break; } auto pEne = EnermySprite::createByType(type) ; float x = CCRANDOM_0_1()*(Director::getInstance()->getWinSize().width - pEne->getBoundingBox().size.width / 2) + pEne->getBoundingBox().size.width / 2; float y = Director::getInstance()->getWinSize().height + pEne->getBoundingBox().size.height / 2 ; float flyTime = 2.0f ; pEne->runAction( Sequence::create( MoveBy::create(flyTime,-(Director::getInstance()->getWinSize().height + pEne->getBoundingBox().size.height))),CallFuncN::create(CC_CALLBACK_1(GameLayer::enermyMoveEndCallback,this)),nullptr ) ) ; addChild(pEne) ; pEne->setPosition(x,y) ; m_pEnermyVec.pushBack(pEne) ; } void GameLayer::enermyMoveEndCallback(Node * pNode) { EnermySprite * pEnermy = dynamic_cast<EnermySprite*>(pNode) ; m_pEnermyVec.eraSEObject(pEnermy); }
又是一个计时器,记得要添加一个敌人的vector来存放敌人。