I'm Using Cocos2d Android.
I have a scrolling background in Cocos2d, i use this method to make it work :
public void moveBackground(float dt) {
bg1.setPosition(CGPoint.ccp(bg1.getPosition().x, bg1.getPosition().y
- speed));
bg2.setPosition(CGPoint.ccp(bg2.getPosition().x, bg2.getPosition().y
- speed));
if (bg1.getPosition().y < -(bg1.getBoundingBox().size.height)) {
bg1.setPosition(CGPoint.ccp(bg1.getPosition().x,
bg2.getPosition().y + bg2.getBoundingBox().size.height - 1));
}
if (bg2.getPosition().y < -(bg2.getBoundingBox().size.height)) {
bg2.setPosition(CGPoint.ccp(bg2.getPosition().x,
bg1.getPosition().y + bg1.getBoundingBox().size.height - 1));
}
}
speed = 5 or 10 or whatever. It is called thanks to a schedule, every frame.
I want to drop targets with the same speed that my background uses to scroll, to add targets i use this method :
public void addTargetFibre(float dt) {
Random rand = new Random();
CCSprite target = CCSprite.sprite(RessourcesManager.Players.blue_ball);
target.setScale(0.3f);
CGSize winSize = CCDirector.sharedDirector().displaySize();
int actualX = (int) a;
int n = rand.nextInt(4);
switch (n) {
case 0:
actualX = (int) a;
break;
case 1:
actualX = (int) b;
break;
case 2:
actualX = (int) c;
break;
case 3:
actualX = (int) d;
break;
}
target.setPosition(actualX, winSize.height
+ (target.getContentSize().height / 2.0f));
addChild(target, 1);
float d = ((win_size.height+target.getContentSize().height/2)/(60*speed));
d = d+(d*(float)1/60);
CCMoveTo actionMove = CCMoveTo.action(d,
CGPoint.ccp(actualX, -target.getContentSize().height / 2.0f));
CCCallFuncN actionMoveDone = CCCallFuncN.action(this,
"removeSprite");
CCSequence actions = CCSequence.actions(actionMove, actionMoveDone);
target.runAction(actions);
}
What i want is the duration that i need to put in CCMoveTo action.