Running in conjunction with acceleration and Input.KeyDown
Author |
Message |
Mayb123
|
Posted: Thu Oct 11, 2007 7:53 pm Post subject: Running in conjunction with acceleration and Input.KeyDown |
|
|
after the ground question, i wanted to incorporate left and right movement so that while the left or right keys are down, the character, at the moment a dot, will move in that direction, building speed up to a maximum velocity. the character must only build speed and/or maintain its velocity until the user releases the key. that's where i'm stuck
code: |
setscreen ("graphics:640;480")
var key : array char of boolean
var ground, vcap, x, y, vx, vy, ax, g, charx, chary, carvx : real
const gravity := -0.5
x := 10
y := 10
vx := 1
ax := .8
vy := 5
% g := -0.5
vcap := 10.1
procedure jump
% Makes character jump at the press of the up key.
loop
cls
View.Set ("offscreenonly")
x += vx
y += vy
if round (y)>=10 then
vy += gravity
end if
drawfilloval (round (x), round (y), 2, 2, black)
delay (10)
exit when y = 10
View.Update
end loop
end jump
procedure crouch
% Makes character crouch on the ground.
end crouch
loop
View.Set ("offscreenonly")
Input.KeyDown (key)
if key (KEY_UP_ARROW) then
jump
elsif key (KEY_DOWN_ARROW) then
crouch
elsif key (KEY_RIGHT_ARROW) then
% movement right here. i think i need a procedure
end if
end loop
|
mod edit: switched quote tags to code |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Mazer
|
Posted: Thu Oct 11, 2007 10:45 pm Post subject: RE:Running in conjunction with acceleration and Input.KeyDown |
|
|
I would not use procedures in that way. You have a main loop for your program (the one at the bottom); don't stick any loops inside of that, especially to draw/View.Update in them.
As for the velocity thing, you've already got variables to keep track of the velocity's x and y component (check out the tutorials for using records, it'll help make things easier to keep track of).
So what you'll do is check inputs each time you run through the loop (as you are doing now) and if the LEFT arrow is pressed subtract some amount from vx, or add if RIGHT arrow is pressed.
After adding to the velocity, you'll want to subtract as well (let's pretend it's friction). But make sure you're bringing vx toward 0 (ie, if vx < 0, add to it, and if vx > 0, subtract).
Now make sure vx isn't going crazy. If it's getting really close to zero, then that last thing we did could cause it to jump back and forth when the user isn't pressing any keys, so if it's in the range of -SMALL_AMOUNT < vx < SMALL_AMOUNT (say SMALL_AMOUNT is 0.5 or something, whatever works for you), then just make vx 0. Also, if it's in the range of -vcap < vx < vcap, make sure to cap it.
Finally, add vx to x.
Did that help any? |
|
|
|
|
|
|
|