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

Trouble with Freeze tag

I’m having trouble with freeze tag. Here’s my code:

game.tagged = 0
ui.track(game, "tagged")
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)

player = game.spawnPlayerXY('captain', 68, 24)
player.maxSpeed = 30
# Make the player bigger so it's easier to tag archers.
player.scale = 2

# Set up the archers' speed and behavior properties onSpawn
def onSpawn(event):
    unit = event.target
    unit.behavior = "Scampers"
    unit.maxSpeed = 8

game.setActionFor("archer", "spawn", onSpawn)

# The event handler for "collide" events.
def onCollide(event):
    # The event owner who has collided with something.
    unit = event.target
    # The object the unit collided with.
    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
        pass
    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)

I’m using setActionFor at the end so it should be fine. Can someone please help me? Thanks.

1 Like

Can you send the link to the level along with the world the level is in? I would like to test your code…

1 Like

It’s in the game dev 2 world. Here’s the link:

https://codecombat.com/play/level/freeze-tag?

Most of the code is already put in. The only part where I put in the code was where the player was “colliding” with the archers.

Ok so I tested it.
Now I haven’t done a lot of web development so forgive me if I am wrong.

It looks like your hero is the one who is not moving. I may be wrong, but you don’t have any lines of code that tell your hero to move. That I think is what is wrong…

My hero is moving. You have to click “play.” She’s going to move where you click your mouse. When the hero collides, the archers have to “defend,” but they aren’t.

I think I see your problem. On this line you said if other type is archer reduce the score one point on this line:

You said if other type is player increase the score by one point
This isn’t working because there are no other players in the game…

You might want to set the decrease to an object if the game allows it like a tree.

The increase should be set to archer…

1 Like

It works! thanks! (20 characters)

You are welcome!
Hey there should be a little :white_check_mark: button where the options to like a post, share, and edit are. Could you press it on my post that helped you fix your code? It shows moderators who I helped and who I haven’t. Good job again!!

1 Like

Thank you! Have a good day :sun_with_face: