Unable to finish Freeze Tag(Game Development 2 - Javascript)[SOLVED]

Hello everyone, I’m finding difficulty finishing this level as when my hero collides with an archer, they aren’t tagged and their behaviour doesn’t change. Could you please give me advice on fixing my code?

// 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 = 20;
// 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 (other.type == 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);
    }
}

Hello, @WhiteLilly32! Nice to meet and see you there.

Welcome to our community. You’ll sure have a great time.

:clap: :clap:

3 Likes

Thank you for the welcome message :slight_smile:

3 Likes

Hi and welcome to our family-friendly forum! You can check out the rules of the discourse here .
I’m not this advanced in coding yet, so I can’t help you, but I can summon a few who are. @RangerGrant9307 @Hydrobolic?

2 Likes

I… Only… Do… Python… :cry:

1 Like

I do java just send me the link to the level plz

oh wait this level Ive only compleated this level in python

Do you do Java or JavaSript?

JavaScript

Suppose the hero collides with a unit that is not tagged.

First, your first if-statement will execute as unit.behavior == "Scampers", toggling the behavior to Defends and incrementing game.tagged.

Then, your second if-statement will execute as unit.behavior == "Defends" (which was set in the first if-statement), undoing the effects

This is not what you want! At most one if-statement should be triggered per collision. Try changing the second if to an else if.

if ((unit.behavior == "Scampers") & (other.type == player)) {
        unit.behavior = "Defends";
            // Increase game.tagged by 1:
        game.tagged += 1;
    } else {
        // 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;
        }
    }

I did this and unfortunately, it still didn’t work.

Scratch my earlier reply, which is incorrect — sorry! Both if-statements check the type of who the archer collided with, so both can’t run as I said above. (I don’t have access to the level, so I can’t test my code snippets as I usually would.)

The .type property is usually a string, but you check if other.type == player, comparing it to the player object instead. Compare to previous levels you’ve done; should that be either other === player or other.type === "<some string>"?

No worries about it! Your suggestion did affect the number of tagged. Perhaps, there’s another error because now, it goes into negative numbers.


Sure. Consider what happens if two archers who both have the tag “scampers” collide. You the handling of that case while translating from your original code to the current code.

2 Likes

Thank you for helping me finish this level Hydrobolic, couldn’t have done it without you :grin:

3 Likes

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