制作飞镖忍者(1) Cocos2d-x 3.0alpha0
来源:未知 责任编辑:责任编辑 发表时间:2013-12-06 08:43 点击:次
本文实践自 Ray Wenderlich 的文章《How
To Make A Simple iPhone Game with Cocos2D 2.X Tutorial》,文中使用Cocos2D,我在这里使用Cocos2D-x3.0alpha0进行学习和移植,前者是用Object-C所写,所以移植到Cocos2D-x会有些差异,比如某些函数、某些功能不能跟原文一样直接实现,需另转换方法实现。之前已经对Cocos2D-x的安装以及简单使用进行了介绍,这里不再介绍,直接进入主题。
步骤如下:使用脚本创建工程
./create-multi-platform-projects.py -p Ninja -k com.HuLuo.Ninja -l cpp
2.编译运行,可以看到如下图所示:

3.下载本游戏所需的资源,将资源放置"Resources"目录下;

4.游戏需要一个白色的背景,最简单的方法是使用CCLayerColor,将HelloWorldScene.h文件"HelloWorld"类改为如下:
class HelloWorld : public cocos2d::LayerColor之后添加如下代码:
Sprite* _player;
Sprite* _monster;
void addMonster();
void spriteMoveFinished(Node* sender);
void gameLogic(float dt);首先添加玩家,让玩家位于左边屏幕中间,将HelloWorldScene.cpp文件的init函数,改为如下:
bool HelloWorld::init()
{
bool bRet = false;
do {
CC_BREAK_IF(!LayerColor::initWithColor(Color4B(255, 255, 255, 255)));
Size winSize = Director::getInstance()->getWinSize();
_player = Sprite::create("Player.png");
_player->setPosition(Point(_player->getContentSize().width / 2, winSize.height / 2));
_player->setScale(2);
this->addChild(_player);
this->schedule(schedule_selector(HelloWorld::gameLogic), 1.0);
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::gameLogic(float dt)
{
}5.编译运行,可以看到玩家精灵在白色背景上,如下图所示:

6.接下来添加怪物,并且让怪物可以移动,我们在屏幕右边创建怪物,建立动作让它们向左移动,增加addMonster方法,代码如下:
void HelloWorld::addMonster()
{
_monster = Sprite::create("monster.png");
_monster->setScale(2);
Size winSize = Director::getInstance()->getWinSize();
int minY = _monster->getContentSize().height / 2;
int maxY = winSize.height - _monster->getContentSize().height / 2;
int rangeY = maxY - minY;
int actualY = (rand() % rangeY) + minY;
_monster->setPosition(Point(winSize.width + _monster->getContentSize().width / 2, actualY));
this->addChild(_monster);
int minDuration = 2;
int maxDuration = 4;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (rand() % rangeDuration) + minDuration;
MoveTo* actionMove = MoveTo::create(actualDuration, Point(-_monster->getContentSize().width / 2, actualY));
CallFuncN* actionMoveDone = CallFuncN::create(std::bind(&HelloWorld::spriteMoveFinished, this,_monster));
_monster->runAction(Sequence::create(actionMove,actionMoveDone, NULL));
}
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 进入详细评论页>>

![cocos2d_x+lua[2]](/uploads/allimg/131030/110J64609-0-lp.jpg)








