Pregunta

I am making a game where i require about 50 small bugs to be entering the scene from all directions. I want them to move around randomly on the screen but it doesn't seem possible. It seems that if i use a MoveModifier, i have to provide an ending position for each sprite. Is there any way i can do this without using move modifier. I am not familiar with box 2d extension but i have seen that many people have used it for moving sprites by attaching them to a physics body. Will i be needing this extension i am not clear on that. Also i need sprites to detect collision detection between themselves and other animated sprites. How can i do this i am not so clear. Please help. Following is my code .. does it seem right

    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);
        }
    }
};
¿Fue útil?

Solución

To be honest, for this problem you don't need box2d unless you want to do something fancy when your bugs are colliding. To do this, you need to make use of IUpdateHandler. I will give you a rough example of how I would do this for your problem:

    //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() {
        }

    });

This should result in random movement of each bug as every frame of the game is rendered. You can make the randomness less frequent if you want to add more bugs and eat on processing power, but you may want to check less than every frame like this algorithm is doing. To do so you might want to use a TimerHandler instead which autoresets after a certain duration.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top