java - Delay between sound-playings? -
i'm building kind of simple game, it's red square can move around arrow keys, made finish object, detects when in it. now, made if "player" intersects "finish-object", play sound, play sound every tick. tried thread.sleep() , wait(), made whole game sleep, , not sound-delay. playsound method:
public void playsound( string filename ) throws ioexception{ inputstream in = new fileinputstream(filename); audiostream = new audiostream(in); audioplayer.player.start(as); } and playerfinishing intersecting:
//player1 level finishing if( player.getbounds().intersects(levelfinish.getbounds())){ message = "a winner you!"; try { playsound("d:/badgame/sounds/winner.wav"); } catch(exception ex){ ex.printstacktrace(); } } message string displays in middle of screen, works, needs kind of delay, sound won't play after each 10 seconds, or when intersect it, 1 time. can guys give me clue on how this? thanks!
the reason sound continues play because conditions still true every tick. intersection still valid after sound done, therefore sound play again.
a simplistic way prevent repetition use variable keep track of if have played sound. this, although untested:
put part somewhere won't changed every tick:
boolean soundplayed = false; use part in function
//player1 level finishing if( player.getbounds().intersects(levelfinish.getbounds())){ message = "a winner you!"; if (soundplayed == false) { try { playsound("d:/badgame/sounds/winner.wav"); soundplayed = true; } catch(exception ex){ ex.printstacktrace(); } } }
Comments
Post a Comment