Forums

ForumsProgramming Forum

Problem?

Posted Nov 19, '13 at 2:27pm

alex57834

alex57834

89 posts

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");
}

}

}

 

Posted Nov 19, '13 at 5:48pm

weirdlike

weirdlike

1,070 posts

Moderator

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");
}

}

}


I think this will work I did not test

 

Posted Nov 24, '13 at 1:11pm

alex57834

alex57834

89 posts

yeah working thanks wierdlike

 

Posted Nov 24, '13 at 1:32pm

alex57834

alex57834

89 posts

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.

 

Posted Nov 24, '13 at 1:36pm

alex57834

alex57834

89 posts

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?

Sorry for the confusing other reply.

 

Posted Nov 24, '13 at 2:55pm

weirdlike

weirdlike

1,070 posts

Moderator

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

 

Posted Nov 25, '13 at 2:39pm

alex57834

alex57834

89 posts

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);

}

}

}

 

Posted Nov 25, '13 at 2:56pm

weirdlike

weirdlike

1,070 posts

Moderator

private function myKeyDown(Event:KeyboardEvent):void
{
if (Event.keyCode == Keyboard.LEFT)
{
trace("This is working");
this.x -= 5;
}
}

avatar class cant look at itself

 
Reply to Problem?

You must be logged in to post a reply!