getcontext reading error when inheriting the createjs. Sprite class

Asked

Viewed 130 times

6

I started developing a HTML5 game using the library Easeljs. When creating a class called Bot, which will contain the code of the robot used in the game, got the following error:

Uncaught TypeError: Cannot read property 'getContext' of undefined

inserir a descrição da imagem aqui

As I understand it, this error occurs when Javascript runs before the canvas initialized. But that doesn’t seem to be the case (the class instance Bot is created only in the call of handleImageLoad, after the image of Sprite sheet be loaded into the call from init performed at the event onLoad of the HTML body).

Debugging, it seems to me that the mistake occurs when trying to create the SpriteSheet (within the call of initialize). I put the code in Jsfiddle. Can anyone identify where I’m going wrong?

1 answer

2


Pass your image to the Bot, and this error will no longer occur:

function handleImageLoad() {
  oBot = new Bot(oBotImage); // << mude aqui!
  oStage.addChild(oBot);
  oStage.update();
}

Note that, as handleImageLoad is called in the context of the image, passing this has the same effect:

  oBot = new Bot(this);
  • Man, without a shadow of a doubt, that was it (what a silly mistake I was making, huh?). Thank you! :)

  • These things happen... Sometimes we need a rubber duck.

  • Hehe Well around. Thanks again! []s

Browser other questions tagged

You are not signed in. Login or sign up in order to post.