鸟, 实现飞行,掉落,扇扇翅膀,停止扇翅膀等,扇翅膀用到了动画animate,实际就是三帧动画不停放,没有图像和动画处理的感觉,总感觉这鸟呆。
BirdSprite.h
#ifndef _BIRD_SRPITE_H_ #define _BIRD_SRPITE_H_ #include "cocos2d.h" class BirdSprite : public cocos2d::Sprite { public: static BirdSprite *getInstance(); void flyUp(); void fallingDown(); virtual void update(float delta) override; void birdDead(); bool isOnFloor(); public: // void setBirdState(BirdState state){ _state = state; } // BirdState getBirdState(){ return _state; } private: BirdSprite(){}; CREATE_FUNC(BirdSprite); virtual bool init() override; void flyCallBack(); private: enum class BirdState { BIRD_READY = 0, BIRD_FLYING, BIRD_FALLING, BIRD_DEAD }; static BirdSprite *_birdSprite; cocos2d::Action *_fly; cocos2d::Action *_fall; BirdState _state; float _gravity; cocos2d::Animate *_animate; }; #endif //_BIRD_SRPITE_H_
BirdSprite.cpp
#include "BirdSprite.h" #include "BirdActions.h" #include "GameInfo.h" USING_NS_CC; BirdSprite * BirdSprite::_birdSprite = NULL; bool BirdSprite::init() { if (!this->initWithSpriteFrameName("bird_hero1.png")) { return false; } _gravity = GA; _state = BirdState::BIRD_READY; this->setAnchorPoint(Vec2(1.0f, 1.0f)); this->setPosition(Vec2(Director::getInstance()->getVisibleSize().width / 4, Director::getInstance()->getVisibleSize().height / 2)); //create animation auto animation = Animation::create(); //get SpriteFrame for animation, 3 frames in total __String framName; for (int i(1); i <= 3; ++i) { framName.initWithFormat("bird_hero%i.png", i); auto spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(framName._string); animation->addSpriteFrame(spriteFrame); } animation->setDelayPerUnit(0.2f / 3); animation->setRestoreOriginalFrame(false); animation->setLoops(-1); //create and run animate _animate = Animate::create(animation); _animate->retain(); this->runAction(_animate); //create the falling begin _fall = EaseOut::create(RotateTo::create(0.4f, 45), 0.4f); _fall->retain(); // this->runAction(_fall); //create the fly action //想提前创建_fly,但是在触摸的时候调用runAction(_fly->clone()),动作极其不协调 // _fly = Sequence::create( // Spawn::create( // Sequence::create(EaseIn::create(RotateTo::create(0.25f, -45), 0.4f), EaseOut::create(RotateTo::create(0.25f, 0), 0.4f), NULL), // EaseIn::create(JumpBy::create(0.5f, Vec2(0, HEIGHT_FLY), HEIGHT_FLY, 1), 0.4f), NULL), // CallFunc::create(std::bind(&BirdSprite::flyCallBack, this)), // NULL); // _fly->retain(); this->schedule((SEL_SCHEDULE)&BirdSprite::update); // _fly = FlyAction::create(); // _fall = FallAction::create(); //for collision return true; } void BirdSprite::flyCallBack() { // this->stopAction(_fall); this->stopAction(_fly); _gravity = GA; this->runAction(_fall->clone()); _state = BirdState::BIRD_FALLING; } void BirdSprite::flyUp() { if (_state == BirdState::BIRD_FLYING) { this->stopAction(_fly); } else if (_fall) { this->stopAction(_fall); _state = BirdState::BIRD_FLYING; } auto height = Director::getInstance()->getVisibleSize().height - this->getPositionY(); height = (height > HEIGHT_FLY ? HEIGHT_FLY : height); _fly = Sequence::create( Spawn::create( Sequence::create(EaseIn::create(RotateTo::create(0.25f, -45), 0.4f), EaseOut::create(RotateTo::create(0.25f, 0), 0.4f), NULL), EaseIn::create(JumpBy::create(0.5f, Vec2(0, height), height, 1), 0.4f), NULL), CallFunc::create(std::bind(&BirdSprite::flyCallBack, this)), NULL); //_fly->retain(); _gravity = GA; this->runAction(_fly); } BirdSprite *BirdSprite::getInstance() { if (!_birdSprite) { _birdSprite = BirdSprite::create(); } return _birdSprite; } void BirdSprite::update(float delta) { switch (_state) { case BirdState::BIRD_FALLING: case BirdState::BIRD_DEAD: if (this->getPositionY() >= this->getContentSize().height && this->getPositionY() <= Director::getInstance()->getVisibleSize().height) { this->setPositionY(this->getPositionY() - _gravity); _gravity += 0.3f; } break; case BirdState::BIRD_FLYING: break; default: break; } } void BirdSprite::birdDead() { if (_state == BirdState::BIRD_FLYING) { this->stopAction(_fly); this->runAction(_fall); } this->stopAction(_animate); _state = BirdState::BIRD_DEAD; } bool BirdSprite::isOnFloor() { if (this->getPositionY() <= this->getContentSize().height) { return true; } return false; }
AppDelegate做如下修改,只为我的背景图就那么大,我还没搞清缩放
bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { glview = GLView::createWithRect("Happy Bird", Rect(0, 0, 284, 512)); director->setOpenGLView(glview); } // turn on display FPS director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object auto scene = GameScene::createScene(); // run director->runWithScene(scene); return true; }
水管管理类管理水管的方法是在初始化的时候生成几根水管放到一数组里,等距排开,然后大家一起动。