2012-07-08 61 views
2

我正在製作一款遊戲,我需要大約50個小蟲子從各個方向進入場景。我希望他們在屏幕上隨意移動,但似乎不可能。看來,如果我使用MoveModifier,我必須爲每個精靈提供一個結束位置。有沒有什麼辦法可以做到這一點,而不使用移動修改器。我不熟悉box 2d的擴展,但我已經看到很多人已經將它用於移動精靈,並將它們附加到物理機構中。我是否需要這個擴展我不清楚。另外我需要精靈來檢測自己和其他動畫精靈之間的碰撞檢測。我怎麼能做到這一點我不是很清楚。請幫忙。以下是我的代碼..它似乎正確如何讓精靈在現場隨機移動: - andengine

private Runnable mStartMosq = new Runnable() { 
    public void run() { 
     getWindowManager().getDefaultDisplay().getMetrics(dm); 
     Log.d("Level1Activity", "width " + dm.widthPixels + " height " + dm.heightPixels); 

     int i = nMosq++; 

     Scene scene = Level1Activity.this.mEngine.getScene(); 
     float startX = gen.nextFloat() * CAMERA_WIDTH; 
     float startY = gen.nextFloat() * (CAMERA_HEIGHT); // - 50.0f); 
     sprMosq[i] = new Sprite(startX, startY, 
       mMosquitoTextureRegion,getVertexBufferObjectManager()); 
     body[i] = PhysicsFactory.createBoxBody(mPhysicsWorld, sprMosq[i], BodyType.DynamicBody, FIXTURE_DEF); 

     sprMosq[i].registerEntityModifier(new SequenceEntityModifier(
       new AlphaModifier(5.0f, 0.0f, 1.0f), 
       new MoveModifier(60.0f, sprMosq[i].getX(), dm.widthPixels/2 , sprMosq[i].getY(), dm.heightPixels/2 , EaseBounceInOut.getInstance()))); 

     scene.getLastChild().attachChild(sprMosq[i]); 
     mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(sprMosq[i], body[i], true, true)); 
     if (nMosq < 50) { 
      mHandler.postDelayed(mStartMosq, 5000); 
     } 
    } 
}; 
+0

哦!我不知道..謝謝 – 2012-07-09 04:03:36

回答

2

說實話,對於這個問題,除非你想做些什麼花哨的,當你的錯誤發生碰撞你不需要Box2D的。爲此,您需要使用IUpdateHandler。我會給你我會怎麼做這您的問題一個粗略的例子:

//Velocity modifier for the bugs. Play with this till you are happy with their speed 
    private final velocity = 1.0f; 

    sprMosq[i] = new Sprite(startX, startY, 
    mMosquitoTextureRegion,getVertexBufferObjectManager()); 

    //Register update handlers 
    sprMosq[i].registerUpdateHandler(new IUpdateHandler(){ 

     @Override 
     public void onUpdate(float pSecondsElapsed) { 
      //First check if this sprite collides with any other bug. 
      //you can do this by creating a loop which checks every sprite 
      //using sprite1.collidesWith(sprite2) 

      if(collision){ 
       doCollisionAction(); //Whatever way you want to do it 
      } else { 
       float XVelocity; //Velocity on the X axis 
       float YVelocity; //Velocity on the Y axis 
       //Generate a different random number between -1 and 1 
       //for both XVelocity and YVelocity. Done using Math.random I believe 

       //Move the sprite 
       sprMosq[i].setPosition(XVelocity*velocity*pSecondsElapsed, 
            YVelocity*velocity*pSecondsElapsed); 

      } 


     } 

     @Override 
     public void reset() { 
     } 

    }); 

這將導致每個錯誤的隨機運動作爲遊戲的每一幀渲染。如果你想增加更多的bug並且在處理能力上吃東西,你可以減少隨機性,但是你可能想要檢查比這個算法所做的更少的每一幀。要做到這一點,您可能需要使用TimerHandler,而不是在一段時間後自動復位。

+0

非常感謝您的幫助!我會試試這個。 – 2012-07-11 04:49:48