当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Detect collision of sprite created TimeHandler

问题描述:

I am using a timehandler to generate sprites in Andengine.

Now I want detect collision between that sprites and other sprites in same Scene.

I tried using the code below but it did not work as expected.

Is there is another way to detect collision?

 //method to create sprite

public void add()

{

mFaceCount++;

Random rand = new Random();

int x = (int) CAMERA_WIDTH + mb1.getWidth();

int minY = mb1.getHeight();

int maxY = (int) (CAMERA_HEIGHT/2 - mb1

.getHeight());

int rangeY = maxY + minY;

// int y = rand.nextInt(280) + 320;

int y=rand.nextInt(350-280) + 280;

int minDuration = 2;

int maxDuration = 3;

int rangeDuration = maxDuration - minDuration;

int actualDuration = rand.nextInt(rangeDuration) + minDuration;

if(this.mFaceCount%2==0)

{

targetSprite =new Sprite(x,y,mb4.clone());

mScene.attachChild(b1);

B1= PhysicsFactory.createBoxBody(this.mPhysicsWorld, b1, BodyType.KinematicBody, FIXTURE_DEF);

}

else if(this.mFaceCount%3==0)

{

targetSprite =new Sprite(x,y,mb2.clone());

mScene.attachChild(b1);

B1= PhysicsFactory.createBoxBody(this.mPhysicsWorld, b1, BodyType.KinematicBody, FIXTURE_DEF);

}

B1.setLinearVelocity(-5, 0);

mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(b1, B1, true, false)

{

@Override

public void onUpdate(float pSecondsElapsed)

{

super.onUpdate(pSecondsElapsed);

}

});

}

// time handler

private void createSpriteSpawnTimeHandler(){

TimerHandler spriteTimerHandler;

float mEffectSpawnDelay = 2f;

spriteTimerHandler = new TimerHandler(mEffectSpawnDelay,true,new ITimerCallback(){

@Override

public void onTimePassed(TimerHandler pTimerHandler) {

add();

}

});

getEngine().registerUpdateHandler(spriteTimerHandler);

}

//update handler to detect collision

IUpdateHandler updateHandler = new IUpdateHandler()

{

@Override

public void reset() { }

@Override

public void onUpdate(final float pSecondsElapsed)

{

if (hanger.collidesWith(targetSprite))

{

System.out.println("Collision);

;

}

}

};

网友答案:

First assign Different fixtures based on type of bodies. Then implement contact listener for your world as follows:

    CustomContactListener customContact = new CustomContactListener(bxWorld);
            bxWorld.setContactListener(customContact); 




public class CustomContactListener implements ContactListener{
    World bxWorld;
    UserData userDataA, userDataB;
    Body bodyA, bodyB;
    GamePlayLayer gameScene;

    Fixture fixtureA, fixtureB;
    public boolean isFirstCollision;
    public  ArrayList<Body> menHitList;

    public CustomContactListener(World bxWorld) {
        bxWorld.setContactListener(this);
        this.bxWorld = bxWorld;
        this.gameScene = GamePlayLayer.gameScene;
        this.menHitList = new ArrayList<Body>();
    }

    @Override
    public void beginContact(Contact contact) {
        fixtureA = contact.getFixtureA();
        fixtureB = contact.getFixtureB();

        bodyA  = fixtureA.getBody();
        bodyB  = fixtureB.getBody();

        userDataA = (UserData) bodyA.getUserData();
        userDataB = (UserData) bodyB.getUserData();
    }

    @Override
    public void endContact(Contact arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void postSolve(Contact arg0, ContactImpulse arg1) {
        // TODO Auto-generated method stub
    }

    @Override
    public void preSolve(Contact contact, Manifold arg1) {
        Vector2[] contactVector = contact.getWorldManifold().getPoints();

        ObjectType objectTypeA = userDataA.getObjectType();
        ObjectType objectTypeB = userDataB.getObjectType();


        if((objectTypeA == ObjectType.ITEM_THROWN ) && objectTypeB == ObjectType.BOUNDARY_BOTTOM_DOWN ){

            // to remove multiple collisions
            // to prevent projectile mirvs from scheduler


        }else if(objectTypeA == ObjectType.BOUNDARY_BOTTOM_DOWN && objectTypeB == ObjectType.ITEM_THROWN ){


        }       


    }


}

You can implement what ever collision you want in this

分享给朋友:
您可能感兴趣的文章:
随机阅读: