KaboomJS collides function error

I’m following along on this tutorial 🎮 Easy JavaScript Game Development with Kaboom.js (Mario, Zelda, and Space Invaders) - Full Course - YouTube in the Space Invaders section.

After adding the collides() function and running through Replit, it gives this error:

My code is exactly as in the tutorial, although that video may be outdated already because some other things haven’t worked which I’ve had to consult the Kaboom documentation to fix. However on this, I’m using it the same way the docs describes.

collides("alien", "right-wall", () => {
  CURRENT_SPEED = -ALIEN_SPEED;
  every("alien", (s) => {
    s.move(0, LEVEL_DOWN)
  })
});

Does anyone know what this issue is?

Hello! Welcome to the community :grin:!

Can you share your repl.it?

You need to specify which object is colliding with and add the area to the object, which is the one that actually provides the collides function. If the tagged object doesn’t have an area it won’t work:

I forked and modified your repl.it. Though it’s still not working you should be able to fix it :slight_smile:.

Just for reference, click here to review the Kaboom Intro.

I did see most of the examples had that method bound to the object. But there’s also this so it seemed that it could be used both ways:

https://kaboomjs.com/#collides

Is that part of the documentation no longer valid?

It’s valid, but your aliens where not tagged nor applied the area method, hence the error.

Kaboom refers to tagged as adding a label to the add method:

add([
  // Other properties
  area(), // This is required for 'collides' function
  "TheObjectTag"
]);

From the Kaboom Intro:

Any game object can have any number of tags, they’re kinda like components but much more light weight. We often use tags to quickly describe behaviors for a group of objects.

My tags are in the config of the addLevel method:
addLevel([

{
“^” : () => [sprite(“alien”), scale(1), “alien”],
“!” : () => [sprite(“wall”), “left-wall”],
“&” : () => [sprite(“wall”), “right-wall”],
})

Am I doing that part of it incorrectly?

No, they’re OK, but you’re still missing the area() function :slight_smile:.

Looks like it’s working now. At least to the point I have it so far. It’s a bit frustrating that the tutorial I’m following appears to be out of date, but I’m learning a lot trying to update the code.
I really appreciate your assistance. Thank you.

1 Like