Level: Leave It To Cleaver Help (solved)


#1

Hey,

so I can’t seem to beat this level when most others seemed to be relatively easy. I simply get an error every time that says “function cleaveWhenClose() not defined”. The point of the level is to define a function and I feel as if I’ve put in all the code it requires as well as tried to play with it for hours to no avail.

`-- This shows how to define a function called cleaveWhenClose
– The function defines a parameter called target

function cleaveWhenClose(target)

if hero:distanceTo(target) < 5 then
     -- Put your attack code here
     -- If cleave is ready, then cleave target
    if hero:isReady("cleave") then
        hero:cleave(target)
     -- else, just attack target!
    else 
        hero:attack(target)  
    end
end

end
– This code is not part of the function.
while true do
local enemy = hero:findNearestEnemy()
if enemy then
– Note that inside cleaveWhenClose, we refer to the enemy as target.
cleaveWhenClose(enemy)
end
end
`
This is my code. Idk if it formatted correctly.


#2

You need to scope your functions like you do variables.

Try:

local function cleaveWhenClose(target)

#3

Thanks! This has been an issue for me for variables and I had thought about it for the function but just in a weird, roundabout way. This solved it for me


#4

What would this look like in java script?


#5

If you need help with the level, copy paste your code and surround it top and bottom with 3 backticks ( ` )


#6
if(hero.distanceTo(target) < 5) {
        if(hero.distanceTo(enemy) < 5) {
        var ready = hero.isReady("cleave");
        hero.cleave(enemy); }                              
        
        else {
            hero.attack(enemy);
        }
    }
}

“I didn’t add the game page instructions.”


#7

Did you define target or enemy in your code?


#8

No, lol I Can’t believe I missed that, Thanks.


#9

what deos it translate to in python?


#10

i need help with the programe


#11

I think there is a laguage trasnlator