Ask coding questions

← Back to all posts
cannot read width of undefined
EduardFaus (3)

I have been working on implementing an ai to flappy bird. I decided to try to create a sort of algorithm like neat from scratch in my code. For some reason everytime i reach the second generation and the birds get replaced with the new generation I get an error.

The console tells me " TypeError: Cannot read property 'width' of undefined ".

When opening it with chrome web tools it seems that this error is referring to when I try drawing my players.

What I don't understand is how are they undefined. In code I tried making it so it would console.log when my birds are undefined and it never runs. I even tried adding if statements to my code so it would only run if it was still defined (now removed). However I couldn't get it to work.

I researched the error and got here ( the thing is I don't see anywhere in the code where I redefined a variable.

Does anyone understand why I am getting this error?

Answered by Coder100 (18071) [earned 5 cycles]
View Answer
Coder100 (18071)

u never defined this.birdImg so make sure to do that.