Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Please help with Freeze tag

this is my code i dont know what is wrong with it, but I cant pass it

// 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);
}
}

thx for help

1 Like

Please format your code correctly using this: FAQ - Check Before Posting

// 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);
    }
}

type or paste code here

is this ok? I used</>

Yes, now what’s the issue? What is your hero doing wrong?

my hero touches archers but doesnt tags them

Sorry, I don’t code javascript, but is there any error message?

I think I can help here…compare the two unit.behavior statements. You are increasing and decreasing as you should, but you are doing so at different levels.

One of the game.tagged increments needs to be moved.

No there is no error

niedz., 22 mar 2020, 18:01 użytkownik ducky duck via CodeCombat Discourse codecombat@discoursemail.com napisał:

I think dedreous can help you.

Zambi, in your code, you have:

    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;
    }

Notice how the final statement of each code block (game.tagged) don’t line up? They do need to match alignment.

where game.tagged should be move and which +=1 or - =1

Try experimenting…I could tell you out right, but that detracts from the learning experience.

Just to be safe, copy all of your code to Notepad, or other application, so you can always go back to where you started and try again.

A quick not aside from the problem: when you’re formatting javascript, put “javascript” straight away after the first three ```, with no space in between and it will make the comments actually commented out so not all the code is red.
e.g.

    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;
    }

Danny

1 Like

Kewl! I was wondering about that.

1 Like

Hey, I trayed to experimenting but it doesnt work , I have tagged increase till 30
without collide please look at the picture and help me

Ok, starting with your original code:

Original 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 = 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);
    }
}

Let’s name the two code blocks “Scampers” and “Defends”:

// Scampers
    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;
        }
    }
// Defends
    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;
    }

Scampers is perfect…make Defends look like it

Hi, #dedreous I wrote what u wrote me and it doesnt work I show my print screen

But they still don’t match.

// Scampers
    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;
        }
    }
// Defends
    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; //<---------this line is still in the wrong place
    }

You need to make game.tagged -= 1 the final statement of the if (other.type == “archer”) loop. Right now, it is the final statement of the if (unit.behavior == “Defends”) loop.

@ducky @Zambi25 @DevinIV1

this should work

// 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);
    }
}