background image

移动

cocos2d-x 精灵的方法

void addTarget()函数将会帮我们完成这一工作,敌人将会以随机的速度,从游戏场景

左移动到右。

HelloWorldScence.h 里声明 void addTarget(),并在 HelloWorldScene.cpp 里添加以下

的代码,

(请不要忘记在 HelloWorldScene.cpp 的开头加入 using namespace cocos2d)

1// cpp with cocos2d-x

2void HelloWorld::addTarget()

3{

4 CCSprite *target = CCSprite::spriteWithFile("Target.png",

5 CCRectMake(0,0,27,40) );

6

7 // Determine where to spawn the target along the Y axis

8 CCSize winSize = CCDirector::sharedDirector()->getWinSize();

9 int minY = target->getContentSize().height/2;

10 int maxY = winSize.height

11 - target->getContentSize().height/2;

12 int rangeY = maxY - minY;

13 // srand( TimGetTicks() );

14 int actualY = ( rand() % rangeY ) + minY;

15

16 // Create the target slightly off-screen along the right edge,

17 // and along a random position along the Y axis as calculated