LoginLogin

Delay in sprite clearing

Root / Programming Questions / [.]

ninja12Created:
Hi, I'm trying to clear an enemy sprite 2 seconds after its hp=0. How can I do this without the wait command holding up the program. Btw I'm using functions using spfunc Thanks

If you're using VSYNC in main loop of your program, your code will execute at about 60 fps. (Or 30 fps for VSYNC 2, etc.) With this, you can create an accurate timer for your dead enemy sprite.
VAR DEATH_TIME=-1'set to -1 when sprite is not dead
WHILE 1'main loop
 ...'the rest of your game's code
 PLAYER_ATTACK'in here, when the player strikes an enemy, it's death_time is set to 0 to signify that it's counter should start
 KILL_ENEMY'delayed enemy death
 VSYNC
WEND

DEF KILL_ENEMY
 IF DEATH_TIME!=-1 THEN INC DEATH_TIME ENDIF'increment the death timer when it is not -1
 IF DEATH_TIME>=60*2 THEN SPCLR ...'clear the sprite
END
We do 60*2 because the code executes at 60 fps and we wish to wait 2 seconds, or 120 frames, before SPCLRing.

For sprite functions, this is usually what I do. It’s handy if you have a particular death animation or action the enemy does when it dies.
'set sprite N
SPVAR N,0,10 'enemy hp
SPVAR N,1,120 'this will be our death timer
SPFUNC N,”DOSTUFF” 'the enemy sprite function

LOOP
 PLAYERCODE
 CALL SPRITE
 VSYNC
ENDLOOP

DEF DOSTUFF
 VAR I=CALLIDX()
 VAR HP=SPVAR(I,0)
 'enemy movement, etc
 IF HP<=0 THEN ' you’ll have to determine when target HP decreases then store that value as SPVAR N,0,HP
  SPANIM I,...'death animation
  SPFUNC I,”DIE”
 ENDIF
END

DEF DIE
 VAR I=CALLIDX()
 VAR T=SPVAR(I,1) 'timer
 DEC T
 SPVAR I,1,T
 IF T<=0 THEN SPCLR I
END
 

Oh nice, thanks for the help guys. Yeah I was trying to figure out incrementing a counter for sprite function variables