我在屏幕上有两个按钮和一个玩家对象。第一个按钮必须将播放器向左移动,第二个按钮向右移动,但播放器不能平稳移动。如何让玩家顺利移动?
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);
}
}
答案 0 :(得分:1)
在MoveLeft方法中,您要添加一个预先固定的值,这不会缩放到帧速率。
您应该在此方法中设置类似“movingLeft = true”的标志,并且在您的processLogics中,您应该根据帧速率移动位置。