Android cocos2d-x开发(四)之如何添加一个精灵和移动一个精灵

[color=red]1、加入图片资源[/color] 如果你运行build_native.sh来编译的话,应该把图片资源加入到Resources文件夹里,否则就把图片文件复制到“assets”文件夹下。

[color=red]2、添加一个精灵[/color]
[color=red] //------------------在场景中添加一个敌人精灵---------start------------------------------[/color] CCSprite *target = CCSprite::spriteWithFile("Target.png", CCRectMake(0, 0, 27, 40));

//determine where to spawn the target along the Y axis
CCSize winSize = CCDirector::sharedDirector()->getWinSize();

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

int maxY = winSize.height - target->getContentSize().height/2;

int rangeY = maxY - minY;

//srand(TimGetTicks);
int actualY = (rand() % rangeY) + minY;

//Create the target slightly off-screen along the right edge,
//and along a random position along the Y axis as calculated
target->setPosition(ccp(winSize.width + (target->getContentSize().width/2), actualY));
this->addChild(target);
[color=red]//------------------在场景中添加一个敌人精灵---------end------------------------------[/color] //下面是使精灵移动的代码
//Determine speed of the target
int minDuration = (int)2.0;
int maxDuration = (int)4.0;

int rangeDuration = maxDuration - minDuration;

//srand(TimGetTicks())
int actualDuration = (rand() % rangeDuration) + minDuration;

//Create the actions
CCFiniteTimeAction* actionMove = CCMoveTo::actionWithDuration((ccTime)actualDuration, ccp(0-target->getContentSize().width/2, actualY));

CCFiniteTimeAction* actionMoveDone = CCCallFuncN::actionWithTarget(this, callfuncN_selector(HelloWorld::spriteMoveFinished));

target->runAction(CCSequence::actions(actionMove, actionMoveDone, NULL));

}

void HelloWorld::gameLogic(ccTime dt)
{
this->addTarget();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值