Hi community!
I got stuck on the Spinach Power level and after scratching my head for a day I have no idea what’s wrong with the code:
local potCount = 0
local enemy = hero:findNearest(hero:findEnemies())
while true do
local item = hero:findNearest(hero:findItems())
if item then
hero:move(item.pos)
local potCount = potCount + 1
if potCount == 7 and enemy then
hero:attack(enemy)
end
end
end
The problem is my hero instead of getting the 7 potions, poke one of the ogres and then resumes the potion collecting. It quite leaves me speechless since it seems she doesn’t care about the “if potCount == 7 and enemy then” line.
ok so this is for 2 levels after spinach power but my code is right because when I sub out the “[1]”
for another number it works.
// One gem is safe, the others are bombs.
// But you know the answer: always take the second.
while (true) {
var items = hero.findItems();
// If the length of items is greater or equal to 2:
if(items.length >= 2){
hero.moveXY(items[1].pos.x, items[1].pos.y);
}
// Move to the second item in items
// Else:
else {
hero.moveXY(40, 33);
}
// Move to the center mark.
}