What is the best way to apply move action to a physics sprite?

Cocos2d-x C++ Physics
  • efares
    Likes 0

    Problem Description

    Hi, I’m using Cocos2d-x v3.7 to make a JavaScript game.

    I have a physics sprite and I am interested in moving it on a certain collision. I know that move actions (MoveTo() and MoveBy() ) cannot be applied to physics sprites. I found applyImpulse() to be the best alternative for my needs but I want to move my physics sprite a tad bit on the screen so I am thinking that I will have to apply an impulse then apply another impulse again in the opposite direction to stop the physics sprite from moving.

    Is this the best alternative method to using MoveBy on a physics sprite or is there a better way?

  • Sonar Systems admin
    Likes 0

    If you only want to move a little bit then use an update method and update its position slightly every frame then stop updating once it gets to a the desired point.

  • efares
    Likes 0

    That’s an interesting thought.

    So you’re saying inside the update method I would use setPosition() ?

    I’ll give that a try!

  • efares
    Likes 0

    That did not work. I tried both

    this.sprite.setPositionY(this.carSprite.getPositionY() - 10);
    //and
    this.body.p.y = this.body.p.y - 10;

    Neither method worked on my android device. I presumed that if MoveBy() did not work on physics bodies then setPosition wouldn’t have worked either.
    They work on web (html5) but not on Android (JSB).

    I guess I’ll just have to use impulse then.

  • Sonar Systems admin
    Likes 0

    Yh use impulse probably best.

  • efares
    Likes 0

    Ya, I solved it using impulse.

    Thanks for your help!

Login to reply