如何让精灵在场景中随机移动: - andengine

时间:2012-07-08 11:34:48

标签: box2d sprite andengine modifiers

我正在制作一个游戏,我需要大约50个小虫子从各个方向进入场景。我希望它们在屏幕上随机移动,但似乎不可能。似乎如果我使用MoveModifier,我必须为每个精灵提供一个结束位置。有没有办法,我可以不使用移动修改器。我不熟悉box 2d扩展,但我已经看到很多人通过将它们附加到物理体来使用它来移动精灵。我是否需要此扩展我不清楚。另外,我需要sprite来检测它们与其他动画精灵之间的碰撞检测。我怎么能这样做我不太清楚。请帮忙。以下是我的代码......看起来是否正确

    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);
        }
    }
};

1 个答案:

答案 0 :(得分:2)

说实话,对于这个问题你不需要box2d,除非你想在你的bug碰撞时做一些奇特的事情。为此,您需要使用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,而不是在一定时间后自动复位。