I’m getting this error when I try to use a bitmap collision test:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PlayerGroundCollisions/frameEvents()[/Users/Bill/Desktop/Lunar_Madnes s/PlayerGroundCollisions.as:41]
at Lunar_Madness/onEnterFrame()[/Users/Bill/Desktop/GAME STUFF/Lunar_Madness/Lunar_Madness.as:61]
Here’s the code,
PlayerGroundCollisions Class:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Point;
public class PlayerGroundCollisions extends MovieClip
{
private var groundObjects:Array;
static var _player:Bitmap;
static var _foreground:Bitmap;
public var _collisionSide:String;
public function PlayerGroundCollisions()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
//Initialize properties
_player = PlayerShip.Player;
_foreground = Lunar_Madness.Foreground;
//Add event listenters
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage)
}
public function frameEvents():void
{//Directives…
//var _foreground = MovieClip(parent).foreground;
var loopCounter:int = 0;
while (loopCounter++ != 10)
{
if(_player.bitmapData.hitTest(new Point(_player.x, _player.y), 255, _foreground, new Point (_foreground.x, _foreground.y), 255)) --------->THIS IS LINE 41 FROM ABOVE
{
//A collision was found.
//next teh code creates “collision boxes” on all
// four sides of the lander to
// find out on which side the collision is occurring
//Switch off gravity
PlayerShip.gravity = 0;
//1. Check for a collision on the bottom
if(_player.bitmapData.hitTest(new Point(_player.x, _player.y + 10), 255, _foreground, new Point(_foreground.x, _foreground.y), 255))
{
//Move teh lander out of the collision
PlayerShip.PlayerY = -1;
PlayerShip.VY = 0;
_collisionSide = “bottom”;
}
//2. Check for a collision on the top
else if(_player.bitmapData.hitTest(new Point(_player.x, _player.y - 10), 255, _foreground, new Point(_foreground.x, _foreground.y), 255))
{
//Move teh lander out of the collision
PlayerShip.PlayerY = 1;
PlayerShip.VY = 0;
_collisionSide = “top”;
}
//3. Check for a collision on the riht
else if(_player.bitmapData.hitTest(new Point(_player.x + 10, _player.y), 255, _foreground, new Point(_foreground.x, _foreground.y), 255))
{
//Move teh lander out of the collision
PlayerShip.PlayerX = -1;
PlayerShip.VX = 0;
_collisionSide = “right”;
}
//4. Check for a collision on the left
else if(_player.bitmapData.hitTest(new Point(_player.x - 10, _player.y + 10), 255, _foreground, new Point(_foreground.x, _foreground.y), 255))
{
//Move teh lander out of the collision
PlayerShip.PlayerX = +1;
PlayerShip.VX = 0;
_collisionSide = “left”;
}
else{
break;
}
}
//Switch gravity back on if there is no ground below the lander.
//Adding "+1+ to the lander;s y position in hte collision
//test is the key to making this work
if(!_player.bitmapData.hitTest(new Point(_player.x, _player.y), 255, _foreground, new Point(_foreground.x, _foreground.y), 255))
{
PlayerShip.gravity = .015;
}
}
}
}
}
LunarMadness Class:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.BitmapData;
public class Lunar_Madness extends MovieClip
{
private var _playerShip:PlayerShip;
private var _playerMissiles:PlayerMissiles;
public static var foreground:Bitmap;
private var _shipGroundCollide:PlayerGroundCollisions;
public var imageBitmapData:BitmapData;
public var foreground:Bitmap;
public function Lunar_Madness()
{
_playerShip = new PlayerShip();
_playerMissiles = new PlayerMissiles();
_shipGroundCollide = new PlayerGroundCollisions();
addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
//Initialize properties
imageBitmapData = new Fg(1437, 376);
foreground = new Bitmap(imageBitmapData);
foreground.x = -26;
foreground.y = 216;
addChild(foreground);
addChild(_playerShip);
addChild(_playerMissiles);
addChild(_shipGroundCollide);
//Add event listenters
addEventListener(Event.ENTER_FRAME, onEnterFrame);
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage)
}
private function onEnterFrame(event:Event):void
{//Directives…
_playerShip.frameEvents(); - ---------------->LINE 61 FROM ABOVE
_playerMissiles.frameEvents();
_shipGroundCollide.frameEvents();
}
public static function get Foreground():Bitmap
{
return foreground;
}
}
}
CAN ANYONE CLUE ME IN AS TO WHAT THE PROBLEM IS?