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,运行程序,效果如图,可以看到一个黑衣忍者站在屏幕最左端,一群黑怪从屏幕右端飞奔而来:

cocos2d-x手游性能优化总结

近段时间在使用cocos2d-x开发2D手游,技术方案使用的是cocos2d-x+lua,因为游戏使用的是cocos2d-x 2.1.5版本,有些优化方案在最新版的cocos2d-x版本已经实现...

阅读全文

cocos2dx-html5 实现网页版flappy bird游戏

我也是第一次使用cocos2d_html5,对js和html5也不熟,看引擎自带的例子和引擎源码,边学边做,如果使用过cocos2d-x的话,完成这个游戏还是十分简单的。游戏体...

阅读全文

【cocos2d-x开发实战 特训99-终结篇】移植到android平台和添加admob广告

上一篇已经完成特性99在win32平台下的开发,现在把它移植到android上,首先修改Android.mk文件,内容如下: LOCAL_PATH := $(call my-dir) include $(CLEAR_...

阅读全文

1 条评论

  1. 写得不错,最近我也在研究cocos2d-x,有空可以多交流一下,Q加好友。

欢迎留言