2012-01-30 27 views
0

我想要一個程序,其中一個對象(球體)從屏幕頂部落下,反彈幾次並安置在屏幕中間。我是android的新開發者。誰能幫我?在Android中彈跳對象

+0

嘿,做一些研究和具體的問題,請編輯並添加一些更多的細節,但通常這是一個具體的問題不一樣,廣泛的人一個問題頁面。我想別人會低估你的問題!作爲提示使用:AndEngine – joecks 2012-01-30 15:18:02

+0

谷歌「android彈跳球示例」或「android彈跳球教程」,本網站針對的是具體問題,而不是全程漫步 – curtisk 2012-01-30 15:19:45

回答

1

嗯,據說這已經不是一個Android特定的問題,但是有一個Android特定的答案。 該樣本來自AndEngine's framework examples。它會給你一個想法如何反彈:

 private static class Ball extends AnimatedSprite { 
      private final PhysicsHandler mPhysicsHandler; 

      public Ball(final float pX, final float pY, final TiledTextureRegion pTextureRegion) { 
        super(pX, pY, pTextureRegion); 
        this.mPhysicsHandler = new PhysicsHandler(this); 
        this.registerUpdateHandler(this.mPhysicsHandler); 
      } 

      @Override 
      protected void onManagedUpdate(final float pSecondsElapsed) { 
        if(this.mX < 0) { 
          this.mPhysicsHandler.setVelocityX(DEMO_VELOCITY); 
        } else if(this.mX + this.getWidth() > CAMERA_WIDTH) { 
          this.mPhysicsHandler.setVelocityX(-DEMO_VELOCITY); 
        } 

        if(this.mY < 0) { 
          this.mPhysicsHandler.setVelocityY(DEMO_VELOCITY); 
        } else if(this.mY + this.getHeight() > CAMERA_HEIGHT) { 
          this.mPhysicsHandler.setVelocityY(-DEMO_VELOCITY); 
        } 

        super.onManagedUpdate(pSecondsElapsed); 
      } 
    } 
+0

謝謝Rodionov – 2012-01-30 15:58:49