Anyone familiar with Kaboom.js? Have 2 major issues with a game I'm working on

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.

https://mr-gun-man.the702guy.repl.co/

Hey 702guy! Thanks for trying out kaboom, just saw this post and happy to help.

So action pushes the callback to a list that gets called every frame, you’ll want that to be on the root of a scene, otherwise not only it’ll not work correctly it’ll also drag the performance (so in conclusion, action shouldn’t really be called in any nested situation like branches or recurring functions, each should only be evaluated once throught the scene). So I’d add a dir custom attribute to the bullet for controlling its direction:

action('bullet', (b) => {
	b.move(b.dir * 200, 0)
})

function shootRight() {
  if (playerRight) {
    spawnBullet(player.pos.add(5,0), 1)
  }
}

function shootLeft() {
  if (playerLeft) {
    spawnBullet(player.pos.add(-5,0), -1)
  }
} 

function spawnBullet(p, dir) {
  add([
    rect(5,1), 
    pos(p), 
    origin(vec2(-3, -20)), 
    color(255, 255, 0),
    'bullet',
    { dir: dir },
  ])
}

Also every operates on existing objects, if you want to define behavior for a type of object including the ones added after this call you might want to use action("tag")

1 Like

Thank you very much for the reply. That code change worked, if its not to much of a hassle I had a couple of questions.

The number you added at the end of

spawnBullet(player.pos.add(5,0), 1)

You added the 1 and the -1. What does that number call? I didn’t add it originally and the bullet didn’t shoot. Is that the dir arguement that you then mulitply by 200 for the speed?

Also do you know where I could find an even further detailed technical sheet of some of the very specific Kaboom.js code? Things like ‘on headbutt’ that aren’t on the Kaboom.js website except for in the examples. There are some helpful things I’ve seen in videos that has things like that but they aren’t on the website.

Sorry for the late reply… Didn’t see this.

Yes I added the second argument to specify the direction, stored it in a custom field under the bullet objects and update them based on that.

Some experimental / prone to change stuff are no on the docs, e.g. headbump event (which actually just got renamed to headbutt)

I am making a game and need some help with the code. I want the enemies to shoot. I tried various different things and it is still not working. Can anyone help me out with it

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.