SFX Played Once per Collision or Hit

Posted by David Dimalanta on Game Development See other posts from Game Development or by David Dimalanta
Published on 2012-12-08T03:20:49Z Indexed on 2012/12/08 6:00 UTC
Read the original article Hit count: 299

I have a question about using Box2D (engine for LibGDX used to make realistic physics). I observed on the code that I've made for the physics here below:

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) 
{
      // TODO Touch Up Event

     if(is_Next_Fruit_Touched)
     {
             BodyEditorLoader Fruit_Loader = new BodyEditorLoader(Gdx.files.internal("Shape_Physics/Fruity Physics.json"));

             Fruit_BD.type = BodyType.DynamicBody;
             Fruit_BD.position.set(x, y);

             FixtureDef Fruit_FD = new FixtureDef(); // --> Allows you to make the object's physics.
             Fruit_FD.density = 1.0f;
             Fruit_FD.friction = 0.7f;
             Fruit_FD.restitution = 0.2f;

             MassData mass = new MassData();
             mass.mass = 5f;

             Fruit_Body[n] = world.createBody(Fruit_BD);
             Fruit_Body[n].setActive(true); // --> Let your dragon fall.
             Fruit_Body[n].setMassData(mass);
             Fruit_Body[n].setGravityScale(1.0f);

             System.out.println("Eggs... " + n);

             Fruit_Loader.attachFixture(Fruit_Body[n], Body, Fruit_FD, Fruit_IMG.getWidth());
             Fruit_Origin = Fruit_Loader.getOrigin(Body, Fruit_IMG.getWidth()).cpy();

             is_Next_Fruit_Touched = false;
             up = y;
             Gdx.app.log("Initial Y-coordinate", "Y at " + up);

             //Once it's touched, the next fruit will set to drag.
             if(n < 50)
             {
                 n++;

             }else{

                 System.exit(0);

             }
     }

         return true;

}

Now, I'm thinking which part o line should I implement for the sound effects. My objectives to make SFX played once for every collision (Or should I say "SFX played once per collision"?) on the following:

  • SFX played once if they hit on the objects of its kind. (e.g. apple vs. apple)
  • SFX played once on a different sound when it hit on the ground. (e.g. apple land on the mud)

Take note that I'm using Box2D for the Java programming version thanks to LibGDX via Box2D engine and I edited the physics body using Physics Body Editor before I implement it to code.

I tried to check every available methods for body, fixture definition, or body definition to code for the SFX when hit but it seems only for the gravity and weight. Is there possibly available on the document for SFX played when collision happens if possible?

© Game Development or respective owner

Related posts about java

Related posts about collision-detection