本实例通过博客http://blog.csdn.net/column/details/flappybird.html在cocos2d-x-3.16上实现整理

一、用TexturePacker做成flappy_packer.png合图

TexturePacker工具:链接:https://pan.baidu.com/s/1htqSysG 密码:xvsi

flappy图片资源:链接:https://pan.baidu.com/s/1jM2_yMjkBBcNSl43i0f4Ig 密码:4h0v

image.png

二、cocos2dx提供加载纹理的接口

Director::getInstance()->getTextureCache()->addImageAsync("flappy_packer.png", CC_CALLBACK_1(LoadingScene::loadingCallBack, this));
TextureCache这个类里面的addImageAsync的方法便是一个异步加载图片纹理的方法,第一个参数是图片的名字,第二个参数便是图片异步加载完成之后需要调用的回调函数

flappy_packer.png是放在image文件夹下的,在AppDelegate.cpp下有将image添加到搜索路径,所以直接写文件名就可以了

FileUtils::getInstance()->addSearchPath("fonts");
FileUtils::getInstance()->addSearchPath("image");
FileUtils::getInstance()->addSearchPath("sounds");

loadingCallback回调函数加载帧动画和声音文件后跳转到欢迎界面

void LoadingScene::loadingCallBack(Texture2D *texture){
//预加载帧缓存纹理
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("flappy_packer.plist", texture);

// After loading the texture , preload all the sound
SimpleAudioEngine::getInstance()->preloadEffect("sfx_die.ogg");
SimpleAudioEngine::getInstance()->preloadEffect("sfx_hit.ogg");
SimpleAudioEngine::getInstance()->preloadEffect("sfx_point.ogg");
SimpleAudioEngine::getInstance()->preloadEffect("sfx_swooshing.ogg");
SimpleAudioEngine::getInstance()->preloadEffect("sfx_wing.ogg");

// After load all the things, change the scene to new one
//auto scene = HelloWorld::createScene();
auto scene = WelcomeScene::create();
TransitionScene *transition = TransitionFade::create(1, scene);
Director::getInstance()->replaceScene(transition);

}

项目源码托管在Github:https://github.com/smiger/FlappyBird​