0

我正在 AndEngine 中开发一款游戏,并且我有一系列汽车 sprties。我正在检查它们何时与屏幕上的其他对象发生碰撞,当它们发生碰撞时,该数组的单个索引应该改变它的速度。但是,屏幕上每辆车的速度也会发生变化。

任何帮助,将不胜感激。请在下面找到附加代码:

this.registerUpdateHandler(new IUpdateHandler() {
        @Override
        public void onUpdate(final float pSecondsElapsed) {
            for (int i = 0; i < rManager.getInstance().carArray.length; i++)
            {
                if(rManager.getInstance().carArray[i].getCarSprite().collidesWith(rManager.getInstance().iceArray[i].getIceSprite())) {
                    Log.e("test", "ice collision");
                    rManager.getInstance().carArray[i].setCarSpeed(3f);
                } else {
                    rManager.getInstance().carArray[i].setCarSpeed(0.5f);
                }
            }
        }

        @Override
        public void reset() {
            // TODO Auto-generated method stub

        }
    });

更新代码:

for (int i = 0; i < rManager.getInstance().carArray.length; i++)
                {
                    for (int j = 0; j < rManager.getInstance().iceArray.length; j++)
                    {
                        if(rManager.getInstance().carArray[i].getCarSprite().collidesWith(rManager.getInstance().iceArray[j].getIceSprite())) {
                            Log.e("log", "CAR COLLIDED WITH ICE LOL");
                            rManager.getInstance().carArray[i].setCarSpeed(3f);
                        } else {
                            rManager.getInstance().carArray[i].setCarSpeed(1f);
                        }
                    }
                }

更新代码2:

@Override
protected void onManagedUpdate(float pSecondsElapsed) {

            for (int i = 0; i < rManager.getInstance().carArray.length; i++)
            {                       
                for (int j = 0; j < rManager.getInstance().iceArray.length; j++)
                {
                    if(this.getX() < (rManager.camera.getWidth() + this.getWidth())){
                        this.setPosition(this.getX() + (rManager.getInstance().carArray[i].getSpeed() + speedMod), this.getY());
                    } else {
                        this.setPosition(0 - this.getWidth(), this.getY());
                    }

                    if (rManager.getInstance().iceArray[j].getIceSprite().collidesWith(rManager.getInstance().carArray[i].getCarSprite()))
                    {
                        Log.e("log", "car collided with ice");
                        this.setPosition(this.getX() + (rManager.getInstance().carArray[i].getSpeed() + 2f), this.getY());
                    }
                }
            }               


            super.onManagedUpdate(pSecondsElapsed);
    }
4

1 回答 1

0

First, notice that you are accessing to the array of (IceSprites) using the index of the cars. If you have a different number of these elements you can have exceptions.

Your code is only checking if each car collides with one ice sprite element. Is this what you want?

rManager.getInstance().carArray[i].getCarSprite().collidesWith(rManager.getInstance().iceArray[i].getIceSprite())

Maybe you want to check if your car collides with any ice element presented in your world. No? In that case you will need another loop for the ice elements.

I hope this helps,
Best Regards

于 2013-04-15T03:41:43.373 回答