I'm just learning how to use classes in AS3 using some very basic code and I'm tearing my hair out trying to figure out how to do the simplest thing.
I've made the document class the 'Test' class (essentially my main class), and all I'm trying to do with it is add an instance of the 'WhiteBall' class (a movieclip) to the stage.
The 'WhiteBall' class is supposed to allow me to control the movieclip with the keyboard. I don't know if this part works yet, because I keep getting this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference. at WhiteBall$iinit()[/Users/Owner/Desktop/Animation/Coding/WhiteBall.as:13] at Test$iinit()[/Users/Owner/Desktop/Animation/Coding/Test.as:11]
Here is the code for the 'Test' class:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Test extends MovieClip
{
var whiteBall:WhiteBall = new WhiteBall ();
public function Test() {
addEventListener(Event.ENTER_FRAME, whiteBallSpawn);
}
public function whiteBallSpawn(evt:Event) {
stage.addChild(whiteBall);
whiteBall.x = 200;
whiteBall.y = 250;
}
}
}
Here is the code for the 'WhiteBall' class:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class WhiteBall extends MovieClip
{
public function WhiteBall() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, keysdown);
}
public function keysdown(mykey:KeyboardEvent) {
if(mykey.keyCode==Keyboard.UP) {
this.y--;
}
if(mykey.keyCode==Keyboard.DOWN) {
this.y++;
}
if(mykey.keyCode==Keyboard.RIGHT) {
this.x++;
}
if(mykey.keyCode==Keyboard.LEFT) {
this.x--;
}
}
}
}
The line-11 error in the 'Test' class refers to this line:
var whiteBall:WhiteBall = new WhiteBall ();
I have no idea what the problem is here. Any help you could give me would be really appreciated.