上一篇地址:http://www.cocoachina.com/bbs/read.php?tid=196836&page=1&toread=1#tpc
- Score indicator:
计分器
在“HelloWorldScene.h”中的“HelloWorld”类声明里编写这些代码,我们要开始制作一个分数指示器。
class HelloWorldHud : public cocos2d::Layer
{
public:
void numCollectedChanged(int numCollected);
virtual bool init();
CREATE_FUNC(HelloWorldHud);
cocos2d::LabelTTF *label;
};
```
在“HelloWorld”类的声明中编写这些代码。
``````
Private:
``````
int _numCollected;
static HelloWorldHud *_hud;
```
``````
In the ‘HelloWorldScene.cpp’,
Type these at the top of file:
编写在文档的顶部
HelloWorldHud *HelloWorld::_hud = NULL;
```
添加进方法HelloWorld::createScene()中,在return语句之前。
``````
auto hud = HelloWorldHud::create();
_hud = hud;
scene->addChild(hud);
```
``````
添加进方法setPlayerPosition中,在判断瓦片是否可收集的部分
``````
this->_numCollected++;
this->_hud->numCollectedChanged(_numCollected);
```
``````
现在添加这些代码:
bool HelloWorldHud::init()
{
if (!Layer::init())
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
label = LabelTTF::create("0", "fonts/Marker Felt.ttf", 18.0f, Size(50, 20), TextHAlignment::RIGHT);
label->setColor(Color3B(0, 0, 0));
int margin = 10;
label->setPosition(visibleSize.width - (label->getDimensions().width / 2) - margin,
label->getDimensions().height / 2 + margin);
this->addChild(label);
return true;
}
void HelloWorldHud::numCollectedChanged(int numCollected)
{
char showStr;
sprintf(showStr, "%d", numCollected);
label->setString(showStr);
}
```
这很好理解,我们创建了一个“LabelTTF”文本制作出我们要的数字(我们将其值设置为0,设置了字体,大小,颜色,还有位置。)
When the vegetable has been eaten by our sprite, the number will be plus 1.
当蔬菜被我们的精灵吃掉后,数字会增加1
8. Music & Sounds:
音效和声音
毫无疑问,如果没有音乐的话这会非常无聊。所以我们这就要让它非富起来。
添加下面两行进“HelloWorldScene. cpp”(当有些事需要涉及到音效的时候,我们要添加‘SimpleAudioEngine.h’,还有CocosDenshion是Cocos2d引擎里涉及音乐的方法。)
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
bool HelloWorld::init()
{
``````
SimpleAudioEngine::getInstance()->preloadEffect("error.mp3");
SimpleAudioEngine::getInstance()->preloadEffect("item.mp3");
SimpleAudioEngine::getInstance()->preloadEffect("step.mp3");
// I make this effect ’wade.mp3’ for fun. You can try to do it yourself, or simply delete them.
SimpleAudioEngine::getInstance()->preloadEffect("wade.mp3");
SimpleAudioEngine::getInstance()->playBackgroundMusic("background.mp3");
SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(0.1);
``````
}
```
(Notice that it is ‘playBackgroundMusic’, not ‘preloadBackgoundMusic’.)
(注意是‘playBackgroundMusic’, 而不是‘preloadBackgoundMusic’.)
// In case for collidable tile 添加进可碰撞瓦片判断语句中
SimpleAudioEngine::getInstance()->playEffect("error.mp3");
```
// In case of collectable tile 添加进可手机瓦片判断与剧中
SimpleAudioEngine::getInstance()->playEffect("item.mp3");
```
// Right before setting player position放置在设定玩家位置前
SimpleAudioEngine::getInstance()->playEffect("step.mp3");
```
// I make this effect for fun. You can try to do it yourself, or simply delete them. 制作这个涉水的声音是做着玩的,你也可以自己试试做一下,不想的话直接删除也行。
SimpleAudioEngine::getInstance()->playEffect("wade.mp3");
```
音效效果用图片似乎不太好展示。
9. Add Enemy:
增加敌人
也许你还记得我们是怎样创建我么你的精灵,人物的。但是我们会把它弄得略有不同以此创建多数的敌人。我们仍然在“Object-Player”层中执行。把属性值设置为如图所示。(名字、 属性名、属性值。如果你打算拥有不同类型的敌人,你可以因此设置不同的值。)
// 编写这些代码在我们创建玩家的后面
bool HelloWorld::init()
{
``````
for (auto& eSpawnPoint: objects->getObjects()){
ValueMap& dict = eSpawnPoint.asValueMap();
if(dict"Enemy"].asInt() == 1){
x = dict"x"].asInt();
y = dict"y"].asInt();
this->addEnemyAtPos(Point(x, y));
}
}``````
}
```
In the ‘HelloWorldScene.cpp’
void HelloWorld::addEnemyAtPos(Point pos)
{
auto enemy = Sprite::create("030.png");
enemy->setPosition(pos);
enemy->setScale(0.5);
this->animateEnemy(enemy);
this->addChild(enemy);
_enemies.pushBack(enemy);
}
```
10. Make them move:
让他们动起来
敌人将会追赶我们的玩家。因为玩家将会随我们的想法移动,我们会使得这些敌人自动追赶我们的玩家。我会是他们每0.3秒移动10个像素点。
void HelloWorld::enemyMoveFinished(Object *pSender)
{
Sprite *enemy = (Sprite *)pSender;
this->animateEnemy(enemy);
}
void HelloWorld::animateEnemy(Sprite *enemy)
{
float actualDuration = 0.3f;
auto position = (_player->getPosition() - enemy->getPosition()).normalize()*10;
auto actionMove = MoveBy::create(actualDuration, position);
auto actionMoveDone = CallFuncN::create(CC_CALLBACK_1(HelloWorld::enemyMoveFinished, this));
enemy->runAction(Sequence::create(actionMove, actionMoveDone, NULL));
}
```
这个方法‘animateEnemy’创建了两个“动作”。第一个使敌人没0.3秒移动10像素点。你能改变这个值,如果你想的话。另一个会引用方法’enemyMoveFinished’。我是用了‘Sequence::create’语句是它们绑定在了一起。当地一个方法实现后,第二个将会被引用并开始工作。
但是我们要敌人能够转向并面对玩家。
添加这些代码进’void HelloWorld::animateEnemy(Sprite *enemy)’
void HelloWorld::animateEnemy(Sprite *enemy)
{
auto actionTo1 = RotateTo::create(0, 0, 180);
auto actionTo2 = RotateTo::create(0, 0, 0);
auto diff = ccpSub(_player->getPosition(), enemy->getPosition());
if (diff.x < 0) {
enemy->runAction(actionTo2);
}
if (diff.x > 0) {
enemy->runAction(actionTo1);
}
``````
float actualDuration = 0.3f;
``````
}
```
Let us see the result:
看看结果:
Enemy movement 1 Enemy movement2
未完待续~(一日一更)
下一篇地址:http://www.cocoachina.com/bbs/read.php?tid=197315&page=1&toread=1#tpc


