微信经典飞机大战之三:飞机登场、帧动画、飞机层接口和属性

来源:互联网 发布:sql语句中union 编辑:程序博客网 时间:2024/05/01 13:11

1.  飞机来临,并将飞机添加到场景中

我们用PlaneLayer来放置飞机,我们的主角。和导演类CCDirector的shareDirector一样,我们在PlaneLayer中也添加PlaneLayer的一个静态指针sharePlane。但是这里和导演类不一样的是,我们必须在创建PlaneLayer后才能使用这个sharePlane,而且在PlaneLayer销毁后就不能使用它了。

PlaneLayer的头文件如下:

#pragma once#include "cocos2d.h"USING_NS_CC;class PlaneLayer : public CCLayer{public:PlaneLayer(void);~PlaneLayer(void);static PlaneLayer* create();  //实现create函数virtual bool init();public:static PlaneLayer* sharedPlane;  //提供sharePlane全局指针};
PlaneLayer.cpp文件如下:

#include "PlaneLayer.h"PlaneLayer* PlaneLayer::sharedPlane=NULL;PlaneLayer::PlaneLayer(void){isAlive=true;}PlaneLayer::~PlaneLayer(void){}PlaneLayer* PlaneLayer::create(){PlaneLayer* pRet=new PlaneLayer();if(pRet && pRet->init()){pRet->autorelease();sharedPlane=pRet;return pRet;}else{CC_SAFE_DELETE(pRet);return NULL;}}

这里不能再简单的使用LAYER_CREATE_FUNC宏了,而是要自己实现create。

我们还需要把这个层添加到GameLayer.cpp的init函数中,才能调用。这样就可以把飞机添加到场景中。

//加入planeLayerthis->planeLayer=PlaneLayer::create();this->addChild(planeLayer);


2.  添加动画效果

有一个帧动画,就是飞机在飞行过程中机尾是在喷火的。

下面介绍一下帧动画的使用步骤:

(1)create,创建CCAnimation类实例

(2)setDelayPerUnit,设置帧间间隔时间

(3)addSpriteFrame,添加帧图片

(4)create,创建CCAnimation类实例,传入(1)从CCAnimation实例,注意CCAnimation是动画过程的名词,CCAnimate才是动画动作。

(5)精灵调用runAction

(6)注意CCAnimationCache这是一个动画类全局缓冲池。可以将大量重复动画放在里面使用,加快游戏速度。

bool PlaneLayer::init(){bool bRet=false;do{CC_BREAK_IF(!CCLayer::init());CCSize winSize=CCDirector::sharedDirector()->getWinSize();//加载全局资源CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("ui/shoot.plist");CCSprite* plane=CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("hero1.png"));//把飞机放置在底部中央plane->setPosition(ccp(winSize.width/2,plane->getContentSize().height/2));//添加精灵,AIRPLANE是tagthis->addChild(plane,0,AIRPLANE);//闪烁动画CCBlink* blink=CCBlink::create(1,3);CCAnimation* animation=CCAnimation::create();animation->setDelayPerUnit(0.1f);animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("hero1.png"));animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("hero2.png"));CCAnimate* animate=CCAnimate::create(animation); //帧动画plane->runAction(blink); //执行闪烁动画plane->runAction(CCRepeatForever::create(animate)); //执行帧动画bRet=true;} while (0);return bRet;}


3.  飞机层PlaneLayer的其他属性和接口

(1)bool isAlive;判断飞机主角是否活着的标志,初始为true

(2)int score;分数,刚开始为0

(3)void MoveTo(CCPoint location);飞机层移动的方法。触摸飞机移动

(4)void Blowup(int passScore);飞机爆炸的方法。当主角碰到炸弹死亡后执行爆炸动画效果

(5)void RemovePlane();移除飞机并转换至GameOver场景

以上这些内容都会在后续章节中有介绍。




0 0
原创粉丝点击