Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Can't read property of position

Why it’s doesn’t work??? I

t’s a bug?

Level is usual day . It’s in Backwoods Forest

while (true) {
    var enemy = hero.findNearestEnemy();
    // With AND, the type is only checked if enemy exists.
    if (enemy && enemy.type == "munchkin") {
        hero.attack(enemy);
    }
    // Find the nearest item.
    let item = hero.findNearestItem();
    let pos = item.pos;
    let posX = pos.x;
    let posY = pos.y;
    // Collect item if it exists and its type is "coin".
    if(item && item.type == "coin"){
     hero.moveXY(posX, posY);   
    }
}

I don’t know JavaScript but you need to post your code and tell us what level this is.

I’m posted my code. level is usual day It’s in Backwoods Forest

he did its called usual day

I think I did this level but now that I accidentally linked my main account to a student account I cant help. Sorry :sob:

I think you have to check if there is an item first.
Lydia

I have done this level but I cant check it anymore :sob:

try refreshing the page @Falcons118 that should do the the trick or close the tab and reopen it

@milton.jinich no thats not the reason. Its because I joined a class do you see the back to classroom button. But I am emailing the support right now to unlink my account from the classroom

okay try that if that doesn’t work than I’ll try and PM you about this

The same code . But it’s code is working

while (true) {
    var item = hero.findNearestItem();
    var distance = hero.distanceTo(item);
    // If the item's type is "gem"
    // OR the distance to the item less than 20 meters:
    let pos = item.pos;
    let x = pos.x;
    let y = pos.y;
    if(item.type == "gem" || distance < 20){
        // Move to item's position.
        hero.moveXY(x, y);
    }
}

It might take them a while to respond to the email because they work on monday to friday.
But this is getting off topic so its fine i will figure it out later

Can you delete your code if it is a sulution please

i deleted my code. But i think it’s a BUG.

It is not a bug, if you can pass the level with no errors, it is not a bug.
Lydia

You must almost copy the code given in the default code:

while (true) {
    let enemy = hero.findNearestEnemy();
    // With AND, the type is only checked if enemy exists.
    if (enemy && enemy.type == "munchkin") {
        hero.attack(enemy);
    }
    // Find the nearest item.
    let item = hero.findNearestItem();
    // Collect item if it exists and its type is "coin".
    if (item &&  item.type == "coin")
        // put here your code for moving to the item
       //  use moveXY or move() if you have more advanced boots 
}

But the most important lesson from this code is that
@Deadpool198, @jka2706 and so on…
We can use now let and probably other a little bit more advanced features of java script without superfluous and annoying warnings and false errors

1 Like