2014-04-22 26 views
0

我是Andengine框架的Android編程初學者。我在互聯網上學習教程。我不知道我誤解了PhysicsHandler中Scence和Velocity屬性的根位置。什麼是PhysicsHandler中Scene,Velocity屬性的位置?

這裏: enter image description here

如果我的意見是真實的。因此,如何設置PhysicsHandler的Velocity屬性來跟蹤Scene的位置?

這是我的例子: enter image description here

而且我的代碼:

scene.setOnSceneTouchListener(new IOnSceneTouchListener() { 
      @Override 
      public boolean onSceneTouchEvent(Scene pScene, 
        TouchEvent pSceneTouchEvent) { 
       if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) { 
        positionUpX = pSceneTouchEvent.getX(); 
        positionUpY = pSceneTouchEvent.getY(); 

        ball = new Ball(positionDownX, positionDownY, positionUpX, 
          positionUpY, mFaceTextureRegion, 
          getVertexBufferObjectManager()); 
        scene.attachChild(ball); 
        return true; 
       } 
       if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) { 
        positionDownX = pSceneTouchEvent.getX(); 
        positionDownY = pSceneTouchEvent.getY(); 

        return true; 
       } 
       return false; 
      } 
     }); 




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

     public Ball(final float pDownX, final float pDownY, //Start 
       final float pUpX,final float pUpY, //End 
       final TiledTextureRegion pTextureRegion, 
       final VertexBufferObjectManager pVertexBufferObjectManager) { 
      super(pDownX, pDownY, pTextureRegion, pVertexBufferObjectManager); 
      this.mPhysicsHandler = new PhysicsHandler(this); 
      this.registerUpdateHandler(this.mPhysicsHandler); 
      this.mPhysicsHandler.setVelocity(pUpX, pUpY); 
     } 

     @Override 
     protected void onManagedUpdate(final float pSecondsElapsed) { 
      if (this.mX < 0) { 
       this.mPhysicsHandler.setVelocityX(BALL_VELOCITY); 
      } else if (this.mX + this.getWidth() > CAMERA_WIDTH) { 
       this.mPhysicsHandler.setVelocityX(-BALL_VELOCITY); 
      } 
      if (this.mY < 0) { 
       this.mPhysicsHandler.setVelocityY(BALL_VELOCITY); 
      } else if (this.mY + this.getHeight() > CAMERA_HEIGHT) { 
       this.mPhysicsHandler.setVelocityY(-BALL_VELOCITY); 
      } 
      super.onManagedUpdate(pSecondsElapsed); 
     } 
    } 

回答