Posted October 01, 2024 by Luiz Bills
#gamedev #game-engine #fantasy-console
From the beginning I had decided that in litecanvas there would be no functions to detect keyboard inputs (I would need a plugin/extension). But I decided to change my mind and now we have a function called iskeydown(key: string): boolean.
// basic usage
iskeydown('a') // check if the A key is pressed
iskeydown('arrowup') // check if the ARROW UP is pressed
iskeydown(' ') // check if SPACE is pressed
// also, you can check if "any" key is pressed
iskeydown('any')
// real world example
litecanvas()
function init() {
posx = 0
}
function update (dt) {
const speed = 200
// move to right pressing ARROW RIGHT
if (iskeydown('arrowright')) {
posx += speed * dt
}
// move to right pressing ARROW LEFT
if (iskeydown('arrowleft')) {
posx -= speed * dt
}
}
function draw() {
cls(0)
circfill(posx, CENTERY, 50, 4)
}