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

**URL:** <https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913>\
**Category:** Level Help\
**Created:** [August 7, 2021, 12:06pm UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913 "2021-08-07T12:06:47Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![WhiteLilly32](https://avatars.discourse-cdn.com/v4/letter/w/f19dbf/32.png) [@WhiteLilly32](https://discourse.codecombat.com/u/WhiteLilly32)\
**Post date:** [August 7, 2021, 12:06pm UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/1 "2021-08-07T12:06:48Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![OgreDestroyer130](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ogredestroyer130/32/23236_2.png) [@OgreDestroyer130](https://discourse.codecombat.com/u/OgreDestroyer130)\
**Post date:** [August 7, 2021, 12:37pm UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/2 "2021-08-07T12:37:39Z")

</div>

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

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

👏 👏

---

<div class="post-metadata">

**Author:** ![WhiteLilly32](https://avatars.discourse-cdn.com/v4/letter/w/f19dbf/32.png) [@WhiteLilly32](https://discourse.codecombat.com/u/WhiteLilly32)\
**Post date:** [August 7, 2021, 1:08pm UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/3 "2021-08-07T13:08:28Z")

</div>

Thank you for the welcome message 🙂

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [August 7, 2021, 4:40pm UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/4 "2021-08-07T16:40:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RangerGrant9307](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/rangergrant9307/32/26001_2.png) [@RangerGrant9307](https://discourse.codecombat.com/u/RangerGrant9307)\
**Post date:** [August 7, 2021, 4:59pm UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/5 "2021-08-07T16:59:38Z")

</div>

I… Only… Do… Python… 😢

---

<div class="post-metadata">

**Author:** ![riticmaster908](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/riticmaster908/32/43503_2.png) [@riticmaster908](https://discourse.codecombat.com/u/riticmaster908)\
**Post date:** [August 7, 2021, 8:04pm UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/6 "2021-08-07T20:04:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![riticmaster908](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/riticmaster908/32/43503_2.png) [@riticmaster908](https://discourse.codecombat.com/u/riticmaster908)\
**Post date:** [August 7, 2021, 8:05pm UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/7 "2021-08-07T20:05:54Z")

</div>

oh wait this level Ive only compleated this level in python

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [August 7, 2021, 8:26pm UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/8 "2021-08-07T20:26:26Z")

</div>

Do you do Java or JavaSript?

---

<div class="post-metadata">

**Author:** ![riticmaster908](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/riticmaster908/32/43503_2.png) [@riticmaster908](https://discourse.codecombat.com/u/riticmaster908)\
**Post date:** [August 7, 2021, 8:31pm UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/9 "2021-08-07T20:31:49Z")

</div>

JavaScript

---

<div class="post-metadata">

**Author:** ![Hydrobolic](https://avatars.discourse-cdn.com/v4/letter/h/e47c2d/32.png) [@Hydrobolic](https://discourse.codecombat.com/u/Hydrobolic)\
**Post date:** [August 8, 2021, 12:59am UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/10 "2021-08-08T00:59:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![WhiteLilly32](https://avatars.discourse-cdn.com/v4/letter/w/f19dbf/32.png) [@WhiteLilly32](https://discourse.codecombat.com/u/WhiteLilly32)\
**Post date:** [August 8, 2021, 1:28am UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/11 "2021-08-08T01:28:38Z")

</div>

```python
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.

---

<div class="post-metadata">

**Author:** ![Hydrobolic](https://avatars.discourse-cdn.com/v4/letter/h/e47c2d/32.png) [@Hydrobolic](https://discourse.codecombat.com/u/Hydrobolic)\
**Post date:** [August 8, 2021, 2:54am UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/12 "2021-08-08T02:54:30Z")

</div>

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>"`?

---

<div class="post-metadata">

**Author:** ![WhiteLilly32](https://avatars.discourse-cdn.com/v4/letter/w/f19dbf/32.png) [@WhiteLilly32](https://discourse.codecombat.com/u/WhiteLilly32)\
**Post date:** [August 8, 2021, 9:18am UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/13 "2021-08-08T09:18:18Z")

</div>

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

 ![Coding](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/8/1/81053d621dd51a4186c12e5087742db822e32120.jpeg)  
 ![Minus Numbers](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/9/9/99226bf5485da8bc4186b0f86ec9b95ff2210bb7.jpeg)

---

<div class="post-metadata">

**Author:** ![Hydrobolic](https://avatars.discourse-cdn.com/v4/letter/h/e47c2d/32.png) [@Hydrobolic](https://discourse.codecombat.com/u/Hydrobolic)\
**Post date:** [August 8, 2021, 6:00pm UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/14 "2021-08-08T18:00:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![WhiteLilly32](https://avatars.discourse-cdn.com/v4/letter/w/f19dbf/32.png) [@WhiteLilly32](https://discourse.codecombat.com/u/WhiteLilly32)\
**Post date:** [August 8, 2021, 10:43pm UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/15 "2021-08-08T22:43:58Z")

</div>

Thank you for helping me finish this level Hydrobolic, couldn’t have done it without 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:** [August 9, 2021, 10:44am UTC](https://discourse.codecombat.com/t/unable-to-finish-freeze-tag-game-development-2-javascript-solved/29913/16 "2021-08-09T10:44:22Z")

</div>

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