【一】仿微信飞机大战cocos2d-x3.0rc1

来源:互联网 发布:ps如何做淘宝详情页 编辑:程序博客网 时间:2024/05/16 05:32

参考 【偶尔e网事】 的 【cocos2d-x入门实战】微信飞机大战  cocos2dx 2.0版本,偶尔e网事他写的非常详细,面面俱到,大家非常有必要看下。可以通过下面链接跳转:

cocos2d-x入门实战

这里面我以【cocos2d-x入门实战】微信飞机大战 为蓝本,用cocos2dx 3.0rc1翻版。安装环境什么的,我就不说了,网上都可以找到,我直接从游戏开始界面说起。

想往下看的话,你必须会的一件事,就是你已经能创建出cocos2dx3.rc1的helloworld工程。

飞机大战源码和资源放在第四节中,不想看的直接去第四节中找吧


打飞机是一项需要前戏的运动,所以我们加个欢迎界面什么的,搞搞前戏气氛,还是很有必要的。

下面就让我们完成前戏,该做的事情:

1.游戏开始界面


一、首先是开始欢迎界面的展示


这里我们实现了简单静态界面,以及一个炫酷的动态图,虽然只是三秒钟!,我这里直接用了偶尔e网事大神的资源,大神请原谅我把你的飞机升级成3.0版本的,如果不爽,请过来打我~好吧,我好jian.....


二、初始工程的介绍

假设你已经创建了一个名为“PlayThePlane”的工程,那么你的解决方案将会是这样的:



工程是从main.cpp开始执行的:

