Reputation: 105
I have two buttons on the screen and a player object. The first button must move the player to the left and the second to the right, but the player doesn't move smoothly. How I can make the player to move smoothly?
ButtonSprite rightButton = new ButtonSprite(360,700,this.resourceManager.RightButtonRegion,this.vertexManager)
{
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
{
player.MoveRight();
return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
}
};
ButtonSprite leftButton = new ButtonSprite(50,700,this.resourceManager.LeftButtonRegion,this.vertexManager)
{
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
{
player.MoveLeft();
return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
}
};
public class Player extends Sprite
{
...
public void MoveLeft()
{
float x = this.body.getPosition().x -0.2f;
float y = this.body.getPosition().y;
this.body.setTransform(x,y,0);
}
public void MoveRight()
{
float x = this.body.getPosition().x + 0.2f;
float y = this.body.getPosition().y;
this.body.setTransform(x, y, 0);
}
}
Upvotes: 0
Views: 85
Reputation: 18276
In your MoveLeft method you are adding a pre-fixed value, this will not scale to frame rate.
You should set a flag like "movingLeft = true" at this method, and at your processLogics you should move the position based on framerate.
Upvotes: 1