飞机游戏

来源:互联网 发布:c专家编程 在线阅读 编辑:程序博客网 时间:2024/04/29 12:01

HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"class HelloWorld : public cocos2d::Layer{public:    // there's no 'id' in cpp, so we recommend returning the class instance pointer    static cocos2d::Scene* createScene();    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone    virtual bool init();    // a selector callback    void menuCloseCallback(cocos2d::Ref* pSender);    //图片按钮事件函数声明    void menuCallback2(cocos2d::Ref* pSender);    // implement the "static create()" method manually    CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp

#include "HelloWorldScene.h"#include "SimpleAudioEngine.h"//声音引擎头文件#include "GameScene.h"USING_NS_CC;using namespace CocosDenshion;//要使用声音引擎,必须使用命名空间CocosDenshionScene* HelloWorld::createScene(){    // 'scene' is an autorelease object    auto scene = Scene::create();    // 'layer' is an autorelease object    auto layer = HelloWorld::create();    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }    Size visibleSize = Director::getInstance()->getVisibleSize();    Vec2 origin = Director::getInstance()->getVisibleOrigin();    /*背景音乐*/    SimpleAudioEngine::getInstance()->playBackgroundMusic("MainUI.mp3", true);    /*添加背景图片*/    auto sprite = Sprite::create("MainBG.png", Rect(0, 0, 480, 800));    this->addChild(sprite);    /*设置背景精灵锚点*/    sprite->setAnchorPoint(Vec2(0, 0));    /*文字按钮*/    auto item1 = MenuItemFont::create("play", [&](Ref *sender) {        auto scene = GameScene::createScene();        Director::getInstance()->replaceScene(TransitionFlipX::create(2, scene));//跳转场景    });    /*图片按钮*/    auto item2 = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCallback2, this));    /*添加按钮*/    auto menu = Menu::create(item1,item2, nullptr);    menu->alignItemsVertically();    /*添加到舞台*/    this->addChild(menu);    return true;}void HelloWorld::menuCallback2(Ref* pSender){    Director::getInstance()->end();}void HelloWorld::menuCloseCallback(Ref* pSender){    Director::getInstance()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)    exit(0);#endif}

然后创建一个游戏场景GameScene
复制一下HelloWorld场景的头文件
GameScene.h

#ifndef __GAME_SCENE_H__#define __GAME_SCENE_H__#include "cocos2d.h"class GameScene : public cocos2d::Layer{public:    // there's no 'id' in cpp, so we recommend returning the class instance pointer    static cocos2d::Scene* createScene();    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone    virtual bool init();    // a selector callback    void menuCloseCallback(cocos2d::Ref* pSender);    // implement the "static create()" method manually    CREATE_FUNC(GameScene);};#endif // __GAME_SCENE_H__

GameScene.cpp

#include "GameScene.h"#include "SimpleAudioEngine.h"//音乐USING_NS_CC;using namespace CocosDenshion;//音乐Scene* GameScene::createScene(){    // 'scene' is an autorelease object    auto scene = Scene::create();    // 'layer' is an autorelease object    auto layer = GameScene::create();    // add layer as a child to scene    scene->addChild(layer);    /*auto s = Director::getInstance()->getWinSize();    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("grossini.plist", "grossini.png");*/    // return the scene    return scene;}// on "init" you need to initialize your instancebool GameScene::init(){    //////////////////////////////    // 1. super init first    if (!Layer::init())    {        return false;    }    Size visibleSize = Director::getInstance()->getVisibleSize();    Vec2 origin = Director::getInstance()->getVisibleOrigin();    auto cache = SpriteFrameCache::getInstance();    cache->addSpriteFramesWithFile("grossini.plist", "grossini.png");    auto sp = Sprite::createWithSpriteFrameName("grossini_dance_01.png");    this->addChild(sp);     auto s = Director::getInstance()->getWinSize();    sp->setPosition(Vec2(s.width / 2 , s.height / 2) );    return true;}
0 0
原创粉丝点击