int APIENTRY _tWinMain(HINSTANCE hInstance,                       HINSTANCE hPrevInstance,                       LPTSTR    lpCmdLine,                       int       nCmdShow){    UNREFERENCED_PARAMETER(hPrevInstance);// UNREFERENCED_PARAMETER 告诉编译器,已经使用了该变量,不必检测警告!    UNREFERENCED_PARAMETER(lpCmdLine);    // 要是没加,应该会有这个“warning C4100: “lpCmdLine” : unreferenced formal parameter.”    // create the application instance 创建应用实例    AppDelegate app;    return Application::getInstance()->run();// cocos2dx AppDelegate程序正式开始运行}
Application::getInstance()->run()里面到底运行了什么呢?混蛋,自己跳进去看下不就知道了,又不是陷阱,那可都是宝藏堆。我只告诉你它调用了AppDelegate.h中的applicationDidFinishLaunching();这时候我们看看

bool AppDelegate::applicationDidFinishLaunching() {    // initialize director 导演    auto director = Director::getInstance();// 窗体框架    auto glview = director->getOpenGLView();    if(!glview) {        glview = GLView::createWithRect("PlayerThePlane", Rect(0, 0, 480, 800)); // 窗体名 + 宽高规格        director->setOpenGLView(glview);// 1.LOOK 该函数会自动按设计宽高和缩放方式适应手机屏幕,设置游戏分辨率 (设计宽,设计高,缩放方式)。glview->setDesignResolutionSize(480, 800, kResolutionNoBorder);     }    // turn on display FPS 打印帧率,不希望左下角三行出现的 就注释掉 或者设置false    director->setDisplayStats(true);    // set FPS. the default value is 1.0/60 if you don't call this 一秒60帧    director->setAnimationInterval(1.0 / 60);    // create a scene. it's an autorelease object 创建场景    auto scene = HelloWorld::createScene();    // run 导演让场景开始运作    director->runWithScene(scene);    return true;}

这里我们修改和添加的东西有:

glview = GLView::createWithRect("PlayerThePlane", Rect(0, 0, 480, 800));  我们设置了我们飞机的名字,和容纳的空间

glview->setDesignResolutionSize(480, 800, kResolutionNoBorder); 

注释很清楚,就不再解释了。

auto scene = HelloWorld::createScene();这个就是我们的开始场景,auto是c++11的特性。触景生情,好的场景,会让人情不自禁的想把这个飞机打下去,所以我们有必要要让场景炫起来。

director->runWithScene(scene);把scene场景交给导演来运作


三、游戏开始界面的具体实现

我们先看下HelloWorldScene.h代码:

#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"class HelloWorld : public cocos2d::Layer{public:// 产生一个场景,然后把本Layer层类加入到场景中去    static cocos2d::Scene* createScene();    // 在Layer层中添加精灵元素    virtual bool init();          // a selector callback 退出按钮回调    void menuCloseCallback(cocos2d::Ref* pSender);    // 它的具体实现其实就是HelloWorld::create(),你进入CREATE_FUNC宏定义可以看到    CREATE_FUNC(HelloWorld);public:void loadingDone(Node* pNode); // 从开始界面 跳到游戏界面void PreloadMusicAndPicture(); // 预加载音乐和图片};#endif // __HELLOWORLD_SCENE_H__


好吧,我发现我都注释了,没什么好说,直接看HelloWorldScene.cpp代码:
#include "HelloWorldScene.h"#include "SimpleAudioEngine.h"USING_NS_CC;Scene* HelloWorld::createScene(){// 创建一个自动释放的场景    auto scene = Scene::create(); // 创建一个自动释放的layer层    auto layer = HelloWorld::create();// 场景中加入layer层    scene->addChild(layer);    // 返回场景    return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){// 当你想调用父类的virtual,又想有自己的实现的时候,就这么写    if ( !Layer::init() )    {        return false;    }        Size visibleSize = Director::getInstance()->getVisibleSize();    Point origin = Director::getInstance()->getVisibleOrigin();// 创建退出按钮    auto closeItem = MenuItemImage::create(                                           "CloseNormal.png",                                           "CloseSelected.png",                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));    closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,                                origin.y + closeItem->getContentSize().height/2));    auto menu = Menu::create(closeItem, NULL);    menu->setPosition(Point::ZERO);    this->addChild(menu, 1);    /////////////////////////////    // 3. add your codes below...    // add a label shows "Hello World"    // create and initialize a label    // 下面的代码去掉,加入自己的代码// 返回OpenGL视图的大小Size winSize=Director::getInstance()->getWinSize();// 预加载图片和音乐PreloadMusicAndPicture();// 背景图(精灵)auto background = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("background.png"));background->setPosition(Point(winSize.width/2,winSize.height/2)); // 设置位置// 场景中加入背景图this->addChild(background);// 加入copyright图片(精灵)auto copyRight = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("shoot_copyright.png"));copyRight->setAnchorPoint(Point(0.5, 0)); // 描点copyRight->setPosition(Point(winSize.width/2,winSize.height/2));this->addChild(copyRight);// 加入loading图片(精灵)auto loading = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading1.png"));loading->setPosition(Point(winSize.width/2,winSize.height/2));this->addChild(loading);// Animation是由许多精灵帧组成,可以设置间隔时间,持续时间等,它实际上是包含着一组数据Animation* animation=Animation::create();animation->setDelayPerUnit(0.2f); // 间隔时间animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading1.png"));animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading2.png"));animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading3.png"));animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading4.png"));// 通过帧数据创建帧动作(创建序列帧动画)Animate* animate=Animate::create(animation);Repeat* repeat=Repeat::create(animate,3); // 重复一个动作的次数 CallFuncN* repeatdone=CallFuncN::create(CC_CALLBACK_1(HelloWorld::loadingDone, this)); // 创建回调函数 CC_CALLBACK_1 代表一个参数Sequence* sequence=Sequence::create(repeat, repeatdone, NULL);// 让多个动作按照前后顺序逐一执行 repeatdone 放在 repeat前的话,就不会播放执行3次序列帧的动画loading->runAction(sequence); // 执行上述动画this->setKeypadEnabled(true); // 设置监听Android的按键,如返回键、菜单键、Home键等。    return true;}void HelloWorld::menuCloseCallback(Ref* pSender){    Director::getInstance()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)    exit(0);#endif}void HelloWorld::PreloadMusicAndPicture(){//png加入全局cache中 plist存储了SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/shoot_background.plist");SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/shoot.plist");// 音效CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/background-music1.mp3");CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/bullet.mp3");CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/enemy1_down.mp3");CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/enemy2_down.mp3");CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/enemy3_down.mp3");CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/game_over.mp3");CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/get_bomb.mp3");CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/get_double_laser.mp3");CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/use_bomb.mp3");CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/big_spaceship_flying.mp3");CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/achievement.mp3");CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/out_porp.mp3");CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/button.mp3");// 背景音乐CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/game_music.mp3",true);}void HelloWorld::loadingDone( Node* pNode ){}


路径

预加载的路径是项目路径下的Resources文件夹

这个是我的工程资源路径:E:\studyCocos2dx3.0RC1\PlayThePlane\Resources

如:SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/shoot_background.plist");

其实就是SpriteFrameCache::getInstance()->addSpriteFramesWithFile("E:/studyCocos2dx3.0RC1/PlayThePlane/Resources/ui/shoot_background.plist");


图片加载

我们的图片是用TexturePacker工具把若干图片打包生成的一张总的png和plist,plist保存着png图片中的各个数据,比如名字大小什么的。当然你也可以不用这种整合的,那么加载图片的方式就改变了,比如背景图的加载:

// 背景图(精灵)auto background = Sprite::create("ui/shoot_background/background.png");background->setPosition(Point(winSize.width/2,winSize.height/2)); // 设置位置// 场景中加入背景图this->addChild(background);


音乐加载

预加载中,有一个不是预加载,而是直接加载开启的:
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/game_music.mp3",true);这个是直接把开启了背景音乐。


图片动画效果以及游戏开始的回调

CallFuncN* repeatdone=CallFuncN::create(CC_CALLBACK_1(HelloWorld::loadingDone, this)); // 创建回调函数 CC_CALLBACK_1 代表一个参数Sequence* sequence=Sequence::create(repeat, repeatdone, NULL);// 让多个动作按照前后顺序逐一执行 repeatdone 放在 repeat前的话,就不会播放执行3次序列帧的动画loading->runAction(sequence); // 执行上述动画
Sequence* sequence=Sequence::create(repeat, repeatdone, NULL);我的理解是,Sequence存放动作队列。其中repeat, repeatdone, NULL这个三个动作是顺序执行的,也就是说先执行完repeat动作(小飞机飞三次),然后执行repeatdone,从而触发回调函数loadingDone(),游戏的开始就是在这里哟。

好了,到这里就完成了所谓的游戏开始前的界面。下次说什么我也不知道,写什么,说什么吧。


我看了下我的排版,着实奇怪,有时候行和行间距离很近,有时候很远。而且怎么设置字体啊,我想一开始就是小型字体,而不是每次写完一段,再手动去改。

大家有什么不懂得,可以直接问我(不要问的太深入~),我也是刚开始学cocos2dx,大家一起学习。

0 0
原创粉丝点击