I recently decided to learn Action Script 3, so I tryed to make my own game… Well, I am posting code of my game below (just a part of important code for that case).
So the DocumentClass.as calls Ball.as (the ball can be controled by keyboard, but I didnt put that part of code here) and PadScreen.as. The PadScreen is also a movie clip (linked with PadScreen.as) which contains few Down movie clips. And I wanted that if Ball movie clip and Down movie clip are colliding the Down movie clip should move a little (and that needs to be done in Down.as file, which dont know ball variable). So the document class should give a ball variable to PadScreen and PadScreen should give it to each Down movie clip inside it for what i used simple loop. The problem is that when I run the game I get output like this:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PadScreen()
at DocumentClass()
**TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Down/onPadTick()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()**
The bold code pops up every time timer execute the following code, but non-bold pops just once at start…
public function onPadTick(timerEvent:TimerEvent):void {
if (ball.hitTestObject(this)) {
this.y = this.y + 1;
}
And if I change it like so:
public function onPadTick(timerEvent:TimerEvent):void {
if (this.hitTestObject(this)) {
this.y = this.y + 1;
}
The Output panel stops poping the bold message out… So the “null object reference” in second output paragraph is most likely ball… I have also searched internet and found many of people getting output like this, but for every case the solution seems to be diferent
Below are my .as files
DocumentClass.as
package {
import flash.display.MovieClip;
public class DocumentClass extends MovieClip {
public var ball:Ball;
public var padScreen:PadScreen
public function DocumentClass() {
ball = new Ball ;
addChild(ball);
padScreen = new PadScreen(ball);
addChild(padScreen);
}
}
}
PadScreen.as
package {
import flash.display.MovieClip;
public class PadScreen extends MovieClip {
public var ball:Ball;
public var down:Down;
public function PadScreen(ball:Ball) {
this.ball = ball;
//Loop through children and pass reference to ball:
for (var i:int = 0; i < numChildren; i++) {
(getChildAt(i) as Down).ball = ball;
}
}
}
}
Ball.as
package {
import flash.display.MovieClip;
public class Ball extends MovieClip {
public function Ball() { // the ball is otherwise controlled by keyboard
x=100
y=100
}
}
}
Down.as
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Down extends MovieClip {
public var padTimer:Timer;
public var ball:Ball;
public function Down() {
padTimer = new Timer(10);
padTimer.addEventListener(TimerEvent.TIMER,onPadTick);
padTimer.start();
}
public function onPadTick(timerEvent:TimerEvent):void {
if (ball.hitTestObject(this)) {
this.y = this.y + 1;
}
}
}
}
Thanks for any help