2011-11-07 109 views
3

我使用此方法AndEngine的精靈添加到屏幕上,並使其在屏幕上移動。如何更改精靈隨機出現在現場的位置?

private void addFace() { 
    Random rand = new Random(); 

     float x = (int) mCamera.getHeight() - mBallTextureRegion.getHeight(); 
     float minY = mBallTextureRegion.getHeight(); 
     float maxY = (int)(mCamera.getWidth() + mBallTextureRegion.getWidth()); 
     float rangeY = maxY - minY; 
     float y = rand.nextInt((int)rangeY) + minY; 

    this.mFaceCount++; 
    Log.e("Faces: ", "Face" + this.mFaceCount); 

    Sprite face = null; 
    Body body = null; 


      face = new Sprite(x, y, this.mBallTextureRegion.clone()); 
      body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF); 

     this.mScene.attachChild(face); 
    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true)); 
      int minDuration = 2; 
      int maxDuration = 4; 

      int rangeDuration = maxDuration - minDuration; 
      int actualDuration = rand.nextInt(rangeDuration) + minDuration; 

      MoveXModifier mod = new MoveXModifier(actualDuration, face.getX(), - face.getWidth()); 

      face.registerEntityModifier(mod); 

}

我想什麼做的是,而不是被選擇的隨機位置和精靈被添加到場景的左側,我想爲它添加到頂部和摔倒。

我基本上要翻轉方向, 我只是無法弄清楚如何。一切我試過,沒有運氣。

任何想法或建議嗎?

回答

3

雖然我不知道很多關於Andengine,我懷疑你想改變這些行:

float x = (int) mCamera.getHeight() - mBallTextureRegion.getHeight(); 
float y = rand.nextInt((int)rangeY) + minY; 

MoveXModifier mod = new MoveXModifier(actualDuration, face.getX(), - face.getWidth()); 

翻轉x和y的語句,但設置y以-mBallTextureRegion.getHeight(),如果你希望它的外屏幕出現。

對於MoveXModifier,我猜想存在對應MoveYModifier(使用face.getY()和分別-face.getHeight())