[Kaboom] Enemies are only moving right

Question:
In my Kaboom.js game, the enemies are supposed to spawn moving in random directions. However, they are only moving right? What’s wrong?

Repl link:
https://replit.com/@element1010/Battle?v=1#code/main.js
https://battle.element1010.repl.co

function spawnEnemy() {
    let num = rand(3);
    let decdir = "right";
    switch(num) {
      case 0:
        decdir = "left";
        break;
      case 1:
        decdir = "right";
        break;
      case 2:
        decdir = "up";
        break;
      case 3:
        decdir = "down";
        break;
    }
    add([
      sprite("enemy"),
      pos(rand(width()), rand(height())),
      area(),
      "enemy",
      {
        direction: decdir,
        frozen: false,
        burning: false,
        health: 20
      }
    ]);
    wait(5, spawnEnemy);
}
onUpdate("enemy", (enemy) => {
    switch(enemy.direction) {
      case "left":
        enemy.move(-ENEMY_SPEED, 0);
        break;
      case "right":
        enemy.move(ENEMY_SPEED, 0);
        break;
      case "up":
        enemy.move(0, -ENEMY_SPEED);
        break;
      case "down":
        enemy.move(0, ENEMY_SPEED);
        break;
    }
});
1 Like

Not an expert in this but if this statement set the direction and decdir is always equal to right , I guess this is why.

1 Like

also shouldn’t ran() be 4 not three?

But there is a switch statement right before that randomly changes the decdir!

did you change the ran() to four yet?

I think rand(3) can generate 0, 1, 2, or 3.

I thought when you call rand(3) , it will generate a random number that can be 0, 1, or 2…

I found the error. When testing it on the KaboomJS Playground, I realized that rand(3) was generating numbers like 1.5615234375, which doesn’t match any of the cases specified.

1 Like

Awesome! Did you check if it would generate the number 4 as well, or did you have to change the 3 to a 4

It didn’t generate the number 4, but I didn’t need to.

sorry, (did it generate 0, 1, and 2, or 0, 1, 2, and 3)

True, not sure what language this is, but in many languages you should not be needed an assignment before, and that confused me.

If you want the functionality you expected, try a function like this:

function randInt(min, max = null) {
	if (max !== null) {
		if (min > max) {
			[ min, max ] = [ max, min ];
		}
		return Math.round(Math.random() * (max - min) + min);
	}
	return Math.round(Math.random() * min);
}
// randomInt(x); returns random integer between 0 and x (inclusive)
// randomInt(x, y); returns random integer between x and y (inclusive)
1 Like

What if x is negative? (I know it wouldn’t be in this example, but I like handling edge cases :upside_down_face:)

1 Like

Lol, handling edge cases is always a good idea, I’m pretty sure everything should work fine with negatives though…

I wasn’t sure since I saw the min/max filp, so I figured I’d check.

Well, this isn’t pure JS, this is kaboomjs, and I can easily fix this using: Math.floor.

You can use randi(num) Kaboom function

3 Likes

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