# JavaScript Freeze Tag Game Dev 2 Help!

**URL:** <https://discourse.codecombat.com/t/javascript-freeze-tag-game-dev-2-help/27069>\
**Category:** Level Help\
**Created:** [January 28, 2021, 8:10am UTC](https://discourse.codecombat.com/t/javascript-freeze-tag-game-dev-2-help/27069 "2021-01-28T08:10:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amira\_Saadi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/amira_saadi/32/19105_2.png) [@Amira\_Saadi](https://discourse.codecombat.com/u/Amira_Saadi)\
**Post date:** [January 28, 2021, 8:10am UTC](https://discourse.codecombat.com/t/javascript-freeze-tag-game-dev-2-help/27069/1 "2021-01-28T08:10:41Z")

</div>

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

```javascript
// 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

---

<div class="post-metadata">

**Author:** ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)\
**Post date:** [January 28, 2021, 2:20pm UTC](https://discourse.codecombat.com/t/javascript-freeze-tag-game-dev-2-help/27069/2 "2021-01-28T14:20:27Z")

</div>

> [@Amira\_Saadi](#):
>
> `if (event.other == "player") {`

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.

---

<div class="post-metadata">

**Author:** ![Amira\_Saadi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/amira_saadi/32/19105_2.png) [@Amira\_Saadi](https://discourse.codecombat.com/u/Amira_Saadi)\
**Post date:** [January 28, 2021, 4:36pm UTC](https://discourse.codecombat.com/t/javascript-freeze-tag-game-dev-2-help/27069/3 "2021-01-28T16:36:46Z")

</div>

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

 ![Screenshot 2021-01-28 at 8.35.23 AM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/e/9/e93bd1f581a6353af7568d6202456296ababcae3.jpeg)

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

---

<div class="post-metadata">

**Author:** ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)\
**Post date:** [January 28, 2021, 4:38pm UTC](https://discourse.codecombat.com/t/javascript-freeze-tag-game-dev-2-help/27069/4 "2021-01-28T16:38:59Z")

</div>

> [@Amira\_Saadi](#):
>
> I tried doing that, but for some reason, when I clicked the archers, it did negatives.

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

---

<div class="post-metadata">

**Author:** ![Amira\_Saadi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/amira_saadi/32/19105_2.png) [@Amira\_Saadi](https://discourse.codecombat.com/u/Amira_Saadi)\
**Post date:** [January 28, 2021, 5:24pm UTC](https://discourse.codecombat.com/t/javascript-freeze-tag-game-dev-2-help/27069/5 "2021-01-28T17:24:01Z")

</div>

? 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.

---

<div class="post-metadata">

**Author:** ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)\
**Post date:** [January 28, 2021, 5:30pm UTC](https://discourse.codecombat.com/t/javascript-freeze-tag-game-dev-2-help/27069/6 "2021-01-28T17:30:15Z")

</div>

> [@Amira\_Saadi](#):
>
> ```python
> 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;
> }
> }
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![Amira\_Saadi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/amira_saadi/32/19105_2.png) [@Amira\_Saadi](https://discourse.codecombat.com/u/Amira_Saadi)\
**Post date:** [January 28, 2021, 5:47pm UTC](https://discourse.codecombat.com/t/javascript-freeze-tag-game-dev-2-help/27069/7 "2021-01-28T17:47:01Z")

</div>

Ohhh!! It worked, thank you!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [January 29, 2021, 5:47am UTC](https://discourse.codecombat.com/t/javascript-freeze-tag-game-dev-2-help/27069/8 "2021-01-29T05:47:06Z")

</div>

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