Question

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.

Was it helpful?

Solution

There are 2 possibilities:

Background movement speed is constant, as you have it right now. Don't use dt in addTargetFibre, because you don't want to call it every frame. If you know the initialPosition, finalPosition and speed, just write a procedure that calculates the distance between 2 points (ccpDistance exists in the iOS version of Cocod2D, I'm sure there's a corespondent for Android)

int d = ccpDistance(intialPosition, finalPosition)/speed;
CCMoveTo actionMove = CCMoveTo.action(d, finalPosition);

Background movement speed is variable (ex: player controls background movement from joystick). If that is the case, you will have to move the sprites every frame, and you will have to have a method to call from your update to fix the position.

public void updateTargetFibres(float dt) 

The formula for updating the position every frame is

sprite.position = CGPointMake(sprite.position.x, sprite.position.y - speed*dt)

In this method, you can also check if your sprite has left the screen by checking the y of the position, and if it has, then delete it from the parent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top