2
\$\begingroup\$

So recently i thought i figured out a solution for it, only to find out it doesnt work when i have more than one of the same object type added to the level.

What i was doing is iterating through my different list of objects and checking if any of them are colliding with the player, if they are i play the sound for that one colliding with the player and then i set the boolean for the sound to true, meaning the sound has been played now.

When their are no collisions going on or if the current object being iterated isnt colliding with the player i set the sound boolean back to false. So as you probably can see, there is a issue when there is more than one of the same objects added because one of them will set the boolean to false and the sound will be played over and over as long as the player is colliding with one of the objects.

So i have run out of solutions to fix this so i am seeking out help.

Thanks anyone who can offer assistance.

EDIT:

Here is an example of how i am doing it now:

private void checkNeutalSwitchers() {

Iterator<Tile> switcherIT = this.neutralSwitcherTiles.iterator();

while (switcherIT.hasNext()) {
    Sprite square_sprite = switcherIT.next();
    float distance = MathUtils.distance(playerBoat.getX(),
            playerBoat.getY(), square_sprite.getX(), square_sprite.getY());
    if (distance < 11) {
        if (square_sprite.collidesWith(player)) {
            if (square_sprite.getUserData().equals("triangle")) {
                playerBoat.detachChild(player);
                player = swithcer.getTriangle();
                playerBoat.attachChild(player);
                if (this.neutralSwitcherBool == false) {
                    this.neutralSwitcherBool = true;
                    this.switchShapesSound.play();
                }

            }
            if (square_sprite.getUserData().equals("square")) {
                playerBoat.detachChild(player);
                player = swithcer.getSquare();
                playerBoat.attachChild(player);
                if (this.neutralSwitcherBool == false) {
                    this.neutralSwitcherBool = true;
                     this.switchShapesSound.play();
                }

            }
            if (square_sprite.getUserData().equals("circle")) {
                playerBoat.detachChild(player);
                player = swithcer.getCircle();
                playerBoat.attachChild(player);
                if (this.neutralSwitcherBool == false) {
                    this.neutralSwitcherBool = true;
                    this.switchShapesSound.play();
                }

            }
            if (square_sprite.getUserData().equals("rectangle")) {
                playerBoat.detachChild(player);
                player = swithcer.getRect();
                playerBoat.attachChild(player);
                if (this.neutralSwitcherBool == false) {
                    this.neutralSwitcherBool = true;
                    this.switchShapesSound.play();
                }

            }
        } else {
            this.neutralSwitcherBool = false;
        }
    }
}

}

\$\endgroup\$
4
  • \$\begingroup\$ I'm new to andengine, but bit seems it is implemented in Java. Isn't it possible to simply give every collidable object a 'collision_sound_playing' property and set this to true and false for each object in a loop during runtime? In short, you should add a flag to each of the objects, not on the sound. \$\endgroup\$ Commented Jan 27, 2013 at 16:10
  • \$\begingroup\$ Check out my edit. \$\endgroup\$ Commented Jan 27, 2013 at 16:18
  • \$\begingroup\$ What happens when you add a neutralSwitcherBool property to the square_sprite class? Right now this.neutralSwitcherBool is referring to a property on 'this' which is probably right now some kind of global engine object? \$\endgroup\$ Commented Jan 27, 2013 at 16:24
  • \$\begingroup\$ Yeah its just a class level boolean right now. \$\endgroup\$ Commented Jan 27, 2013 at 16:33

1 Answer 1

1
\$\begingroup\$

Maintain a list of active contacts. Then you can check or have events for "collision started," "collision ended," and "collision maintained." Fire the sound only when a collision is started.

You can maintain contacts easily using a hash table. Sort the object ids/ pointers. So the lesser is first, and add the pair to the table. You can store extra information if it helps at all. Just use the sorted objects pair as a key.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks for the response. I think this may solve my problem. Could you give me an example? \$\endgroup\$ Commented Jan 27, 2013 at 23:23
  • \$\begingroup\$ This is an interesting idea. Curious for an example as well if anyone has one handy. \$\endgroup\$ Commented Feb 6, 2013 at 17:57
  • \$\begingroup\$ All "real" physics engines do this in some way. check eg Box2D. \$\endgroup\$ Commented Feb 6, 2013 at 19:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.