JavaScript Freeze Tag Game Dev 2 Help!

Hello! Anytime I try to click on the archers they won’t freeze. Please help! My code is down Below >:)

// Let's make a game of Freeze Tag!
// game.tagged is used to count tagged archers.
game.tagged = 0;
ui.track(game, "tagged");
var goal = game.addManualGoal("Tag all archers.");
// Spawn the archers.
game.spawnXY("archer", 12, 52);
game.spawnXY("archer", 12, 16);
game.spawnXY("archer", 24, 52);
game.spawnXY("archer", 24, 16);
var player = game.spawnPlayerXY('captain', 68, 24);
player.maxSpeed = 40;
// Make the player bigger so it's easier to tag archers.
player.scale = 2;
// Set up the archers' speed and behavior properties onSpawn
function onSpawn(event) {
    var unit = event.target;
    unit.behavior = "Scampers";
    unit.maxSpeed = 8;
}
game.setActionFor("archer", "spawn", onSpawn);
// The event handler for "collide" events.
function onCollide(event) {
    // The event owner who has collided with something.
    var unit = event.target;
    // The object the unit collided with.
    var other = event.other;
    // Use behavior as a marker for the current frozen state.
    // "Scampers" means the archer wasn't yet tagged.
    if (unit.behavior == "Scampers") {
        // If "other" is the player.
        if (event.other == "player") {
            // Set unit.behavior to "Defends":
            unit.behavior = "Defends";
            // Increase game.tagged by 1:
            game.tagged += 1;
        }
    }
    if (unit.behavior == "Defends") {
        // If other's type is "archer":
        if (other.type == "archer") {
            // Set unit.behavior to "Scampers":
            unit.behavior = "Scampers";
        }
        // Reduce game.tagged by 1.
        game.tagged -= 1;
    }
}
// }
// Use setActionFor to assign onCollide to the "collide" event for "archer"s.
game.setActionFor("archer", "collide", onCollide);
while (true) {
    if (game.tagged >= 4) {
        game.setGoalState(goal, true);
    }
}

Thanks so much! -Amira

1 Like

Here is your small mistake. Compare with the object player instead of a string as you check a game object, not a type.

Also, there is a mistake with game.tagged -= 1; and its placement. I think you can easily find the solutions.

2 Likes

I tried doing that, but for some reason, when I clicked the archers, it did negatives.

I don’t exactly know how to fix it. Thanks though! :slight_smile:

1 Like

Yes, check WHERE/WHEN you are reducing game.tagged – in what the scope (curly brackets)

1 Like

? I don’t exactly understand. the game.tagged isn’t in the wrong place I just checked…? or that’s where I meant it to be.

1 Like

As for line – yes the right place, as from the code scopes - nope :slight_smile:

4 Likes

Ohhh!! It worked, thank you!!

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