Error reading 'getContext' when inheriting class 'createjs.Sprite'

6

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

Uncaught TypeError: Cannot read property 'getContext' of undefined

AsfarasIunderstand,thiserroroccurswhenJavaScriptexecutesbeforethecanvashasbeeninitialized.Butthisdoesnotseemtobethecase(theinstanceofclassBotiscreatedonlyinthehandleImageLoadcall,afterthespritesheetimageisloadedintotheinitcallmadeintheeventonLoadofthebodyoftheHTML).

Debugging,itseemstomethattheerroroccurswhentryingtocreatetheSpriteSheet(withintheinitializecall).Iputthe code in JSFiddle . Can anyone identify where I'm going wrong?

    
asked by anonymous 11.01.2014 / 17:29

1 answer

2

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

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

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

  oBot = new Bot(this);
    
11.01.2014 / 17:40