actionscript 3 - Object is a null reference only when using a method? -


i have object called "target" property called "movement":

private function start() { var movement:imovement = new zigzagmovement(target); target.movement = movement; mediumdifficulty(); }  private function mediumdifficulty(target:abstracttarget):void {              var movement:imovement = target.movement;             movement.mediummove();         } 

my abstract target class looks this:

package  {     import flash.display.sprite;      public class abstracttarget extends sprite     {         protected var __movement:imovement;          public function abstracttarget()         {         }          public function set movement(value:imovement):void         {             __movement = value;         }          public function movement():imovement         {             return __movement;         }       } } 

my "movement" class looks this:

package {     import flash.events.event;      public class zigzagmovement implements imovement         {             private var __target:object;              public function zigzagmovement(target:object)             {                 __target = target;             }                public function mediummove():void             {                 // movement code             }           }     } 

the weird thing when trace "target" , "target.movement" looking for...but if try say, write:

target.movement.mediummove(); 

or

movement.mediummove();

then both target , "movement" (local variable) trace out null??

i think need more information. there things i'm not seeing in code sample or explanation:

  • what concrete object you're passing mediumdifficulty function? not zigzagmovement, movement member variable of abstracttarget.
  • when instantiating concrete subclass of abstracttarget? specific subclass of abstracttarget? didn't post code it. sure has instantiated movement object time it's passed mediumdifficulty function?

it sounds what's happening abstracttarget subclass you're testing in isolation has imovement, not 1 you're passing mediumdifficulty function, without more information it's difficult say.

(setting community wiki in case i'm missing something)

edit: new information --

it appears have class-level variable called "target" you're assigning movement to, , have param named "target" in mediumdifficulty() function signature, , you're not passing class-level target object function. results in when mediumdifficulty tries ascertain value of target, it's null because it's looking @ param , not class-level variable. that's possibly how can manage trace target , target.movement in start() function not values in mediumdifficulty.

try in future have function params or function scoped variables not share names class-scoped vars cut down on kind of confusion.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -