Android(AndEngine):身体连接上下的精灵

时间:2012-09-11 14:50:42

标签: android andengine physics

我试图让我游戏中的敌人上下起伏;因为我正在使用带有Sprite的物理体,所以我不能使用实体修饰符,所以我决定每当它的精灵到达某个点时使用.setLinearVelocity(float x,float y)方法给身体稍微推动一下在屏幕上。

只有一个身体很好,但我需要每隔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总是设置为true,所以有一个恒定的推动向上;当第三个物体进入时,第二个物体也会掉下来,最后一个物体就会上升

使用这段代码我没想到其他的东西......但我不知道还能尝试什么......我怎么能控制它呢?

提前致谢:)

编辑:在下面的anwser中添加了工作代码

2 个答案:

答案 0 :(得分:1)

我建议你将敌人的代码与更新处理程序的代码分开。创建一个包含Sprite和Body的类Enemy,将你的敌人放在一个数组中,并覆盖PhysicsWorld的onUpdate方法,使它通过敌人阵列并完成你想要的所有敌人。

这是一个代码片段,显示了一种非常简单的方法:

mEngine.registerUpdateHandler(new IUpdateHandler() {
        @Override
        public void reset() {}

        @Override
        public void onUpdate(float pSecondsElapsed) {
            for (Enemy e : enemies) {
                e.checkPositionAndBounce();
            }
        }
    });

请注意,这可能不是一个好主意,因为此代码可能会运行在与物理引擎不同的线程上,这可能会导致各种问题。更好的方法是覆盖PhysicsWorld的onUpdate方法:

@Override
public void onUpdate(final float pSecondsElapsed) {
    super.onUpdate();
    for (Enemy e : enemies) {
        e.checkPositionAndBounce();
    }
}

如果您不确定第一个代码段的含义,请查看“匿名内部类”。

答案 1 :(得分:1)

好的,这是最后的工作代码(我没有创建一种方法来检查敌人的位置或类别,因为现在我只是搞乱了机制;当我准备好开始时,我将创建一个新项目:

this.m_PhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false)
{
    @Override
    public void onUpdate(float pSecondsElapsed)
    {
        super.onUpdate(pSecondsElapsed);

        for(int i = 0; i <= MyApp.this.mSpriteCounter; i++)
        {

            if (rectangle_face[i].getY() >= y-50)
            {       
                final PhysicsConnector spritePhysicsConnector = m_PhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(rectangle_face[i]); 

                spritePhysicsConnector.getBody().setLinearVelocity(0, -3);      
             }
        }
    };

在这段代码中,rectangle_face []是一个Sprite数组;每个精灵都是这样创建的:

private void add_Box_Face()
{

    float random_x = (float) (28 +  (int)(Math.random() * ((this.CAMERA_WIDTH - 28*2) + 1)));

    final Body rectangle_face_body;


    rectangle_face[this.mSpriteCounter] = new Sprite(random_x, y, this.mRectangleFaceTextureRegion, this.getVertexBufferObjectManager());


    rectangle_face_body = PhysicsFactory.createBoxBody(this.m_PhysicsWorld, rectangle_face[this.mSpriteCounter], BodyType.DynamicBody, this.BOX_FIXTURE_DEF);

    rectangle_face_body.setUserData("target");

    rectangle_face_body.setLinearVelocity(0, -5);


    this.m_PhysicsWorld.registerPhysicsConnector(new PhysicsConnector(rectangle_face[this.mSpriteCounter], rectangle_face_body, true, false));

    this.mscene.attachChild(rectangle_face[this.mSpriteCounter]);

}

那就是它,再次感谢你的帮助:)

相关问题