This site requires JavaScript, please enable it in your browser!
Greenfoot back
EatCakeAndDie
EatCakeAndDie wrote ...

2011/9/3

Wombat world edge problem! (newbie)

EatCakeAndDie EatCakeAndDie

2011/9/3

#
When wombat hits world edge I want him too: -turn -play a sound When he hits the edge he turns multiple times and plays multiple sounds and sometimes get stuck. Is there a way to make him turn ONCE and play the sounds ONCE, maybe with a wait-command or something? Thanks for help. Current Code: if ( atWorldEdge() ) { turn(180); Greenfoot.playSound("2525.wav"); }
kiarocks kiarocks

2011/9/3

#
maybe add a variable called hasturned or something. at the top write:
public boolean hasturned = false;
and then write
 if ( atWorldEdge() && hasturned == false)
        {
 turn(180);
         Greenfoot.playSound("2525.wav");
hasturned = true;
        }
instead of
 if ( atWorldEdge() )
        {
 turn(180);
         Greenfoot.playSound("2525.wav");
        }
danpost danpost

2011/9/3

#
@kiarocks -- What happens when it reaches another world edge? There is no code to reset the value of 'hasturned' back to 'false'! What is needed is a statement that asks if sound is playing: 'if (!(GreenfootSound.isPlaying("2525.wav"))) { Greenfoot.playSound("2525.wav"); }' It is also possible that the scenario is using cells and copying over the 'mover' class 'atWorldEdge()' method has the stop-distance (20) way too large. If that is the case, change all instances of '20' to '1' or '0', whichever works better in each instance of '20'.
kiarocks kiarocks

2011/9/4

#
how about this:
 if ( !atWorldEdge() )
        {
hasturned = false;

        }
DonaldDuck DonaldDuck

2011/9/4

#
I'm not all to familiar with this code, so I don't know all the methods, but this should work for you.
if( atWorldEdge() )
{
    turn(180);
    Greenfoot.playSound("2525.wav");
    move(); // whatever the method for making the actor step forwards is called.
}
This way, the next time it checks for Worlds Edge, the wombat will have moved and won't be at it!
DonaldDuck DonaldDuck

2011/9/4

#
danpost wrote...
What is needed is a statement that asks if sound is playing: 'if (!(GreenfootSound.isPlaying("2525.wav"))) { Greenfoot.playSound("2525.wav"); }'
Indeed, this would prevent the sound from playing on top of itself, but it doesn't solve the initial problem, it's just a workaround. The sound is playing multiple times because the if statement (if ( atWorldEdge() )) is resulting to true multiple times. To fix the problem, all you need to do is move off of the worlds edge before the if statement ends so it doesn't run again in the same instance of the edge.
You need to login to post a reply.