Recently I have tried making a simple platformer game in kaboom js.
For some reason when I swiftly move the mouse upwards the player’s x position jumps from 400 to 2000 even though I don’t have anything that detects mouse movement.
kaboom({
background: [134, 135, 247],
width: 800,
height: 800,
});
var preference = localStorage.getItem('outfit');
if(null === preference)
{
localStorage.setItem("outfit",0)
}
loadSound( "background", "bg.wav" );
const music = play("background", {
volume: 0.8,
loop: true
})
onLoading((progress) => {
// Black background
drawRect({
width: width(),
height: height(),
color: rgb(0, 0, 0),
})
// A pie representing current load progress
drawCircle({
pos: center(),
radius: 32,
end: map(progress, 0, 1, 0, 360),
})
drawText({
text: "loading" + ".".repeat(wave(1, 4, time() * 12)),
font: "monospace",
size: 24,
anchor: "center",
pos: center().add(0, 70),
})
})
setGravity(1600)
let outfits=["tommy/normal.png","tommy/drip.png"]
loadSpriteAtlas(outfits[parseInt(localStorage.getItem("outfit"))], {
"left": {
"x":0,
"y":0,
"width": 96,
"height": 80,
"sliceX": 3,
"anims": {
"run": {
"from": 0,
"to": 2,
"speed": 15,
"loop":true
},
"idle": {
"from": 1,
"to": 1,
"speed": 15,
"loop":true
},
"jump": 1
}
},
})
loadSprite("grass","grass.png")
const level = addLevel([
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"==================",
"==================",
"==================",
"==================",
"==================",
], {
// The size of each grid
tileWidth: 48,
tileHeight: 48,
// The position of the top left block
pos: vec2(0,0),
// Define what each symbol means (in components)
tiles: {
"=": () => [
sprite("grass"),
area(),
scale(3),
body({ isStatic: true }),
anchor("bot"),
],
},
})
const player = add([
sprite("left", { anim: "idle" }),
pos(center()),
area(),
scale(2),
body(),
])
onKeyRelease("right", () => {player.play("idle")})
onKeyRelease("left", () => {player.play("idle")})
onKeyDown("right", () => {
player.flipX=false;
player.move(500*dt()*30,0)
if(player.curAnim() !== "run"){
player.play("run")
}
})
onUpdate(() => {
debug.log(player.pos)
})
let timee=0;
onKeyPress("space", () => {
if(player.isGrounded()){
player.jump()
}
})
onKeyDown("left", () => {
player.flipX=true;
player.move(-500*dt()*30,0)
if(player.curAnim() !== "run"){
player.play("run")
}
})
I tried limiting the player’s x so the player cannot go off screen but the code was messy and it did not really work.