Double Creek Help (Solved)

Hi! I have been stuck on this level for a while and was wondering if you could help me out. My code says “Cannot read property ‘x’ of undefined”

1 Like

your character is looking into an array of items.
item = hero.findItems() won’t work

1 Like

You’ve defined item as an array. Essentially, it’s a list of items, not one particular item with an xy position.

1 Like

Put

if item:

if item:
        hero.moveXY(item.pos.x, item.pos.y)
1 Like

Have you gotten for-loops yet? If you have, you can just put in the following:

bestCoin = None
leastDistance = 999999#these set up variables to use in the for-loop
for item in items:#this defines item as an entry in the 'items' index
    #put something here to find out if this item is the closest
hero.moveXY(bestCoin.pos.x, bestCoin.pos.y)# you should put 'bestCoin' instead of 'item' to insure that you get the 'best coin' instead of the last item looped over in the for-loop.
1 Like

also, if you want to post your code, you can enter it by pressing the ‘</>’ button, and pasting code from the game inside the triple black tick marks ---------> ‘’’
You can also type code, like the following:

While True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.moveXY(enemy.pos.x, hero.pos.y)
        hero.say(enemy.id + ", I challenge you to a " + challengeType + "!")
        initiateChallenge(challengeType)
    else:
        hero.say("Aha! I have defeated you all!")
1 Like

Of course, you could skip the loops (which allow you to create conditions to help collect more gold) and just write the following:
item = hero.findNearestItem()
But where’s the fun in that?! I, personally, prefer to write my own code once in a while, instead of using a pre-prepared function. Who knows? You may find something even better!

1 Like

Oh wait… whatever, same idea.

1 Like

Ok I’ll try that!:blush:

1 Like

I put, “if item:” and runned it but it says “item is not defined”

1 Like

before if item put

item = hero.findNearestItem()

It also says to remove the say() message. So delete line 25.

1 Like

Yay! It worked, thanks for the help!:grin:

1 Like

4prv1

Put Solved so nobody thinks it isn’t solved.

1 Like