Game Development 2 (5. Chokepoint)

What did I do wrong? when i clicked on test level nothing happened.

function defendLane(event) {
// Remember to create a variable for the target, to remember:
var unit = event.target;
// Save the unit’s starting pos.x
var startX = unit.pos.x;
while(true) {
var enemy = unit.findNearestEnemy();
// If there is an enemy, use unit.attack to attack!
if (Enemy)
unit.attack(Enemy);
Else;
unit.moveXY(startX, 16); // ∆

}

}

game.spawnXY(“soldier”, 9, 16);
game.spawnXY(“soldier”, 30, 16);
game.spawnXY(“soldier”, 54, 16);
game.spawnXY(“soldier”, 75, 16);

game.setActionFor(“soldiers”, “spawn”, defendLane);

With the code below, only one of the soldier move the other 3 stay still.

function defendLane(event) {
// Remember to create a variable for the target, to remember:
var unit = event.target;
// Save the unit’s starting pos.x
var startX = unit.pos.x;
while(true) {
var enemy = unit.findNearestEnemy();
// If there is an enemy, use unit.attack to attack!
if (enemy)
unit.attack(enemy);
Else;
unit.moveXY(startX, 16); // ∆

}

}

game.spawnXY(“soldier”, 9, 16);
game.spawnXY(“soldier”, 30, 16);
game.spawnXY(“soldier”, 54, 16);
game.spawnXY(“soldier”, 75, 16);

game.setActionFor(“soldier”, “spawn”, defendLane);

1 Like

Try pasting your code from the game (not just copied from here) using the </> button. Then people can see the formatting and help you. If you read the link, it tells you how to paste your code from the game and retain the formatting.

https://discourse.codecombat.com/faq

Also, are you getting an error message? What is happening when you run the code? Please provide some information and then people can assist you.

1 Like

I think it may have something to do with your capitalization? Variable enemy is not supposed to be capitalized and neither is else, I believe.

This is just at first glance so I may be missing out on some details, and also, I am not fluent in JavaScript so some of my advice may be faulty.

1 Like

You are right, it is supposed to be lowercase.

1 Like