andengineでスプライトをランダムに動かす方法を知りたいですか?ゲーム内のボールを「ブラウン運動」のようにランダムな方向に連続的に動かしたい。
私はたくさん検索し、MoveModifierを使用してそれを取得しようとしましたが、残念ながらそれは機能しませんでした...。
動くボールの例を確認してください。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);
}
}
「Vinicius DSL」が言ったように box2d を使用するか、onUpdate [または onManagedUpdated] をオーバーライドしてそこから x と y の位置を変更することで、ボールを手動で移動できます。