cocos2dx 基础学习(一)

前端之家收集整理的这篇文章主要介绍了cocos2dx 基础学习(一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

#
hello world项目之后,接着需要自己开发游戏了,涉及到的东西比较多,一些常用的基础知识整理,同时也是对原来学过的知识总结。


vs2010 + cocos2d-2.1beta3-x-2.1.1 项目结构


AppDelegate.cpp的 applicationDidFinishLaunching() 方法

bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director
    CCDirector @H_301_16@*pDirector @H_301_16@= CCDirector::sharedDirector();
    pDirector@H_301_16@->setOpenGLView(CCEGLView::sharedOpenGLView());

    // turn on display FPS
    pDirector@H_301_16@->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector@H_301_16@->setAnimationInterval(1.0 @H_301_16@/ 60);//帧率

    // create a scene. it's an autorelease object
    CCScene @H_301_16@*pScene @H_301_16@= HelloWorld::scene();//初始场景

    // run
    pDirector@H_301_16@->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))); // r g b 透明度

layer中添加label,sprite,button等基本三步骤
创建,设置位置,添加到layer,例如

CCSprite@H_301_16@* player @H_301_16@= CCSprite::create("Player.png");
player@H_301_16@->setPosition(ccp(20,size.height/2));
this@H_301_16@->addChild(player);

menu菜单/菜单
1 创建菜单
2 给菜单添加回调函数,编写回调函数
3 创建菜单添加菜单
4 把菜单添加到layer中

如下一个场景切换的菜单

// 得到屏幕size
CCSize size @H_301_16@= CCDirector::sharedDirector()@H_301_16@->getWinSize();

// 菜单的使用
CCMenuItemImage @H_301_16@*item @H_301_16@= CCMenuItemImage::create(
  "btn_restart.png","btn_restart.png",this,menu_selector(GameOverLayer::overCallback));

item@H_301_16@->setPosition(ccp(0,size.height/2 @H_301_16@- 100));

CCMenu@H_301_16@* pMenu @H_301_16@= CCMenu::create(item,NULL);// 加入菜单

this@H_301_16@->addChild(pMenu);

在头文件添加响应的回调函数,编写回调函数

//重玩
void GameOverLayer::overCallback(CCObject@H_301_16@* pSender)
{
    // CCDirector::sharedDirector()->end();
    CCScene @H_301_16@*helloScene @H_301_16@= HelloWorld::scene();
    CCDirector::sharedDirector()@H_301_16@->replaceScene(helloScene); //切换场景
}

关于切换场景时的传值
1. 首先在创建场景的时候,可以给某个layer设置tag

CCScene@H_301_16@* GameOverLayer::scene()
{
    CCScene @H_301_16@* scene @H_301_16@= NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene @H_301_16@= CCScene::create();
        CC_BREAK_IF(@H_301_16@! scene);

        // 'layer' is an autorelease object
        GameOverLayer @H_301_16@*layer @H_301_16@= GameOverLayer::create();
        layer@H_301_16@->setTag(100); //设置layer的tag

        CC_BREAK_IF(@H_301_16@! layer);

        // add layer as a child to scene
        scene@H_301_16@->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

2 在切换时,
先定义要切换的场景,该场景的getChildByTag()能够得到layer,得到layer在对layer中的控件操作

CCScene @H_301_16@*overScene @H_301_16@= GameOverLayer::scene(); //CCScene -> GameOverLayer ->_label
GameOverLayer @H_301_16@*overLayer @H_301_16@= (GameOverLayer @H_301_16@*)overScene@H_301_16@->getChildByTag(100);
overLayer@H_301_16@->_label@H_301_16@->setString("You Lost!");
CCDirector::sharedDirector()@H_301_16@->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 @H_301_16@*touch @H_301_16@= (CCTouch @H_301_16@*)pTouches@H_301_16@->anyObject();
CCPoint locInView @H_301_16@= touch@H_301_16@->getLocationInView(); // 这里是UI坐标系,需要转换成cocos坐标系
CCPoint loc @H_301_16@= CCDirector::sharedDirector()@H_301_16@->convertToGL(locInView);

给Sprite添加动画,动作

// 创建飞镖
CCSprite @H_301_16@*proj @H_301_16@= CCSprite::create("Projectile.png");
proj@H_301_16@->setPosition(ccp(20,screenSize.height @H_301_16@/ 2.0));
this@H_301_16@->addChild(proj);
// 添加一系列动作
CCMoveTo @H_301_16@*move @H_301_16@= CCMoveTo::create(D @H_301_16@/ 320,ccp(endx,endy));// move对象 速度 移动的终点
CCCallFuncN @H_301_16@*moveFinish @H_301_16@= CCCallFuncN::create(this,callfuncN_selector(HelloWorld::myDefine));
CCSequence @H_301_16@*actions @H_301_16@= CCSequence::create(move,moveFinish,NULL);
proj@H_301_16@->runAction(actions); // 给精灵添加actions

定时器,刷新
init()方法中设置定时器

this@H_301_16@->schedule(schedule_selector(HelloWorld::update)); //每隔刷新周期重写

void HelloWorld::update(float delta) // delta - 1.0 / fps
{
    //TODO
}

碰撞检测
// TODO


参考学习:

原文链接:https://www.f2er.com/cocos2dx/339496.html

猜你在找的Cocos2d-x相关文章