So I tried the following code in the level in the title, and it didn’t work, can someone please help me? I thought I could use this to single out only gold and silver coins, but my hero isn’t moving at all
# Be the first to 100 gold!
# If you are defeated, you will respawn at 67% gold.
while True:
items = hero.findItems()
enemy = hero.findNearestEnemy()
index = 0
enemyDistance = hero.distanceTo(enemy)
if enemy and enemyDistance < 10:
hero.electrocute(enemy)
hero.attack(enemy)
itemValue = items[index].value
hero.say(itemValue)
#Says Item Value
if items and itemValue >= 2: <------ So for the reasons commented, I concluded this is the problem
hero.say(items[index].value)
#Doesn't Say Item value
hero.moveXY(items[index].pos.x, items[index].pos.y)
#Doesn't Move
index = index + 1
So did I write something wrong in the condition, I get no errors, only the hero standing still
There are a few issues with this. Firstly, you need another loop if you want to loop through the coins. At the moment you’re setting index to 0 at the start of the loop, then adding 1 to it at the end, but straight after that the loop will repeat and index will go back to 0. You need to add a while loop like you do in the desert coin levels(e.g. Madmaxer levels). (If you’ve done them).
The reason this bit isn’t working:
must be because items[0] (which is what index always is in your code) must be either bronze or silver (value < 3), so the code never runs.
Danny
Oh, thanks so much, I didn’t notice, so now if he cant find a siler or gold coin, he stand still, should I make him move around in circles to find coins or use flags?
on coins to transfer it from an array of items(which, in this case will only contain coins), to the nearest one, and then place an
if coin:
statement whenever you are picking up the coin or finding its value, so your code will never throw the error “cannot read property x of null,” nor the same for the property y or the property value.
Also, for this:
it will not move, because you are asking it to move to the x and y position of an array of multiple items, which this action is not performed, as this is impossible, nor will it say the value of such array, because you are asking it the value of items[index], which;
a. is an array length, and thus just a number
b. defined as 0 at the start of each loop through the while True loop you are using, because you still have the
in your code.
@Ankh_Coding In short, there are several errors remaining unfixed, even in your new code.
Hope this helps!
So findNearest(coins) finds the nearest from the array, but that isn’t what I wanted, I wanted it to cycle through the array so that it doesn’t get stuck on a bronze coin