Double Creek Help (Solved)


#1

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”


#2

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


#3

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


#4

Put

if item:

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

#5

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.

#6

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!")

#7

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!


#8

Oh wait… whatever, same idea.


#9

Ok I’ll try that!:blush:


#10

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


#11

before if item put

item = hero.findNearestItem()

#12

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


#13

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


#14

4prv1


#15

Put Solved so nobody thinks it isn’t solved.