cocos2dx 基础知识
来源:互联网 发布:科比0506赛季数据 编辑:程序博客网 时间:2024/04/30 22:37
#
hello world项目之后,接着需要自己开发游戏了,涉及到的东西比较多,一些常用的基础知识整理,同时也是对原来学过的知识总结。
vs2010 + cocos2d-2.1beta3-x-2.1.1 项目结构
AppDelegate.cpp的 applicationDidFinishLaunching() 方法
bool AppDelegate::applicationDidFinishLaunching(){ // initialize director CCDirector *pDirector = CCDirector::sharedDirector(); pDirector->setOpenGLView(CCEGLView::sharedOpenGLView()); // turn on display FPS pDirector->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this pDirector->setAnimationInterval(1.0 / 60);//帧率 // create a scene. it's an autorelease object CCScene *pScene = HelloWorld::scene();//初始场景 // run pDirector->runWithScene(pScene); return true;}
获得导演(单例),设置了opengl、帧率,设置了初始的启动场景
HelloWorldScene.h 的一些成员
public: cocos2d::CCSprite* target ; void myDefine(CCNode* who); void createTarget(); void gameLogic(float dt); // CClayer touch事件 void ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent); // 定义飞镖 和 怪物的集合 // array 插入 删除效率低, 查找的效率高 // list 高 低 // 添加 : 飞镖 怪物出现 // 删除 : 碰撞 // 遍历 : 每隔fps事件 就要遍历检查(多,所以选择array) cocos2d::CCArray *_targets; cocos2d::CCArray *_projs; ~HelloWorld(); void update(float delta); // delta - 1.0 / fps int _successCount; // 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); // 响应函数 void responseFunc(CCObject* pSender); // implement the "static node()" method manually CREATE_FUNC(HelloWorld);
director – scene – layer – sprite
class HelloWorld : public cocos2d::CCLayerColor // CCLayer的一个子类 ,可以设置颜色
在HelloWorld::init() 中可以初始化如下
// 初始化颜色CC_BREAK_IF(! CCLayerColor::initWithColor(ccc4(255, 255, 255, 255))); // r g b 透明度
layer中添加label , sprite , button等基本三步骤
创建,设置位置,添加到layer, 例如
CCSprite* player = CCSprite::create("Player.png");player->setPosition(ccp(20, size.height/2));this->addChild(player);
menu菜单/菜单项
1 创建菜单项
2 给菜单项添加回调函数,编写回调函数
3 创建菜单时添加菜单项
4 把菜单添加到layer中
如下一个场景切换的菜单项
// 得到屏幕sizeCCSize size = CCDirector::sharedDirector()->getWinSize();// 菜单的使用CCMenuItemImage *item = CCMenuItemImage::create( "btn_restart.png", "btn_restart.png", this, menu_selector(GameOverLayer::overCallback));item->setPosition(ccp(0, size.height/2 - 100));CCMenu* pMenu = CCMenu::create(item, NULL);// 加入菜单this->addChild(pMenu);
在头文件添加响应的回调函数,编写回调函数
//重玩void GameOverLayer::overCallback(CCObject* pSender){ // CCDirector::sharedDirector()->end(); CCScene *helloScene = HelloWorld::scene(); CCDirector::sharedDirector()->replaceScene(helloScene); //切换场景}
关于切换场景时的传值
1. 首先在创建场景的时候,可以给某个layer设置tag
CCScene* GameOverLayer::scene(){ CCScene * scene = NULL; do { // 'scene' is an autorelease object scene = CCScene::create(); CC_BREAK_IF(! scene); // 'layer' is an autorelease object GameOverLayer *layer = GameOverLayer::create(); layer->setTag(100); //设置layer的tag CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene;}
2 在切换时,
先定义要切换的场景,该场景的getChildByTag()能够得到layer,得到layer在对layer中的控件操作
CCScene *overScene = GameOverLayer::scene(); //CCScene -> GameOverLayer ->_labelGameOverLayer *overLayer = (GameOverLayer *)overScene->getChildByTag(100);overLayer->_label->setString("You Lost!");CCDirector::sharedDirector()->replaceScene(overScene); //切换场景
layer的touch事件
// 打开CCLayer的touch事件
this->setTouchEnabled(true);
// 重写各个touch事件
bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);
touch事件可以得到点击的position,判断点击的目标等
CCPoint touchPos = pTouch->getLocation();
CCTouch *touch = (CCTouch *)pTouches->anyObject();CCPoint locInView = touch->getLocationInView(); // 这里是UI坐标系,需要转换成cocos坐标系CCPoint loc = CCDirector::sharedDirector()->convertToGL(locInView);
给Sprite添加动画,动作
// 创建飞镖CCSprite *proj = CCSprite::create("Projectile.png");proj->setPosition(ccp(20, screenSize.height / 2.0));this->addChild(proj);// 添加一系列动作CCMoveTo *move = CCMoveTo::create(D / 320, ccp(endx,endy));// move对象 速度 移动的终点CCCallFuncN *moveFinish = CCCallFuncN::create(this,callfuncN_selector(HelloWorld::myDefine));CCSequence *actions = CCSequence::create(move, moveFinish, NULL);proj->runAction(actions); // 给精灵添加actions
定时器,刷新
init()方法中设置定时器
this->schedule(schedule_selector(HelloWorld::update)); //每隔刷新周期重写void HelloWorld::update(float delta) // delta - 1.0 / fps{ //TODO}
碰撞检测
// TODO
参考学习:
- http://blog.csdn.net/s_xing/article/details/18557631
相应的视频教程讲解需要好好学习
- cocos2dx 基础知识
- cocos2dx基础知识 网址 记录
- cocos2dx 基础知识体系
- 161112 笔记---cocos2dx之libcurl基础知识 + 热更新基础
- cocos2dx
- cocos2dx
- cocos2dx
- cocos2dx
- cocos2dx
- cocos2dx
- Cocos2dx
- cocos2dx
- cocos2dx
- cocos2dx
- cocos2dx
- cocos2dx
- cocos2dx
- Cocos2dx
- jquery onready的问题
- [Array]Two Sum
- poj 2892 Tunnel Warfare(树状数组+二分)
- JAVA设计模式之单例模式
- jquery mobile.js文件与jquery.js的插入顺序
- cocos2dx 基础知识
- c++关闭io同步流
- 博客自荐(十一期 2016年07月)
- 手把手教你用python写脚本看caffe训练好的网络测试单张图片的输出
- java中哈希表,Map,HashCode,Equals等介绍
- CODEVS 1134 noip2011 铺地毯
- HBase(0.96以上版本)过滤器Filter详解及实例代码
- innerTHML的问题
- for循环排序