我想知道在发动机中平稳地随机移动精灵的方法吗? 我希望我的游戏中的球能像“布朗运动”一样随机移动。
我搜索了很多,并试图通过使用MoveModifier来获得它,但不幸的是,这不起作用....
答案 0 :(得分:4)
检查移动球的例子,你只做一个类扩展Sprite或AnimatedSprite,以后你只设置X和Y速度的随机值:
private static class Ball extends AnimatedSprite {
private final PhysicsHandler mPhysicsHandler;
Random randomGenerator = new Random();
private float RandomX;
private float RandomY;
private int CAMERA_WIDTH=720;
private int CAMERA_HEIGHT=480;
public Ball(final float pX, final float pY, final TiledTextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
this.mPhysicsHandler = new PhysicsHandler(this);
this.registerUpdateHandler(this.mPhysicsHandler);
RandomX =randomGenerator.nextInt(3);
RandomY =randomGenerator.nextInt(3);
RandomX=RandomX*100;
RandomY=RandomY*100;
this.mPhysicsHandler.setVelocity(RandomX, RandomY);
}
@Override
protected void onManagedUpdate(final float pSecondsElapsed) {
if(this.mX < 0) {
this.mPhysicsHandler.setVelocityX(RandomX);
} else if(this.mX + this.getWidth() > CAMERA_WIDTH) {
this.mPhysicsHandler.setVelocityX(-RandomX);
}
if(this.mY < 0) {
this.mPhysicsHandler.setVelocityY(RandomY);
} else if(this.mY + this.getHeight() > CAMERA_HEIGHT) {
this.mPhysicsHandler.setVelocityY(-RandomY);
}
super.onManagedUpdate(pSecondsElapsed);
}
}
答案 1 :(得分:0)
您可以像使用“Vinicius DSL”所说的那样使用box2d,也可以通过覆盖onUpdate [或onManagedUpdated]并从那里更改x和y位置来手动移动球