Copy link to clipboard
Copied
I am making a game for with AS3 and have only been doing it for a week but I always get this problem and there is always a different solution each time. could someone help me out. here is the source code(frame 2 of 3 where the main problem is on line 192):
full error:
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at collisionwallgame_fla::MainTimeline/greenwinZone()
source code:
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.Sprite;
import flash.events.MouseEvent;
stop();
var hero:Sprite = new Sprite ;
var characterSpeed:int = 7;
//var prevX:Number = 0;
//var prevY:Number = 0;
hero.x = 133.35;
hero.y = 188;
addChild(hero);
//keyboard movements
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyboardCommands);
function keyboardCommands(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.LEFT)
{
hero.x -= 5;
}
if (e.keyCode == Keyboard.RIGHT)
{
hero.x += 5;
}
if (e.keyCode == Keyboard.UP)
{
hero.y -= 5;
}
if (e.keyCode == Keyboard.DOWN)
{
hero.y += 5;
}
}
//wall collision detection
stage.addEventListener(Event.ENTER_FRAME, wallblock);
/*function resetXY():void
{
trace(hero.x +" "+prevX);
hero.x = prevX;
hero.y = prevY;
}*/
function wallblock(e:Event)
{
if (hero.hitTestObject(wall1))
{
//prevX = hero.x;
//prevY = hero.y;
hero.x += 7.5;
}
/*{
resetXY()
}*/
if (hero.hitTestObject(wall2))
{
hero.y -= 7.5;
}
if (hero.hitTestObject(wall3))
{
hero.x += 7.5;
}
if (hero.hitTestObject(wall4))
{
hero.y -= 7.5;
}
if (hero.hitTestObject(wall5))
{
hero.y -= 7.5;
}
if (hero.hitTestObject(wall6))
{
hero.y -= 7.5;
}
if (hero.hitTestObject(wall7))
{
hero.y -= 7.5;
}
if (hero.hitTestObject(wall8))
{
hero.x -= 7.5;
}
if (hero.hitTestObject(wall9))
{
hero.y -= 7.5;
}
if (hero.hitTestObject(wall10))
{
hero.x -= 7.5;
}
if (hero.hitTestObject(wall11))
{
hero.y += 7.5;
}
if (hero.hitTestObject(wall12))
{
hero.x -= 7.5;
}
if (hero.hitTestObject(wall13))
{
hero.x -= 7.5;
}
if (hero.hitTestObject(wall14))
{
hero.y += 7.5;
}
if (hero.hitTestObject(wall15))
{
hero.y += 7.5;
}
if (hero.hitTestObject(wall16))
{
hero.y += 7.5;
}
if (hero.hitTestObject(wall17))
{
hero.x += 7.5;
}
if (hero.hitTestObject(wall18))
{
hero.y += 7.5;
}
}
//adding in enemy sprites
var enemySpeed:int = 2;
var Enemy1:Enemyclass1 = new Enemyclass1 ;
var Enemy2:Enemyclass1 = new Enemyclass1 ;
var Enemy3:Enemyclass1 = new Enemyclass1 ;
var Enemy4:Enemyclass2 = new Enemyclass2 ;
var Enemy5:Enemyclass2 = new Enemyclass2 ;
Enemy1.x = 187;
Enemy1.y = 116;
Enemy2.x = 271;
Enemy2.y = 116;
Enemy3.x = 349;
Enemy3.y = 116;
Enemy4.x = 219;
Enemy4.y = 260;
Enemy5.x = 309;
Enemy5.y = 260;
addChild(Enemy1);
addChild(Enemy2);
addChild(Enemy3);
addChild(Enemy4);
addChild(Enemy5);
function terminate():void
{
gotoAndStop(3);
trace("hit");
removeChild(Enemy1);
removeChild(Enemy2);
removeChild(Enemy3);
removeChild(Enemy4);
removeChild(Enemy5);
removeChild(hero);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyboardCommands);
stage.removeEventListener(Event.ENTER_FRAME, wallblock);
hero.removeEventListener(Event.ENTER_FRAME, enemyhit);
}
//code that advances you to the next screen)
stage.addEventListener(Event.ENTER_FRAME, greenwinZone)
function greenwinZone(e:Event):void
{
if (hero.hitTestObject(greenwinzone)) <============================================= LINE 192
{
gotoAndStop(4);
trace("hit");
removeChild(Enemy1);
removeChild(Enemy2);
removeChild(Enemy3);
removeChild(Enemy4);
removeChild(Enemy5);
removeChild(hero);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyboardCommands);
stage.removeEventListener(Event.ENTER_FRAME, wallblock);
hero.removeEventListener(Event.ENTER_FRAME, enemyhit);
hero.removeEventListener(Event.ENTER_FRAME, greenwinZone);
}
}
//enemy collision
hero.addEventListener(Event.ENTER_FRAME, enemyhit);
function enemyhit(e:Event):void
{
if (hero.hitTestObject(Enemy1))
{
terminate();
}
if (hero.hitTestObject(Enemy2))
{
terminate();
}
if (hero.hitTestObject(Enemy3))
{
terminate();
}
if (hero.hitTestObject(Enemy4))
{
terminate();
}
if (hero.hitTestObject(Enemy5))
{
terminate();
}
}
It sounds like the function does not stop executing when it should. I think I can see why now that I take a closer look. You assign the event listener that calls that function to the stage (~ line 189), but then you try to remove that event listener from the hero (~ line 205).
Copy link to clipboard
Copied
Use the trace function before line 192 to see if the greenwinzone object is coming up null before the "hit" trace or after it.
Copy link to clipboard
Copied
I did what you asked and the object comes up as null as soon as it hits one of the enemy sprites. it still traces hit, but immediately after the null error shows up.
Copy link to clipboard
Copied
It sounds like the function does not stop executing when it should. I think I can see why now that I take a closer look. You assign the event listener that calls that function to the stage (~ line 189), but then you try to remove that event listener from the hero (~ line 205).
Copy link to clipboard
Copied
I have fixed the problem, what I did was set the event listener for winning on line 189 to hero as opposed to stage, and inserted hero.removeEventListener(Event.ENTER_FRAME, greenwinZone) into the terminate function. your really helped me out with this one thanks!
Copy link to clipboard
Copied
You're welcome
Find more inspiration, events, and resources on the new Adobe Community
Explore Now