Cannot read property 'value' of undefined


#1

Before submitting to gitHub, I’d like to check first here. I decided to make two helper functions

function walkTo(positioned) {
    hero.moveXY(positioned.pos.x, positioned.pos.y);
}
function ifCollect() {
    var item = hero.findNearestItem();
    if (item) 
        walkTo(item);    
}

The first works directly in program, however the second one - ifCollect - raises a strange error on the “walkTo(item)” line:
Cannot read property ‘value’ of undefined

I’m pretty sure this looks like a bug but I’d like to check first.


#2

do

  if (item) {
        walkTo(item); }

#3

Thanky you, but this obviously does not help (I really tried, just to check) - JavaScript like all C-like languages expects a command after if, and if it is a single command, it does not require braces.


#4

function walkTo(positioned) {
hero.moveXY(positioned.pos.x, positioned.pos.y);
} // why tou need it?

function ifCollect() {
var coin = hero.findNearestItem();
if (coin )
hero.move(coin.pos); //or hero.moveXY(coin.pos.x, coin.pos.y) );
}


#5
  1. Is that important? This is a common language feature that is not working.
  2. It simplifies things - you simply write that function once, include into your library and use it for flag collection, coins collenction etc. etc.