cosos2d-x 环境搭建和初学习
来源:互联网 发布:js中的保留字有哪些 编辑:程序博客网 时间:2024/04/29 15:51
cocos2d-x的版本太多,而最新的cocos2d-x基本上没有太多的图书,视频教程可以参考,所以选择了以 《Cocos2d-x游戏开发之旅》这本书作为个人学习的教材。(其实,新版本功能可能很好,只不过开发工具与文档似乎不太友好,各种坑,所以决定回到原始版本,先学基础、再研究新版本)
首先需要搭建环境,我弄了半天,终于成功搭建了
win7 + vs2010 + cocos2d-2.1beta3-x-2.1.1的开发环境
软件下载在我分享的网盘里:http://pan.baidu.com/s/1pKE67zH
具体的cocos2d-x安装和vs2010搭建环境参考如下网址:
http://www.xuebuyuan.com/866555.html
这其中可能会出现各种问题,注意win7打上各种补丁,测试下vs的使用
基本没有问题。
参考《Cocos2d-x游戏开发之旅》的前三章,基本上可以写出像样的cocos2d-x程序。
如下新建一个cocos2d-x项目,并新建了自己Scene
基本上代码都仿照着例子写
MyHelloWorldScene.h
#ifndef __MYHELLOWORLD_SCENE_H__#define __MYHELLOWORLD_SCENE_H__#include "cocos2d.h"#include "Box2D/Box2D.h"#include "SimpleAudioEngine.h"using namespace cocos2d;class MyHelloWorldScene : public cocos2d::CCLayer{public: // 通过静态的ccene函数,创建一个场景对象 static CCScene* scene(); // MyHelloWorldScene的初始化工作在init里执行 virtual bool init(); // 调用宏CREATE_FUNC定义create函数 CREATE_FUNC(MyHelloWorldScene);};#endif // __MYHELLOWORLD_SCENE_H__
MyHelloWorldScene.cpp
#include "MyHelloWorldScene.h"CCScene* MyHelloWorldScene::scene(){ CCScene * scene = NULL; do { scene = CCScene::create(); CC_BREAK_IF(! scene); // 创建MyHelloWorldScene对象 MyHelloWorldScene *layer = MyHelloWorldScene::create(); CC_BREAK_IF(! layer); // 添加MyHelloWorldScene到场景中 scene->addChild(layer); } while (0); return scene;}bool MyHelloWorldScene::init(){ // 在场景中添加一个精灵 CCSprite* pSprite = CCSprite::create("CloseNormal.png"); pSprite->setPosition(ccp(200, 200)); this->addChild(pSprite, 0); // 在场景中添加一个label CCLabelTTF *label = CCLabelTTF::create("hello welcome", "Arial", 40); label->setPosition(ccp(200, 300)); this->addChild(label, 0); return true;}
其中HelloWorldScene有个回调函数,可以用来改写,稍作改动,用来切换场景,代码如下
void HelloWorld::menuCloseCallback(CCObject* pSender){ // "close" menu item clicked // CCDirector::sharedDirector()->end(); // 普通的场景切换 // CCDirector::sharedDirector()->replaceScene(MyHelloWorldScene::scene()); // 带滑动效果的场景切换 CCDirector::sharedDirector()->replaceScene( CCTransitionSlideInT::create(3.0f,MyHelloWorldScene::scene()));}
当然,仿照着可以自己写一个menu菜单,定义自己的回调函数
部分代码如下
// 创建一个菜单项,类型为图片菜单类型 CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", // 正常状态下的图片 "CloseSelected.png", // 单击状态下的图片 this, menu_selector(HelloWorld::menuCloseCallback)); //单击时的回调函数 CC_BREAK_IF(! pCloseItem); // Place the menu item bottom-right conner. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)); // 自定义一个Label Menu CCLabelTTF* pLabeltmp = CCLabelTTF::create("Label Item:close", "Arial", 24); //CCMenuItemLabel *pLabelItem = CCMenuItemLabel::create(pLabeltmp); CCMenuItemLabel *pLabelItem = CCMenuItemLabel::create(pLabeltmp, this, menu_selector(HelloWorld::menuLabelCallback)); pLabelItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 200, 60)); // Create a menu with the "close" menu item, it's an auto release object. // 创建菜单,用来存放各种菜单项 //CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); CCMenu* pMenu = CCMenu::create(pCloseItem,pLabelItem, NULL); pMenu->setPosition(CCPointZero); CC_BREAK_IF(! pMenu);
void HelloWorld::menuLabelCallback(CCObject* pSender){ // "close" menu item clicked CCDirector::sharedDirector()->end();}
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "Box2D/Box2D.h"#include "SimpleAudioEngine.h"class HelloWorld : public cocos2d::CCLayer{public: // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // there's no 'id' in cpp, so we recommand to return the exactly class pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); // 自定义的Label Menu 点击的回调 void menuLabelCallback(CCObject* pSender); // implement the "static node()" method manually CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__
运行截图
采用了滑动的切换效果
切换之中
切换后
0 0
- cosos2d-x 环境搭建和初学习
- APUE初学 环境搭建
- 初学Java 环境搭建
- 初学Android_0_Android环境搭建
- appium初学环境搭建
- Cocos2d-x 环境搭建和项目创建
- 初学stm32-环境搭建——stm32和IAR配置
- 初学stm32-环境搭建——stm32和IAR配置
- Cosos2d-x创建新的HelloWorld工程 Mac
- EJB初学之环境搭建
- 初学mongodb笔记-搭建环境
- 初学PHP 之环境搭建
- 初学Hibernate之环境搭建
- 初学Spring之环境搭建
- 初学php环境搭建集锦
- 初学PX4之环境搭建
- JAVA 初学习____搭建JAVA开发环境
- struts1.X 环境搭建
- 程序员的自我修养读书笔记
- 第二章:probability distribution exericse 35-44
- leetcode 345 Reverse Vowels of a String
- 字段返回null值,却需要进行运算怎么处理?
- C程序内存分配
- cosos2d-x 环境搭建和初学习
- 贩妖记 > 第五十四章,大天机
- MarqueeView源码简单分析笔记
- Android 5.0以后启动服务的正确做法
- C++的头文件和实现文件分别写什么
- 如何使用GCC编译器
- 启动Eclipse 报错:java was started but returned exit code = 1
- 高级语言采用左连接统计总和
- 使用GeoGebra绘制简单函数图像