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

[SOLVED] Hunter and Prey Help

here is my code I need help.

# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    coin = hero.findNearestItem
    if coin:
        hero.moveXY(coin.pos.x, coin.pos.y)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    enemy = friend.findNearestEnemy()
    if enemy:
        hero.command(friend, "attack", enemy)
    pass

# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandArcher(archer):
    enemy = friend.findNearestEnemy()
    if enemy > 25:
        hero.command(friend, "attack", enemy)



while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandArcher(friend)
            pass

someone help please?

Line 8 TypeError: Cannot read property ‘x’ of undefined

What is on line 8?
hero.moveXY(coin.pos.x, coin.pos.y)

property x of undefined. So coin is undefined.
you need to define what is coin

coin = hero.findNearestItem?

you need () after item = hero.findNearestItem

i need help on this lvl please here is my code

# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    if coin:
        coin = hero.findItems()
        hero.moveXY(coin.pos.x, coin.pos.y)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold < hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    enemy = soldier.findNearestEnemy
    if enemy:
        hero.command(soldier, "attack", target)
    pass

# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandarcher(archer):
    enemy = archer.findNearest(archer.findenemies)
    if enemy and archer.distanceto(enemy) < 25:
        hero.command(archer, "attack", enemy)
    else:
        hero.command(archer, "move", archer.pos)
while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandArcher(friends)
            pass

and welcome @InnerdWhap to the forum! my issue is that on the bottom line it says command archer is not defined.

Watch out for capitalization…it’ll byte you every time :stuck_out_tongue_winking_eye:

now on line 31 its saying archer has no method findNearest

this line

    enemy = archer.findNearest(archer.findenemies)

You can use archer.findNearestEnemy() there.

Andrei

its saying the same thing but with findNearestEnemy

You forgot a parenthesis (or is it pair of parenthesis :thinking:) at the end of archer.findEnemies()

1 Like

Don’t forget those ().

Andrei

1 Like

same issue here is my current code

# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    if coin:
        coin = hero.findItems()
        hero.moveXY(coin.pos.x, coin.pos.y)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold < hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    enemy = soldier.findNearestEnemy
    if enemy:
        hero.command(soldier, "attack", target)
    pass

# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandarcher(archer):
    enemy = archer.findNearestEnemy(archer.findEnemies())
    if enemy and archer.distanceto(enemy) < 25:
        hero.command(archer, "attack", enemy)
    else:
        hero.command(archer, "move", archer.pos)
while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandarcher(friends)
            pass

need a screen shot? (20char)

You need a () at the end of this. Also, try defining soldier as hero.findNearest(hero.findFriends()) You don’t have it properly defined as a commandable unit, so maybe this will help.

1 Like

Delete this because you do not need to have anything in between the() from findNearestEnemy.

Andrei

1 Like

it still has the same issue

Also, You are telling your soldiers to attack target, which is an undefined variable. Try using enemy instead in this line:

and i forgot to mention that my hero dosent move