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”
your character is looking into an array of items.
item = hero.findItems() won’t work
You’ve defined item as an array. Essentially, it’s a list of items, not one particular item with an xy position.
Put
if item:
if item:
hero.moveXY(item.pos.x, item.pos.y)
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.
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!")
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!
Oh wait… whatever, same idea.
Ok I’ll try that!
before if item put
item = hero.findNearestItem()
It also says to remove the say()
message. So delete line 25.
Yay! It worked, thanks for the help!
Put Solved so nobody thinks it isn’t solved.