LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Jumping and Gravity

Root / Programming Questions / [.]

JacboyXCreated:
Now I know for a fact I can use variables to make my character move left and right, but how can I get my character to jump and use a gravity system?

Here's a simple demo:
VAR X,Y'Position XY
VAR VX,VY'Velocity for each XY. Unlike acceleration, this is the actual movement speed of the player.
VAR AX,AY'Acceleration for each XY. This will be reset every frame.
VAR MVSP=0.3'Movement speed. Zero point three pixels per frame of button press.
VAR FRCTN=0.9'Think of this as "how much velocity do I want to preserve after the character strikes a wall?"
VAR BN'Button variable
WHILE 1
 BN=BUTTON()
 PHYSICS
 COLLISION
 RENDER
 VSYNC
WEND
DEF PHYSICS
 INC AY,0.5'Simulates gravity
 IF BN AND 1 THEN INC AY,-MVSP
 IF BN AND 2 THEN INC AY,MVSP
 IF BN AND 4 THEN INC AX,-MVSP
 IF BN AND 8 THEN INC AX,MVSP
 INC VX,AX
 INC VY,AY
 AX=0
 AY=0
END
DEF COLLISION
 IF(X<0)OR(X>400)THEN X=MIN(MAX(X,0),400):VX=-VX*FRCTN
 IF(Y<0)OR(Y>240)THEN Y=MIN(MAX(Y,0),240):VY=-VY*FRCTN
END
DEF RENDER
 GCLS
 GPSET X,Y
END
Don't shoot me if it doesn't work or doesn't look good. I can't quite test it atm, but this is the generic setup I use when dealing with a game's physics: Physics, collision, then render.

Okay, now I know where to put the SPSET command, but how can I use the SP commands to make sure the whole thing doesn't call out errors, to drive me insane?

Okay, now I know where to put the SPSET command, but how can I use the SP commands to make sure the whole thing doesn't call out errors, to drive me insane?
What kind of errors are you getting? Are you using SPOFS to move the sprite?

That's how it worked in one of my test programs. I wanted to put that sort of idea in the control mechanic program I did, but the animation looked chunky.

Call SPSETbefore the while and SPOFS during the render method (DEF render)

heres a kind of demo code thing:
'main loop
while death==0
spset 0,0
spofs 0,x,y
'movement left right
if button(0)==4 then dec x
if button(0)==8 then inc x
'gravity
inc y,2
'ground stopping fall
if y<(number of the ground NO PARENTHESIS!) then y=(number of ground STILL NO PARENTHESIS!)
'jump
if button(0)==16 then dec y,10
wait 1
wend
but idk if this will work just got it off the top of my head.