飞机游戏
来源:互联网 发布: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
- 飞机游戏
- 飞机游戏
- 飞机游戏
- 飞机游戏
- 《飞机游戏》
- 飞机游戏
- 飞机游戏
- 飞机游戏
- Java版飞机游戏
- 开发飞机游戏总结
- C#打飞机游戏
- 飞机游戏策划
- 小型安卓打飞机游戏
- 小型安卓打飞机游戏
- 飞机游戏2
- 飞机游戏的制作
- 飞机游戏初步
- 飞机游戏(1)
- Android 基本控件
- 启动挂载
- [AHK]为通达信增加打开关闭交易窗口的功能热键
- HTML5 Canvas
- Xcode 代码提示功能失效
- 飞机游戏
- Nginx安装及常用配置
- 【ie8bug】img设置max-width:100%,外层浮动,图片消失
- 简单批处理执行SQL语句
- 寻梦
- 关于position:fixed
- css选择器与JQ选择器的性能比对与优化方案
- lamada
- Android annotations 框架