0

我想知道在andengine中随机移动精灵的方法是什么?我想让球在我的比赛中像“布朗运动”一样在随机方向上连续移动。

我已经搜索了很多,并试图通过使用 MoveModifier 来获得它,但不幸的是那没有用....

4

2 回答 2

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);
            }
        }
于 2012-12-26T17:22:11.570 回答
0

您可以像“Vinicius DSL”所说的那样使用box2d,也可以通过覆盖onUpdate [或onManagedUpdated]并从那里更改它们的x和y位置来手动移动球

于 2012-12-31T06:08:08.273 回答