Java scripts good code who didn't really work


#1

Hi for the level “A Fine Mint” i made a code for this level.

function pickUpCoin() {
var coin = hero.findNearestItem();
if(coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}

while(true) {
var enemy = hero.findNearestEnemy();

hero.attack(enemy);

pickUpCoin();

}

but the hero take to much time for pick the coin and the level fail and say “Ran out of time”

I don’t know what to do.

If u can help me =) and sorry for my english i’m a french =^-^=

Thanks :*


#2

@Persephone hi I see u play on java script can you help me for my problems?


#3

Please surround your code in 3 backticks (`) above and below your code, so it will format correctly.


#4

@Serg

‘’'function pickUpCoin() {
var coin = hero.findNearestItem();
if(coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}

while(true) {
var enemy = hero.findNearestEnemy();

hero.attack(enemy);

pickUpCoin();

}’’’

like that?


#5

Use ( ` ) instead of ( ’ ).


#6

Oh sorry

var coin = hero.findNearestItem();
if(coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}

while(true) {
var enemy = hero.findNearestEnemy();

hero.attack(enemy);

pickUpCoin();
}```

#7

Almost, gotta use 3 of them above and below.


#8
function pickUpCoin() {
var coin = hero.findNearestItem();
if(coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}

while(true) {
var enemy = hero.findNearestEnemy();

hero.attack(enemy);

pickUpCoin();
}

#9

that’s what I do, but they disapear


#10

@Serg I just want to advance through the levels.
my code works but is not optimal and I have two coins to pick up at the end but ran out of time… So i don’t know what to do…


#11

Hi, when I copy paste your code into the level, it doesn’t work. Are you sure you are copy-pasting correctly?


#12

Sure I’am…

On my code in the part of the "while(true)
for the “hero.attack(enemy);” there is a “play” button on the left and and a blue and white frame around the line of code.
Can I give you a screenshot or something like this?


#13

yes, you can drag a picture into the typing box:


like this


#14

@Tom_H @Serg sorry for loosing your time I retry and now it’s working why? i don’t know but the champion don’t make the same decison O.o

Sorry… :confused:


#15

I was having a problem with opening the full chest, but it is fixed now :grin: