Cocos2d-x_一个简单的Cocos2d-x程序
来源:互联网 发布:tita软件 编辑:程序博客网 时间:2024/05/18 03:41
//// AppDelegate.h//#ifndef _APP_DELEGATE_H_#define _APP_DELEGATE_H_#include "cocos2d.h"class AppDelegate : private cocos2d::CCApplication{public: AppDelegate(); virtual ~AppDelegate(); virtual bool applicationDidFinishLaunching(); virtual void applicationDidEnterBackground(); virtual void applicationWillEnterForeground();};#endif
//// AppDelegate.cpp//#include "AppDelegate.h"#include "HelloWorldScene.h"USING_NS_CC;AppDelegate::AppDelegate() {}AppDelegate::~AppDelegate() {}bool AppDelegate::applicationDidFinishLaunching() { // 初始化导演和OpenGL CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); // 导演拥有OpenGLView pDirector->setOpenGLView(pEGLView); // 是否显示FPS pDirector->setDisplayStats(true); // 设置FPS刷新的频率 pDirector->setAnimationInterval(1.0 / 60); // 创建场景 CCScene *pScene = HelloWorld::scene(); // 导演调用场景开始运行 pDirector->runWithScene(pScene); return true;}// App进入后台void AppDelegate::applicationDidEnterBackground(){ CCDirector::sharedDirector()->stopAnimation(); // 如果你使用简单的声音引擎,必须设置声音暂停 // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();}// App进入前台void AppDelegate::applicationWillEnterForeground(){ CCDirector::sharedDirector()->startAnimation(); // 如果你使用简单的声音引擎,必须设置声音恢复 // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();}
//// HelloWorldScene.h//#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"class HelloWorld : public cocos2d::CCLayer{public: virtual bool init(); static cocos2d::CCScene* scene(); void menuCloseCallback(CCObject* pSender); void closeIconItem(CCObject *pSender); CREATE_FUNC(HelloWorld);};#endif
//// HelloWorldScene.cpp//#include "HelloWorldScene.h"USING_NS_CC;CCScene* HelloWorld::scene(){ // 初始化场景 CCScene *scene = CCScene::create(); // 初始化层 HelloWorld *layer = HelloWorld::create(); layer->setTouchEnabled(true); // 把层添加到场景 scene->addChild(layer); // 返回场景 return scene;}// 在“init”中初始化你的对象bool HelloWorld::init(){ // 首先初始化基类 if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); ///////////////////////////// // 创建一个退出按钮,为菜单项 CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback));pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); // 创建一个菜单,把菜单项添加进去 CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); ///////////////////////////// // 创建一个标签 CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); // 设置标签的位置 pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); // 添加到层中 this->addChild(pLabel, 1); ///////////////////////////// // 创建精灵,也就是背景图片 CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // 设置精灵位置 pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // 添加到层中 //this->addChild(pSprite, 0); ///////////////////////////// // 创建自己的精灵 CCMenuItemImage *pIconItem = CCMenuItemImage::create("111.png", "222.png", this, menu_selector(HelloWorld::closeIconItem)); pIconItem->setPosition(ccp(visibleSize.width/2.0, visibleSize.height/2.0)); // 创建一个菜单,把菜单项添加进去 CCMenu *pMenuIcon = CCMenu::create(pIconItem,NULL); pMenuIcon->setPosition(CCPointZero); this->addChild(pMenuIcon); return true;}void HelloWorld::closeIconItem(CCObject *pSender){ CCLog("按钮回调函数!");}// 按钮回调函数void HelloWorld::menuCloseCallback(CCObject* pSender){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");#else// CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)// exit(0);#endif#endif}
//// IconLayer.h//#ifndef HelloWorld_IconLayer_h#define HelloWorld_IconLayer_h#include "cocos2d.h"class IconLayer : private cocos2d::CCLayer{public: //virtual bool init(); static cocos2d::CCScene* scene(); };#endif
//// IconLayer.cpp//#include "IconLayer.h"USING_NS_CC;cocos2d::CCScene * IconLayer::scene(){ CCScene *scene = CCScene::create(); cocos2d::CCLayer *layer = IconLayer::create(); scene->addChild(layer); return scene;}//bool IconLayer::init()//{////}
0 0
- Cocos2d-x_一个简单的Cocos2d-x程序
- cocos2d-x 一个简单的游戏界面
- Cocos2d-X开发一个简单的小游戏
- Cocos2d-3.x_重力加速器的使用
- Cocos2d-x_单点触摸
- Cocos2d-x_多点触摸
- Cocos2d-x_粒子特效
- Cocos2d-3.x_定时器
- cocos2d-x简单的runAction
- cocos2d-x简单的绘制
- Cocos2d-x简单的Demo
- cocos2d-x俄罗斯方块--基于简单工厂的程序重构
- Cocos2d-X的一个示例
- cocos2d-x入门(2)-实现一个简单的游戏SimpleGame
- 如何制作一个简单的游戏 Cocos2d-x 2.0.4
- Cocos2d-x 2.0.4 如何制作一个简单的游戏
- Cocos2d-x 2.0.4 如何制作一个简单的游戏
- 如何制作一个简单的游戏 Cocos2d-x 2.1.2
- Add two numbers & Add binary
- HLJU 1188 Matrix (二维树状数组)
- 产品设计必读书籍推荐
- ios 编译openssl支持arm64
- 使用git提取Minix_R3.2.1源码
- Cocos2d-x_一个简单的Cocos2d-x程序
- Apache二级域名实现
- 第二届战神杯线上编程挑战赛月赛第二题:数字游戏(Java)
- Android 从相册获取最近拍摄的多张照片(获取相机拍照所存储的照片)
- Ubuntu下SSH的安装
- Android中Preference的使用以及监听事件分析
- 如何在网站开发中使用LINQ操作数据库
- window下安装Oracle11G安装
- Python 之 __new__() 方法与实例化