cocos2d-x 3.x HelloWorld入门
2014-11-19 20:25
162 查看
先创建HelloWorld项目,命令行下输入cocos new –p com.game.simple –l cpp –d F:\simple Game,引擎会为我们自动生成一个名为Game的项目,进入proj.win32文件夹用vs2012打开Game.sln,直接运行。第一次编译会需要较长时间,看到cocos2d-x HelloWorld就是运行成功了。
项目中还有其他的文件夹,其中Resources主要用于存放游戏中需要的图片、音频和配置等资源文件,为了方便管理,可以在其中创建子文件夹。
proj.win32文件夹中的main.h,main.cpp,resource.h是Windows平台相关的程序文件
Classes文件夹为HelloWorld项目的主要内容,包括AppDelegate.h,AppDelegate.cpp,HelloWorldScene.h,HelloWorldScene.cpp
AppDelegate.h和AppDelegate.cpp是cocos2d-x游戏的通用入口文件,包含三个主要的函数
HelloWorldScene定义了一个HelloWorld类,该类继承自Layer,因此HelloWorld本身是一个层,这里也就是定义了一个HelloWorld层。
项目中还有其他的文件夹,其中Resources主要用于存放游戏中需要的图片、音频和配置等资源文件,为了方便管理,可以在其中创建子文件夹。
proj.win32文件夹中的main.h,main.cpp,resource.h是Windows平台相关的程序文件
Classes文件夹为HelloWorld项目的主要内容,包括AppDelegate.h,AppDelegate.cpp,HelloWorldScene.h,HelloWorldScene.cpp
AppDelegate.h和AppDelegate.cpp是cocos2d-x游戏的通用入口文件,包含三个主要的函数
virtual bool applicationDidFinishLaunching(); // 应用程序启动后将调用这个方法,默认的实现中已经包含了游戏启动后的必要准备 virtual void applicationDidEnterBackground(); // 当应用程序进入后台时,会调用这个方法 virtual void applicationWillEnterForeground(); // 在应用程序回到前台时被调用 bool AppDelegate::applicationDidFinishLaunching() { // 初始化导演类 auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { glview = GLView::create("My Game"); // 可以在这里改变GL窗口的大小来调试 // glview = GLView::createWithRect("Game", Rect(0, 0, 640, 960)); director->setOpenGLView(glview); } /* 开启FPS显示(FPS即每秒帧速率,也就是屏幕每秒重绘的次数) 启用FPS显示后,当前FPS会在游戏的左下角显示,通常在开发阶段启用 */ director->setDisplayStats(true); // 设置绘制间隔(两次绘制的时间间隔),默认间隔为1/60秒,约等于人眼的刷新频率 director->setAnimationInterval(1.0 / 60); // 创建HelloWorld场景,需要在此处对游戏进行初始化 auto scene = HelloWorld::createScene(); // 导演运行这个场景 director->runWithScene(scene); return true; }HelloWorldScene.h和HelloWorldScene.cpp这两个文件定义了HelloWorld项目中默认的场景
HelloWorldScene定义了一个HelloWorld类,该类继承自Layer,因此HelloWorld本身是一个层,这里也就是定义了一个HelloWorld层。
// 在层下创建场景,这是一个静态函数 Scene* HelloWorld::createScene() { // 创建一个空场景 auto scene = Scene::create(); // 创建HelloWorld层的实例 auto layer = HelloWorld::create(); /* 把层置入场景中,addChild可以把一个游戏元素放置到另一个元素之中,只有把它放置到已经呈现出来的游戏元素中,它才会呈现出来,我们把HelloWorld放入空场景中,在 AppDelegate.cpp中,我们已经让导演运行了该场景,因此HelloWorld层会显示在屏幕上 */ scene->addChild(layer); return scene; } // 初始化HelloWorld类 bool HelloWorld::init() { ////////////////////////////// // 1. 首先对父类进行初始化 if ( !Layer::init() ) { return false; } // 返回以点为单位的 OpenGL 视图的可见大小(可视区域大小),如果不调用EGLView::setDesignResolutionSize()其值等于getWinSize() Size visibleSize = Director::getInstance()->getVisibleSize(); // 返回可视区域的起点坐标 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 // cocos2d-x中菜单由两部分组成,分别是菜单项和菜单本身,MenuItem表示一个菜单项,每个菜单项都是一个独立的按钮;Menu则是菜单,负责将菜单项组织到一起并添加到场景中,Menu可以看成是管理所有菜单项的一个类 auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); /* setPosition()用于设置节点(Node)的位置,Position指的是锚点(AnchorPoint)在父节点中的坐标值,锚点的两个参量x和y的取值通常都是0到1之间的实数,表示锚点相对于节点长宽的位置。如果把节点的左下角作为锚点,其值为(0,0);把节点的中心作为锚点,值为(0.5,0.5);把节点右下角作为锚点,值为(1,0)。精灵的锚点默认值为(0.5,0.5),也就是图片中心的地方,其他节点默认值为(0,0)。setPosition()也就是把该节点的锚点放到父节点中的哪个位置。 如果想把一个精灵放到其父节点的正中心,也就是要把该精灵的锚点放到正中心(因为精灵的锚点默认为(0.5,0.5),是图片的中心),就应该这样写sprite->setPosition(Vec2(父节点宽度/2, 父节点高度/2)); 对于场景或层等大型节点,它们的_ignoreAnchorPointForPosition属性为true,此时引擎在计算它的坐标时会忽略锚点属性的影响,即认为锚点永远为(0,0);而其他节点该属性为false,计算坐标时它们的锚点不会被忽略。需要注意的是当设置_ignoreAnchorPointForPosition属性为true时,只有计算节点坐标时才会忽略锚点属性,在计算其他属性时则不会忽略,例如即时设置_ignoreAnchorPointForPosition属性为true,节点旋转时依然会以锚点为中心,而不是左下角(0,0)的位置。 */ // getContentSize()是获取节点的内容大小,对于精灵来说,ContentSize是它的纹理显示部分的大小,对于层或场景等全屏的大型节点来说,ContentSize则是屏幕大小 closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); // void Node::addChild(Node *child, int zOrder);zOrder指的是child的z轴顺序,也就是显示的先后顺序,其值越大,显示位置越靠前 ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto label = LabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen 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" auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(sprite, 0); return true; }
相关文章推荐
- 【Cocos2d入门教程一】Cocos2d-x环境搭建
- 【Cocos2d入门教程五】Cocos2d-x动作篇
- 【Cocos2d入门教程六】Cocos2d-x事件篇之触摸
- Cocos2d-x学习入门之HelloWorld程序
- Cocos2d-HTML5入门第一天
- cocos2d-x入门——场景显示分析
- Cocos2d-JS 快速入门二 HelloWord
- Cocos2d入门--1-- 初涉相关属性或代码
- Cocos2d-x入门实战(三) cocos2d-x游戏是怎么跑起来的
- (译)cocos2d-x跨android&ios平台开发入门教程
- Cocos2d-x 3.1.1 学习日志1--安装和创建项目(入门必看)
- IOS环境搭建与开发入门-------------------cocos2d-x 3.0正式版本(7.8)
- 【cocos2d-x入门实战】微信飞机大战之十二:分数的本地存储
- [置顶] 【cocos2d-x入门实战】微信飞机大战之十二:分数的本地存储
- Cocos2d入门 <二> 添加一个精灵
- Cocos2d-x 入门调研总结
- cocos2d-x游戏实例 《简单棋》入门尝试(三) 初始化棋子布局信息
- cocos2d-x入门笔记(二)
- cocos2d-x 入门游戏:许特尔图_3 of 4
- Cocos2d-x3.10入门(创建项目)