废话不说 直接看注释
<span >AppDelegate.cpp</span>
#include "AppDelegate.h"#include "HelloWorldScene.h"//USING_NS_CC == using namespace cocos2dUSING_NS_CC;//using namespace cocos2d//构造函数AppDelegate::AppDelegate() {}//析构函数AppDelegate::~AppDelegate() {}//程序初始化函数bool AppDelegate::applicationDIDFinishLaunching() { // initialize director //获取设备 auto director = Director::getInstance(); //取得OpenGL窗口 auto glvIEw = director->getopenGLVIEw(); if(!glvIEw) { //如果为空,则创建以" www.sundaboke.com"为窗口标题的窗口。 glvIEw = GLVIEw::create("www.sundaboke.com");//这里我修改过 //刚刚开始是My Game 我修改成www.sundaboke.com cocos2dx 默认的设置的窗口大小是960,640 //设置设备使用的窗口,此句可以去掉。 //director->setopenGLVIEw(glvIEw);这一句我个人医保写在下面 } //设置设备使用的窗口, director->setopenGLVIEw(glvIEw); // turn on display FPS //打开FPS显示 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 //创建HelloWorld场景 auto scene = HelloWorld::createScene(); // run //运行场景 director->runWithScene(scene); return true;}// This function will be called when the app is inactive. When comes a phone call,it's be invoked too//当收到电话时,游戏转入后台服务,响应这句话voID AppDelegate::applicationDIDEnterBackground() { Director::getInstance()->stopAnimation(); //如果使用使用,下面可以用这句代码暂停 // if you use SimpleAudioEngine,it must be pause // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();}// this function will be called when the app is active again//当电话完成,选择恢复游戏是,响应这一句voID AppDelegate::applicationWillEnterForeground() { Director::getInstance()->startAnimation(); // if you use SimpleAudioEngine,it must resume here //如果使用声音,下面可以用这句代码恢复 // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();}
<span ></span><pre name="code" ><span >HelloWorldScene.h</span>
<pre name="code" >#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 //静态函数创建createScene cocos2dx3.x以后都是这个 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函数来创建实例。 CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
#include "HelloWorldScene.h"USING_NS_CC;//使用cocos2dx命名空间 USING_NS_CC == using namespace cocos2d//静态函数创建场景Scene* HelloWorld::createScene(){ // 'scene' is an autorelease object<span > </span>//创建一个Scene auto scene = Scene::create(); // 'layer' is an autorelease object<span > </span>//创建一个 layer auto layer = HelloWorld::create(); // add layer as a child to scene<span > </span>// 把 layer 发到 scene 中 scene->addChild(layer); // return the scene return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){ ////////////////////////////// // 1. super init first<span > </span>//先初始化 if ( !Layer::init() ) { return false; } //获取屏幕分辨率 Size visibleSize = Director::getInstance()->getVisibleSize();<span > </span><span > </span>//获取圆点坐标 Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image,which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object<span > </span>//创建一个按钮,用2张图表示,默认是第一张图片,按下使用第二张,按下的时候调用HelloWorld::menuCloseCallback方法 auto closeItem = MenuItemImage::create( "Closenormal.png","CloseSelected.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this)); //设置按钮的位置<span > </span>closeItem->setposition(Vec2(origin.x + visibleSize.wIDth - closeItem->getContentSize().wIDth/2,origin.y + closeItem->getContentSize().height/2)); // create menu,it's an autorelease object<span > </span>//由菜单项创建按钮. auto menu = Menu::create(closeItem,NulL); menu->setposition(Vec2::ZERO);//设置菜单的位置 this->addChild(menu,1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label //创建一个文本标签, auto label = LabelTTF::create("www.sundaboke.com","Arial",24); // position the label on the center of the screen<span > </span>//设置文本标签的位置 label->setposition(Vec2(origin.x + visibleSize.wIDth/2,origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer this->addChild(label,1);//在屏幕上面显示标签 // add "HelloWorld" splash screen"<span > </span>//创建一个背景 auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen<span > </span>//设置背景的位置 sprite->setposition(Vec2(visibleSize.wIDth/2 + origin.x,visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer<span > </span>//把背景添加到屏幕中 this->addChild(sprite,0); return true;}//响应按钮按下时的事件处理 voID HelloWorld::menuCloseCallback(Ref* pSender){<span > </span>//如果是WP8平台,d出消息框提示一下。#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)<span > </span>MessageBox("You pressed the close button. windows Store Apps do not implement a close button.","Alert"); return;#endif<span > </span>//否则,终止程序。 Director::getInstance()->end();<span > </span>//退出程序#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0);#endif}总结
以上是内存溢出为你收集整理的cocos2dx3.2helloword分析全部内容,希望文章能够帮你解决cocos2dx3.2helloword分析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)