
我是在mac环境下,使用的是xcode写的,直接创建一个新的项目 ,然后,就包含了一些基本的文件了。直接开始创建就可以了。
第一步: 先创建头文件://// GameScene.h// study_01//// Created by Carlos on 15/6/3.////#ifndef study_01_GameScene_h#define study_01_GameScene_h//防止重复包含头文件#include "cocos2d.h"//新建一个类,公共继承于cocos2d里面的Layer类class GameScene:public cocos2d::Layer{//声明一些方法private: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(); // implement the "static create()" method manually CREATE_FUNC(GameScene);};#endif 如上面所示,我都做了一些注释,头文件里面,因为就是一些声明。主要的过程,都在cpp文件里面。而这些方法,都是默认的例子里面有的, 第二点: 把自己的一些资源文件,放在resource目录下面,因为我是第一次使用xcode,对于这个,还真是不熟,我直接使用finder,往resource目录里面,拖了两张图片,以为就可以了,没想到不行,最后写完了,运行的时候,一直报错。原来,直接使用finder拖拽,xcode还不会自动刷新。我的解决办法是,直接把图片拖到xoode的项目目录中,这样就可以了。
第三步: 编写自己的cpp文件,这里面,我做了尽量详细地注释,应该不难理解。
//// GameScene.cpp// study_01//// Created by carlos on 15/6/3.////#include "GameScene.h"#include "cocos2d.h"USING_NS_CC;Scene* GameScene::createScene(){ auto scene = Scene::create(); auto layer = GameScene::create(); scene->addChild(layer); return scene;}bool GameScene::init(){ //如果父类没有初使化成功,就返回false if (!Layer::init()) { return false; } //在这里,我们往场景里面添加两张图片,需要先图片添加到我们的Resources文件夹 //先获取窗口的尺寸 Size size = Director::getInstance()->getVisibleSize(); //这里先创建背景bg,里面的参数呢,就是图片的名字 auto bg = Sprite::create("bg.jpg"); //设置背景图片的位置 bg->setposition(size.wIDth/2,size.height/2); //获取缩放比例 float sx = size.wIDth/bg->getContentSize().wIDth; float sy = size.height/bg->getContentSize().height; //根据前面计算的比例,设置让背景全屏 bg->setScale(sx,sy); //将背景添加到这个里面,第一个参数表示要添加的场景,第二个参数是Z轴的坐标,如果你再添加一个场景,Z轴为1,那它就会在0的上面 this->addChild(bg,0); //再创建第二个场景 auto button = Sprite::create("button.png"); button->setposition(size.wIDth/2,size.height/2); this->addChild(button,1); return true; } 这两个创建好了以后,需要把代理类里面的场景,换成我们自己的。因为新建的项目,默认的场景是HellowWorldScene,我们替换成我们的,然后,运行,就可以看到效果,如下面所示,是AppDelegate.cpp中的一个方法:
bool AppDelegate::applicationDIDFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glvIEw = director->getopenGLVIEw(); if(!glvIEw) { glvIEw = GLVIEwImpl::create("My Game"); director->setopenGLVIEw(glvIEw); } // turn on display FPS director->setdisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 / 60); register_all_packages(); // create a scene. it's an autorelease object <span > </span>//就是在这里替换的,GameScene就是我自己创建的一个场景// auto scene = HelloWorld::createScene(); auto scene = GameScene::createScene(); // run director->runWithScene(scene); return true;}总结
以上是内存溢出为你收集整理的第一节:创建自己的每一个场景全部内容,希望文章能够帮你解决第一节:创建自己的每一个场景所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)