现在的位置: 首页 > 综合 > 正文

cocos2d-x入门代码讲解(本人之前没怎么学过c++,这里做一下笔记)

2013年02月15日 ⁄ 综合 ⁄ 共 3767字 ⁄ 字号 评论关闭

首先是GameScene.h:

#ifndef __GAME_SCENE_H__
#define __GAME_SCENE_H__
#include "cocos2d.h"
 
class SimpleGame : public cocos2d::CCLayerColor
{
public:
    SimpleGame();
 
    ~SimpleGame();
 
    virtual bool init();  
 
    static cocos2d::CCScene* scene();
    //退出游戏菜单回调函数
    virtual void menuCloseCallback(CCObject* pSender);
 
    void addEnemy();
 
    void spriteMoveFinished(cocos2d::CCNode *sender);
 
    void gameLogic(cocos2d::ccTime dt);
 
    LAYER_NODE_FUNC(SimpleGame);
 
};
#endif

现在讲解一下以上代码:

a..h文件,是在c/c++的头文件,先将相关变量和类进行声明。

b.#ifndef XXX    #define XXX  这里是定义一个变量,用于防止该文件被重复编译,因为编译器发现存在该变量时,就不会继续编译该文件了。

c.#include 引入文件

d.classSimpleGame
:
publiccocos2d::CCLayerColor代码的意思是:SimpleGame
类继承CCLayerColor类,而CCLayerColor的命名空间是cocos2d(使用命名空间可以避免命名冲突),而这里的public,是代表继承的权限为public。

e.public:

方法1();

方法2();

........

这里代表对下列的所有方法都为public,同样,private、protected也可以这样使用。

f.SimpleGame(),构造函数,在实例化对象的时候调用。

g.~SimpleGame(),析构函数,在对象销毁时调用。

h.virtual
方法名(),虚函数。

i.staticcocos2d::CCScene*
scene();static ,表示这个方法是静态方法,依赖于类而不依赖于对象,现在这个方法返回CCScene对象的指针。

j.LAYER_NODE_FUNC(SimpleGame);
定义了宏。


接下来看看GameScene.cpp文件。

  代码比较长,直接在代码中加注释了。

#include "GameScene.h"  //引入头文件
 
using namespace cocos2d;   //使用命名空间cocos2d,一般在.h文件中不会这么用,多用于cpp文件
 
SimpleGame::SimpleGame()   //实现构造方法
{
}
 
SimpleGame::~SimpleGame()  //实现析构方法
{
}
 
CCScene* SimpleGame::scene() //实现方法scene()

{
    CCScene * scene = NULL;
    do
    {
        // 'scene' is an autorelease object
        scene = CCScene::node();
        CC_BREAK_IF(! scene);
 
        // 'layer' is an autorelease object
        SimpleGame *layer = SimpleGame::node();
        CC_BREAK_IF(! layer);
 
        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);
 
    // return the scene
    return scene;
}
 
// on "init" you need to initialize your instance
bool SimpleGame::init()
{
    bool bRet = false;
    do
    {
        CC_BREAK_IF(! CCLayerColor::initWithColor(ccc4(255,255,255,255)));
        CCSize size = CCDirector::sharedDirector()->getWinSize();
        //添加一个退出游戏按钮菜单
        CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(SimpleGame::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);
 
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
 
        CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);
 
        // Add the menu to SimpleGame layer as a child layer.
        this->addChild(pMenu, 1);
 
        // 3. Add add a splash screen, show the cocos2d splash image.
        CCSprite* hero = CCSprite::spriteWithFile("Hero.png",CCRectMake(0,0,27,40));
        CC_BREAK_IF(! hero);
 
        //默认值 
        //player->setAnchorPoint(ccp(0.5,0.5));
        // Place the sprite on the center of the screen
        hero->setPosition(ccp(hero->getContentSize().width/2, size.height/2));
 
        // Add the sprite to SimpleGame layer as a child layer.
        this->addChild(hero, 0);   //this,自己这个对象的指针,this->addChild(hero, 0)则代表执行该对象的addChild方法
 
        this->schedule(schedule_selector(SimpleGame::gameLogic),1.0); //同上
 
        bRet = true;
    } while (0);
 
    return bRet;
}
 
void SimpleGame::addEnemy()
{
    CCSprite *enemy = CCSprite::spriteWithFile("Enemy.png",CCRectMake(0,0,27,40));
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    int minY = enemy->getContentSize().height / 2;
    int maxY = winSize.height - enemy->getContentSize().height / 2;
    int rangeY = maxY - minY;
    int actualY = (rand() % rangeY) + minY;
    enemy->setPosition(ccp(winSize.width + enemy->getContentSize().width / 2,actualY));
    this->addChild(enemy);
 
    int minDuration = 2;
    int maxDuration = 4;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = (rand() % rangeDuration) + minDuration;
    CCFiniteTimeAction *actionMove = CCMoveTo::actionWithDuration((ccTime)actualDuration,ccp(0 - enemy->getContentSize().width / 2,actualY));
    CCFiniteTimeAction *actionMoveDone = CCCallFuncN::actionWithTarget(this,callfuncN_selector(SimpleGame::spriteMoveFinished));
    enemy->runAction(CCSequence::actions(actionMove,actionMoveDone,NULL));
}
 
void SimpleGame::spriteMoveFinished(CCNode *sender)
{
    CCSprite *sprite = (CCSprite *)sender;
    this->removeChild(sprite,true);
}
 
void SimpleGame::gameLogic(ccTime dt)
{
    this->addEnemy();
}
 
void SimpleGame::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}

抱歉!评论已关闭.