Domanda

I don't know why I'm getting this error in AS3, "Access of undefined property Block.".

I was following a tutorial and copied the code as it is,

function mainLoop (e:Event) {
   for (var i = 0; i < numChildren; i++){
   if (getChildAt(i) is Block)
   {
      var bb = getChildAt(i) as Block;
      if (bb.hitTestPoint(mouseX, mouseY))
      {
      trace("hit"); 
      }
   }
   }    
}

Maybe a missing import? Although FlashDevelop didn't automatically add as it does sometimes so I'm a bit clueless

È stato utile?

Soluzione

You should have class definition for Block. It could be exported as part of .swc library or defined in code. Without definition of Block class of cause you will have errors.

Also you could improve your main loop, you shouldn't perform several times same lookup with getChildAt and operations is and as also could be optimised:

function mainLoop(e:Event) {
    var block:Block, len:int = numChildren, i:int;
    for (i = 0; i < len; i++) {
        block = getChildAt(i) as Block;
        if (block != null) {
            if (block.hitTestPoint(mouseX, mouseY)) {
                trace("hit");
            }
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top