我試圖讓我在遊戲中的敵人有漲有跌;因爲我使用的是物理機身採用了雪碧上它,我不能用實體修飾符,所以我決定給身體使用.setLinearVelocity(浮法X,浮法y)的每一次方法輕輕一推它的精靈達到一定點在屏幕上。的Android(AndEngine):雪碧與身體連接上上下下
只有一個身體很好,但我需要每隔5秒產卵一次並做同樣的事情,但我不知道如何跟蹤它們...我的意思是,我不知道如何在每一個機構獨立於彼此到達y位置控制......
例如,現在的代碼是這樣的:
private void add_Box_Face()
{
float random_x = (float) (28 + (int)(Math.random() * ((this.CAMERA_WIDTH - 28*2) + 1)));
final Body rectangle_face_body;
final Sprite rectangle_face = new Sprite(random_x, this.y, this.mRectangleFaceTextureRegion, this.getVertexBufferObjectManager());
rectangle_face_body = PhysicsFactory.createBoxBody(this.m_PhysicsWorld, rectangle_face, BodyType.DynamicBody, this.BOX_FIXTURE_DEF);
rectangle_face_body.setUserData("target");
//I give the body a initial push
rectangle_face_body.setLinearVelocity(0, -5);
//I register an update handler to the sprite to control if it reaches a certain Y value
rectangle_face.registerUpdateHandler(new IUpdateHandler()
{
@Override
public void onUpdate(float pSecondsElapsed)
{
if (rectangle_face.getY() >= y-50)
{
//Here I just use a flag so that later on below I can do the push
MyApp.this.setLinearVelocity = true;
}
}
@Override
public void reset()
{
// TODO Auto-generated method stub
}
});
//Here I register the physic connector and if the flag permits it, I push the body up
this.m_PhysicsWorld.registerPhysicsConnector(new PhysicsConnector(rectangle_face, rectangle_face_body, true, false)
{
@Override
public void onUpdate(float pSecondsElapsed)
{
super.onUpdate(pSecondsElapsed);
if(MyApp.this.setLinearVelocity)
{
rectangle_face_body.setLinearVelocity(0, -3);
MyApp.this.setLinearVelocity = false;
}
}
});
this.mscene.attachChild(rectangle_face);
}
通過這樣的代碼第一個主體按照計劃進行,它會上下移動,但一旦另一個主體彈出,它就會掉下來,另一個主體會上升,因爲布爾setLinearVelocity總是s如果真的如此,那麼就有一個向上的推動。當第三機身自帶的,第二身體下落以及和這最後一個需要它的地方去了
有了這個代碼,我沒想到別的了......不過我不知道還有什麼我可以試試...我怎麼能控制這個?
感謝提前:)
編輯:添加在anwser工作的代碼如下
感謝您的答覆,併爲您的幫助! 是的我試過使用一個數組,它的工作原理!我重寫PhyiscsWorld的方法的onUpdate,就像你說的,它似乎是即使有移動物體的一打(這是非常之多是爲了什麼我想要做的最壞的情況下)相當順利工作。 我想發佈現在的代碼,以防有人想要做同樣的事情,但由於我沒有足夠的聲譽,所以我還不能回答我的問題,而且我也不想將其添加到原來的帖子,因爲它會變得太長;再次感謝你的幫助! :) – Beriol