Okay, I have a problem,
I have two classes, one called car, one called game. When the user drives a car off the map, i wish to remove the car from the map, do some fiddling with vars (lifes etc) and then re-add the child.
But i get the following error #1009: Cannot access a property or method of a null object reference… I think it is because the car class is still trying to control the car even though it is being removed…I think.
Anyway as usual im pulling my hair out, going to be very bald very soon, Heres my two class files, no doubt one of you superstars will spot it within seconds, I am so very grateful for this website, im learning things all the time
Car Class
package
{
// IMPORT ANY CLASSES OR LIBRARIES
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Car extends MovieClip
{
// DEFINE ANY VARIABLES OR PROPERTIES
//Keyboard Variables
public var up:Boolean;
public var down:Boolean;
public var left:Boolean;
public var right:Boolean;
//Speed Settings of the car
public var speed:Number = 0;
public var speedMax:Number = 5;
public var speedMaxReverse:Number = -2;
public var speedAcceleration:Number = .3;
public var speedDeceleration:Number = .15;
//Updating the cars position
public var velocityX:Number;
public var velocityY:Number;
public var rotationRate:Number = 0;
public function Car()
{
// CONSTRUCTOR CODE
up = false; // Setting up to false
down = false; // Setting down to false
left = false; // Setting left to false
right = false; // Setting right to false
velocityX = 0; // Setting vX to 0
velocityY = 0; // Setting vY to 0
addEventListener(Event.ENTER_FRAME, loop); // Event Listener for Every Frame.
}
// Function to act Every Frame
public function loop(e:Event)
{
// Listens for keypress;
stage.addEventListener(KeyboardEvent.KEY_DOWN, myOnPress);
// Listens for keyunpress;
stage.addEventListener(KeyboardEvent.KEY_UP, myOnRelease);
// Calls Update Car Pos Function
updatePos();
// Calls Update Car Speed Function
updateSp();
}
// Keyboard Switch Statement For Keypress
public function myOnPress(event:KeyboardEvent)
{
switch (event.keyCode)
{
case Keyboard.UP :
up = true;
this.gotoAndStop(1);
break;
case Keyboard.DOWN :
down = true;
this.gotoAndStop(1);
break;
case Keyboard.LEFT :
left = true;
rotationRate = -10;
this.gotoAndStop(3);
break;
case Keyboard.RIGHT :
right = true;
rotationRate = 10;
this.gotoAndStop(2);
break;
}
event.updateAfterEvent();
}
// Keyboard Switch Statement For Keyunpress
protected function myOnRelease(event:KeyboardEvent)
{
switch (event.keyCode)
{
case Keyboard.UP :
up = false;
break;
case Keyboard.DOWN :
down = false;
break;
case Keyboard.LEFT :
left = false;
rotationRate = 0;
this.gotoAndStop(1);
break;
case Keyboard.RIGHT :
right = false;
rotationRate = 0;
this.gotoAndStop(1);
break;
}
event.updateAfterEvent();
}
// Function that updates Cars Speed Settings
public function updateSp()
{
if (up) // If up is pressed
{
if (speed < speedMax) // If speed is less then speedMax
{
speed = speed + speedAcceleration; // Speed is increased by speedAcceleration
}
}
if (!up && speed > 0) // If up isn't pressed and the car is moving
{
speed = speed - speedDeceleration; // Reduce the speed by speedDeceleration
}
if (down) // If down is pressed
{
if (speed > speedMaxReverse) // If speed is greater than speedMaxReverse
{
speed = speed - speedAcceleration; // Speed is decreased by speedAcceleration
}
}
if (!down && speed < 0) // If down isn't pressed and the car is reversing.
{
// Speed up slightly?
}
}
// Function that updates Cars Position
public function updatePos(){
// Calculate the cosine of the angle, convert to radians and times by -speed.
velocityX = Math.cos ((this.rotation + 90) * Math.PI / 180) * -speed;
// Calculate the sine of the angle, convert to radians and times by -speed.
velocityY = Math.sin ((this.rotation + 90) * Math.PI / 180) * -speed;
// Update Cars X and Y Position Based on the Trigonometry above..
this.x += velocityX; // Car.x is equals to the result of velocityX
this.y += velocityY; // Car.y is equals to the result of velocityY
this.rotation += rotationRate; // Change the Cars rotation based on rotation rate, set within the switch.
}
}// class bracket
}// package bracket
Game (document) class
package
{
// IMPORT ANY CLASSES OR LIBRARIES.
import flash.display.MovieClip;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import ReusableCode.MyMaths;
public class Game extends MovieClip
{
// DEFINE ANY VARIABLES/PROPERTIES.
public var car:Car = new Car();
public var mapContainer:MovieClip;
public var map:mainMap = new mainMap();
// Coins
public var YcoinArray:Array = new Array(Coin);
public var coinsOnstage:Array = new Array();
public var coinsCollected:Number;
// Lifes and Timers
public var lifes:int;
public function Game()
{
// CONSTRUCTOR CODE
addEventListener(Event.ENTER_FRAME, everyFrame);// Calls this function every frame
coinsCollected = 0; // Set coinsCollected to 0
lifes = 3; // Set lives to 3
mapContainer = new MovieClip(); // Create new mapContainer MC
addMap();
addCoins(); // Calls the addCoins function once
}
// Function to add the coins to the stage, only called once
public function addCoins()
{
var areas:Array = [map.area1_mc,map.area2_mc,map.area3_mc,map.area4_mc,map.area5_mc,map.area6_mc];
for (var i:int = 0; i < areas.length; i++)
{
for (var j:int = 0; j < 3; j++)
{
var Ycoin:Coin = new Coin();
Ycoin.x = Math.round(Math.random() * ((areas*.x + areas*.width - Ycoin.width) - areas*.x)) + areas*.x;
Ycoin.y = Math.round(Math.random() * ((areas*.y + areas*.height - Ycoin.height) - areas*.y)) + areas*.y;
mapContainer.addChild(Ycoin);
coinsOnstage.push(Ycoin);
}
}
}
public function addMap()
{
mapContainer.addChild(map); // Adds the map to the Container.
stage.addChild( mapContainer ); // Adds the container to the stage.
mapContainer.x = 654.90; // Set the map Containers .X
mapContainer.y = 169.05; // Set the map Containers .y
stage.addChild(car); // Add the Car.
car.x = 76; // Set the cars.X
car.y = 530; // Set the cars.y
}
// Function that is called Every Frame
public function everyFrame(e:Event)
{
// Check for coins collected
CatchCoins();
// Check if car is on the map areas
checkCarsPos();
// Moves Map accordingly
moveMap();
}
// Function that moves the map container.
function moveMap()
{
var playerBounds = car.getBounds(stage);
var distanceY = MyMaths.Position(600,car.y);
var distanceX = MyMaths.Position(800,car.x);
if (distanceX > 200)
{
mapContainer.x -= car.velocityX;
}
if (distanceY > 200)
{
mapContainer.y -= car.velocityY;
}
}
// Function to check coinsCollected
function CatchCoins():void
{
for (var i:int = coinsOnstage.length-1; i > -1; i--)
{
var currentCoin:MovieClip = coinsOnstage*; // Create the variable currentCoin as movieclip
if (currentCoin.hitTestObject(car)) // Is the car hitting the coin
{
coinsCollected++; // Add one to the coinsCollected value
mapContainer.removeChild(currentCoin); // Remove the specific coin from the screen
coinsOnstage.splice(i,1); // Remove the specific coin from the array
var mySound:Sound = new Sound(); // Declare new sound variable
mySound.load(new URLRequest("coin.mp3")); // Load in coin.mp3
mySound.play(); // Play the sound when the car hits the coin
}
}
}
// Function to check if the car is on the map areas.
function checkCarsPos():Boolean
{
var areas:Array = [map.area1_mc,map.area2_mc,map.area3_mc,map.area4_mc,map.area5_mc,map.area6_mc,
map.bridge1, map.bridge2, map.bridge3, map.bridge4, map.bridge5];
var n:int = areas.length;
for (var i:int=0; i < n; i++){
if (areas*.hitTestObject(car)){
trace("OK");
return true;
}
}
lifes--; // Take a life.
removeEventListener(Event.ENTER_FRAME, everyFrame);
stage.removeChild(car);
if (lifes >=0){
doReset(); // Call the reset function
} else {
doGameOver(); // Call the GameOver Function
}
return false;
}
// Function to reset the car and timers etc.
public function doReset()
{
trace("reset");
//addChild(car);
//addEventListener(Event.ENTER_FRAME, everyFrame);
}
// Function to do all GameOver Stuff.
public function doGameOver()
{
trace("game over");
}
}// class
}// package