Getting this error: Document Class Working TypeError: Error #1009: Cannot access a property or method of a null object reference. at avatar() at Document() and make the answer sort of simple to understand please,
package { import flash.display.MovieClip; public class Document extends MovieClip { var avatarGuy:avatar;
public function Document() { trace("Document Class Working" var avatarGuy:avatar = new avatar(); avatarGuy.x = 20; avatarGuy.y = 90; addChild(avatarGuy); }
} }
package { import flash.display.MovieClip; import flash.events.Event; import flash.events.KeyboardEvent; import flash.ui.Keyboard; public class avatar extends MovieClip {
public function avatar():void { stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown); } private function myKeyDown(Event:KeyboardEvent):void { trace("key press" }
looks like you declare the variable 2 times, maybe not the issue I think the issue is actually using the stage in your avatar class without importing it
package { import flash.display.MovieClip; public class Document extends MovieClip { //declare var here var avatarGuy:avatar;
public function Document() { trace("Document Class Working" //change to this avatarGuy = new avatar(stage); avatarGuy.x = 20; avatarGuy.y = 90; addChild(avatarGuy); }
} }
package { import flash.display.MovieClip; import flash.events.Event; import flash.events.KeyboardEvent; import flash.ui.Keyboard; public class avatar extends MovieClip { //the s public function avatar(stage):void { stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown); } private function myKeyDown(Event:KeyboardEvent):void { trace("key press" }
One more little question. I am sort of following a tutorial on how to make things work and in the myKeyDown function they put trace(e.KeyCode); and it says
avatar.as, Line 17 1120: Access of undefined property e.
Its a tutorial on how to make things move sorry and they put the trace(e.KeyCode); in the myKeyDown function. I followed and it didn't work for me so has as3 changed or was the tutorial wrong?
I suspect the tutorial is correct as there are a few different ways to approach the key down method, so I am going to take a wild guess and go off the above code here
private function myKeyDown(Event:KeyboardEvent):void { trace(Event.KeyCode); }
the event is the triggering argument that is being passed, if you wanted you could change the text in bold to e and it would still work
Another problem, I don't know whats wrong with me, soooo confused. Some how it cant move the avatar becuase C:\\Users\\Alex The Best\\Documents\\My Games\\Platformer\\Classes\\avatar.as, Line 20 1120: Access of undefined property avatarGuy.
I really don't understand how I haven't defined it.
package { import flash.display.MovieClip; import flash.events.Event; import flash.events.KeyboardEvent; import flash.ui.Keyboard; public class avatar extends MovieClip {
public function avatar(stage):void { stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown); } private function myKeyDown(Event:KeyboardEvent):void { if (Event.keyCode == Keyboard.LEFT) { trace("This is working" avatarGuy.x -= 5; } }
} }
package { import flash.display.MovieClip; public class Document extends MovieClip { var avatarGuy:avatar;
public function Document() { trace("Document Class Working" var avatarGuy:avatar = new avatar(stage); avatarGuy.x = 50; avatarGuy.y = 265; addChild(avatarGuy); }