Code:
function pickUpCoin() {
var coin = hero.findNearestItem();
if(coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}
// Write the attackEnemy function below.
// Find the nearest enemy and attack them if they exist!
var enemy = hero.findNearestEnemy();
function attackEnemy() {
if (enemy) {
hero.attack(enemy);
}
}
while(true) {
//attackEnemy(); // ∆ Uncomment this line after you write an attackEnemy function.
pickUpCoin();
attackEnemy();
}
1 Like
Hi! Could you please format your code using the </> in the message? It will be easier to understand the code.
1 Like
I found a problem though. You should put enemy=hero.findNearestEnemy();
inside the function. Otherwise, it will determine enemy only once.
2 Likes
Aya
August 26, 2022, 6:50am
4
Yup i agree, and dont forget to uncomment this line once you fix what Peter said
Turnerkid:
//attackEnemy();
2 Likes
Here’s my new code:
It’s still not working I think there is another problem.
function pickUpCoin() {
var coin = hero.findNearestItem();
if(coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}
// Write the attackEnemy function below.
// Find the nearest enemy and attack them if they exist!
function attackEnemy() {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
}
}
while(true) {
//attackEnemy(); // ∆ Uncomment this line after you write an attackEnemy function.
pickUpCoin();
}
1 Like
As Aya and the comments said, you should uncomment this (delete //), so the function will work.
2 Likes
I uncommented that line. But it’s still not working.
function pickUpCoin() {
var coin = hero.findNearestItem();
if(coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}
// Write the attackEnemy function below.
// Find the nearest enemy and attack them if they exist!
function attackEnemy() {
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
}
}
while(true) {
pickUpCoin();
}
1 Like
Aya
August 29, 2022, 2:16pm
8
Turnerkid:
uncommented
So basically, when you have working code it is uncommented, but when you want to disable code in javascript, you use //
For example, the code below is in action:
hero.moveXY(item.pos.x, item.pos.y)
hero.say("I picked up an item")
so the result of the code up is that the hero picks up an item you specified and says the statement
Now if you commented the say statement like this:
hero.moveXY(item.pos.x, item.pos.y)
//hero.say("I picked up an item")
you notice the code turned grey in this editor and the hero will just pick up the item without saying anything, meaning you disabled that specific piece of code
Now for the level, you just remove the // from the line where attackEnemy is and you keep attackEnemy
Hint:
while (true) {
attackEnemy();
pickUpCoin();
}
1 Like
It worked but I had to change hero’s because Okar kept getting stuck at the edge of the map. Should I report it in the Bugs Form.
1 Like
ZAX155
August 29, 2022, 2:41pm
10
I would say yes. But I would let @Chaboi_3000 know as well.
Aya
August 29, 2022, 3:38pm
11
Turnerkid:
It worked
Congrats
Turnerkid:
report it in the Bugs
Yup what Zax said
Please dont forget to mark the post with what helped you as a solution so the topic would close :]
1 Like
system
Closed
August 30, 2022, 3:20pm
12
This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.