I have three classes: a Main, a Player and a Car.
The main creates a Car and a Player. The Car needs to trace the player's x position. But the code below is not working. I have researched and found that declaring it as static should fix the problem although it is not the best way to do it?
Main:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip
{
public static var _player:Player;
public static var _car:Car;
public function Main()
{
createPlayer();
}
public function createPlayer():void
{
_player= new Player();
_car = new Car();
_player.x = stage.stageWidth / 2;
_player.y = stage.stageHeight / 2;
stage.addChild(_player);
stage.addChild(_car);
}
}
}
Car:
package {
import flash.display.MovieClip;
import Main;
public class Player extends MovieClip
{
public function Player()
{
trace(Main._car.x);
}
}
}
But this causes an error: TypeError: Error #1009: Cannot access a property or method of a null object reference. at Player() at Main/createPlayer() at Main()
I want to know why this is giving me an error and what are the other good ways to do this?
//EDIT: Sorry _hero was my mistake, it is actually _car. Thank You Very Much...........