我试图让我在游戏中的敌人有涨有跌;因为我使用的是物理机身采用了雪碧上它,我不能用实体修饰符,所以我决定给身体使用.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