[SOLVED]problem with code (timer and keypresssed)

Started by deltree, Mon 02/05/2005 12:31:03

Previous topic - Next topic

deltree

hi,
I've got a problem with this code: (repeatedly execute)

if (IsKeyPressed(32)==1 && IsTimerExpired(2)==1) {
   SetTimer(2,40); //can't redo before 1 second
   PlaySound(5);
}

It is supposed to play a sound everytime you press "space", and it waits 1 second before you can press space again.
the problem is : it doesn't work. the sound doesn't play. however, I'm sure the sound plays in normal code.
the timer is already set in "player enters screen(after fadeout)" interaction code.
do you see what the problem is?
thanx

Ashen

According to the manual, IsTimerExpired() only returns 1 as the timer expires. So, unless you're pressing space at exactly that moment, the condition is never true and the sound never plays.
You might have to do something like:

Code: ags

int count;

//then, in rep_ex:

if (IsKeyPressed (32) && count == 0) {
  PlaySound (5);
  SetTimer (2, 40);
  count = 1;
}

if (IsTimerExpired (2)) count = 0;
I know what you're thinking ... Don't think that.

deltree

thanx,
I though it could be something like that.

SMF spam blocked by CleanTalk