This is my first time building an original game and I am using kaboom.js in replit to do so. I’m a fairly in-experienced coder (I know basic html, css, and js) and since it is a fairly new system, it is hard to google search answers so a lot of my building has been reading the sheet from the kaboom.js website and a lot of trial and error. I am building a platformer with some shooting mechanics and I have 2 issues I can’t figure out for the life of me how to fix.
Issue 1: When the character changes directions, and a new sprite is added, when I push the shoot button the first bullet freezes and won’t move until I shoot the second bullet. Its supposed to move in the direction my character is moving. I have done a ton of tinkering to try and make it work but here is the code as it stands now:
function spawnBullet(p){
add([
rect(5,1),
pos(p),
origin(vec2(-3, -20)),
color(255, 255, 0),
'bullet',
])
}
function shootRight() {
if (playerRight) {
spawnBullet(player.pos.add(5,0))
action('bullet', (b) => {
b.move(200, 0)
})
}
}
function shootLeft() {
if (playerLeft) {
spawnBullet(player.pos.add(-5,0))
action('bullet', (b) => {
b.move(-200, 0)
})
}
}
keyPress('space', () => {
if (ammo.value > 0) {
shootLeft()
shootRight()
ammo.value -= 1
ammo.text = "Ammo: " + ammo.value
}
})
// This is the code for the sprite change
keyDown('right', () => {
playerLeft = false
playerRight = true
player.move(100, 0)
player.changeSprite('player-right')
player.scale = (.8)
})
keyDown('left', () => {
playerRight = false
playerLeft = true
player.move(-100, 0)
player.changeSprite('player-left')
player.scale = (.8)
})
Issue 2: I have melee based enemies that are supposed to move back and forth between certain objects. When they collide with an object with the tag ‘edge’ they are supposed to change directions and go the other way, however what happens is either they do it only once then freeze at the next edge, or they freeze at the first edge they hit. Here is the code as it stands:
function goLeft() {
action('enemy-knife-left', (e) => {
e.move(-15, 0)
})
}
function goRight() {
action('enemy-knife-right', (e) => {
e.move(15, 0)
})
}
action('enemy-knife-left', (e) => {
e.move(-15,0)
})
action('enemy-knife-right', (e) => {
e.move(15,0)
})
collides('enemy-knife-left', 'edge', (k,e) => {
k.changeSprite('enemy-knife-right')
goRight()
})
collides('enemy-knife-right', 'edge', (k,e) => {
k.changeSprite('enemy-knife-left')
goLeft()
})
Any help would be greatly appreciated. I will link the game with its current issues so you can check out the issues firsthand.