3

I'm creating an android game and using AndEngine. Background Sprite movement speed is not same in different devices,its varying. I'm using the below code to move bgSprite. How to maintain constant speed of the game for different devices.?

 bgSprite1.setPosition(bgSprite1.getX() - 10, bgSprite1.getY());
4

1 回答 1

0

创建一个扩展 Sprite 的类,例如我为我做了这个(应该是一个 var,我用来知道何时销毁 sprite(在这种情况下,当它离开屏幕时)):

public class BackgroundMovingLine extends Sprite {

public boolean shouldDie;
private int speed;

BackgroundMovingLine(final int pX,final int pY, final ITextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager)
{
    super(pX,pY,pTextureRegion,pVertexBufferObjectManager);
    shouldDie=false;

    speed=(new Random()).nextInt(150)+250;
}
@Override
protected void onManagedUpdate(final float pSecondsElapsed) { 

    float h=this.getY();

    h=(h+(speed*pSecondsElapsed));

    if(h>800)
    {
        shouldDie=true;
    }

    this.setY(h);

}
}

速度它是移动精灵的速度。我是随机的,但你可以将它设置为你想要的。

于 2012-10-31T13:46:41.240 回答