Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

}

share|improve this question
    
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. –  Qqwy Jan 27 '13 at 16:10
    
Check out my edit. –  twalker22 Jan 27 '13 at 16:18
    
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? –  Qqwy Jan 27 '13 at 16:24
    
Yeah its just a class level boolean right now. –  twalker22 Jan 27 '13 at 16:33
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks for the response. I think this may solve my problem. Could you give me an example? –  twalker22 Jan 27 '13 at 23:23
    
This is an interesting idea. Curious for an example as well if anyone has one handy. –  Cameron Fredman Feb 6 '13 at 17:57
    
All "real" physics engines do this in some way. check eg Box2D. –  Sean Middleditch Feb 6 '13 at 19:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.