2012-12-25 85 views
0

我想知道在引擎中隨機移動精靈的方法嗎? 我想在我的遊戲中連續移動像「布朗運動」這樣的隨機方向的球。如何隨機移動引擎中的精靈

我已經搜索了很多,已經嘗試通過使用MoveModifier得到那個,可惜沒有奏效....

回答

4

檢查動球例如,你只能讓一個類繼承雪碧或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); 
      } 
     } 
0

你要麼使用Box2D的像什麼「維尼DSL說:」或者你可以通過重寫的onUpdate [或onManagedUpdated]並改變其x和y位置手動移動球從那裏開始