cocos2d-x入门(3)-添加一个英雄和一群移动的敌人

1. 首先创建一个Cocos2d-win32 Application,名为SimpleGame,这里不需要物理引擎,所以去掉Box2D选项。具体方法在 (win7下vs2010+cocos2d-x配置图文详解) 这篇文章里已经介绍了。
2. 创建一个GameScene.h头文件和对应的源文件GameScene.cpp
代码如下:
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
SimpleGame继承了CCLayerColor,这样就可以指定游戏的背景颜色。LAYER_NODE_FUNC这个宏在引擎内部(CCLayer.h)被定义。主要是为自己建的层增加一个node()函数,node()函数的作用是new一个实例,并执行实例的init方法(所以必须得定义init方法),将它加入autorelease,也就是所有由node()函数得来的指针,都不需要手动的释放(除非自己调用了retain())。源码如下
#define LAYER_NODE_FUNC(layer) \
static layer* node() \
{ \
layer *pRet = new layer(); \
if (pRet && pRet->init()) \
{ \
pRet->autorelease(); \
return pRet; \
} \
else \
{ \
delete pRet; \
pRet = NULL; \
return NULL; \
} \
};

GameScene.cpp
#include "GameScene.h"
 
using namespace cocos2d;
 
SimpleGame::SimpleGame()
{
}
 
SimpleGame::~SimpleGame()
{
}
 
CCScene* SimpleGame::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->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();
}

init函数中实现了以下功能:添加了一个退出游戏的菜单,一个英雄精灵,启动了一个任务调度每隔1秒添加一个敌人。addEnemy函数是用来添加敌人精灵的,给敌人指定了一个移动动作,精灵运行到最左端外部的位置后调用spriteMoveFinished函数,从该层中清除超出屏幕的精灵。rand()是C标准库函数,rand()%100表示随机从0到99返回一个数。注意:schedule(schedule_selector(SimpleGame::gameLogic),1.0)表示每隔1.0s调用一次指定的回调函数,查看cocos2d-x源码可以看到:
typedef void (CCObject::*SEL_SCHEDULE)(ccTime);
#define schedule_selector(_SELECTOR) (SEL_SCHEDULE)(&_SELECTOR)

回调函数gameLogic的格式必须为gameLogic(ccTime dt)

最后运行之前别忘了修改AppDelegate.cpp,把HelloWorldScene改成自己定义的SimpleGame。

CCScene *pScene = SimpleGame::scene();

ctrl+f5,运行程序,效果如图,可以看到一个黑衣忍者站在屏幕最左端,一群黑怪从屏幕右端飞奔而来:




转载请注明来自:Alex Zhou,本文链接:http://codingnow.cn/cocos2d-x/680.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值