TGC Codebase Backup



sprite jumping code, timer based by Visigoth

26th Nov 2008 2:23
Summary

I've been playing with sprites, and needed a way to make a sprite jump, and just came up with this tonight. It is a function that allows a sprite to jump using the spacekey(), and



Description

basically, the function moves the sprite after a certain time has elapsed, set by the interval# value, and uses the sin() function to calculate what the y position is going to be. At 60 fps, a setting of around 30 to 40 for the interval seems pretty good. The higher the interval, the slower the jump. The height parameter is just how many pixels high to jump. Also, you could tweak the jump_angle incrementer to change the speed as well.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    rem requires 4 global variables to be declared and initialized.
global has_jumped
global sy
global jump_timer#
global jump_angle

has_jumped = 0
jump_angle = 10

function player_jump(sprite_id,height,interval#)

if spacekey() = 1 and has_jumped = 0
   has_jumped = 1
   sy = sprite y(sprite_id)
   jump_timer# = timer()
endif

if has_jumped = 1
   if timer() - jump_timer# > interval#
      sprite sprite_id, sprite x(sprite_id), sy - (sin(jump_angle) * height), sprite image(sprite_id)
      jump_timer# = timer()
      inc jump_angle,10
      if jump_angle > 180
         has_jumped = 0
         jump_angle = 10
         exitfunction
      endif
   endif
endif

endfunction