keyboard movement and jumping
Create a symbol instance named player on the main stage. The controller uses
its bounds to keep it inside the stage, and treats the stage bottom as the floor.
It does not add platform or obstacle collisions.
general script
Put this in the entry q0rg's general script. It is the GAME_CONTROLLER fixture
from the player tests, so initial values and function definitions run once.
import q0.input
import q0.scene
import q0.time
speed = 240
gravity = 1440
jump_speed = -480
vy = 0
pb func update current_vy {
dt = time.dt
dx = 0
if input.left and not input.right {
dx = -speed * dt
}
if input.right and not input.left {
dx = speed * dt
}
grounded = player.bottom >= stage.height - 0.5
if input.jump_pressed and grounded {
current_vy = jump_speed
}
current_vy = current_vy + gravity * dt
dy = current_vy * dt
if player.left + dx < 0 {
dx = -player.left
}
if player.right + dx > stage.width {
dx = stage.width - player.right
}
if player.top + dy < 0 {
dy = -player.top
if current_vy < 0 {
current_vy = 0
}
}
if player.bottom + dy >= stage.height {
dy = stage.height - player.bottom
if current_vy > 0 {
current_vy = 0
}
}
player.x = player.x + dx
player.y = player.y + dy
return current_vy
}
frame code
Right-click the frame in the timeline and choose Add Frame Code.... Put this in that frame's code, not the general script:
vy = update(vy)
Frame code normally runs when playback enters the frame. With q0.input
imported, q0player also ticks a held frame at the movie's fps, including a
one-frame movie. time.dt is that fixed step in seconds.
controls
| action | keys |
|---|---|
| left | a or left arrow |
| right | d or right arrow |
| jump | w, up arrow or space |
input.jump stays true while a jump key is held; input.jump_pressed marks the
new press. The controller only starts a jump when that press happens on the floor.
Export a bundled .q0s and test it in q0player. The editor's code analysis does
not supply keyboard input or instance measurements.
See host api, syntax and built-in libraries.