TypeError: Error #2007: AS3 - actionscript-3

I have trouble with my ActionScript 3 code, it gives me this error :
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at MethodInfo-8()
This is the code:
//Kills you if you touch an Enemy
addEventListener(Event.ENTER_FRAME, checkCollision);
function checkCollision(e:Event):void
{
if (square.hitTestObject(newEnemy)){
//removeEventListener(Event.ENTER_FRAME, checkCollision);
removeEventListener(Event.ENTER_FRAME,update);
stage.removeEventListener(KeyboardEvent.KEY_DOWN,jump);
removeChild(square);
init();
}
}

It seems the newEnemy variable is not defined.

Related

TypeError: Error #1009: Cannot access a property or method of a null object reference. For My PAP

This is for my game of obstacles that I am creating and what is giving error is at the time of the player colliding with the end that appears the following error:
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
obstaculo_fla::MainTimeline/handleCollision()[obstaculo_fla.MainTimeline::frame9:55]
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
obstaculo_fla::MainTimeline/handleCollision_final()[obstaculo_fla.MainTimeline::frame367:18]
This is the code:
player.addEventListener( Event.ENTER_FRAME, handleCollision_final)
function handleCollision_final( e:Event ):void
{
if(player.hitTestObject(nextum))
{
gotoAndPlay(368);
}
else
{
trace("MISS");
}
}
player.addEventListener( Event.ENTER_FRAME, handleCollision)
function handleCollision( e:Event ):void
{
if(player.hitTestObject(wall))
{
player.y-=20;
gotoAndPlay(6);
}
else
{
trace("MISS");
}
}
Thank You
I tried modifying your code with a Drag event (in a single scene), everything seem to be working OK. I've run in to a similar problem in the past, and found that I had to get rid of the event listeners. I've also found that I need a separate function to do that. Give this a shot:
player.addEventListener(Event.ENTER_FRAME, handleCollision_final)
function handleCollision_final(e: Event): void {
if (player.hitTestObject(nextum)) {
removeELs(); //<<<<<<<<<<<<<<<<<<<<<<<<< ELs (EventListeners)
gotoAndPlay(368);
} else {
trace("nextum: MISS");
}
}
player.addEventListener(Event.ENTER_FRAME, handleCollision)
function handleCollision(e: Event): void {
if (player.hitTestObject(wall)) {
player.y -= 20;
removeELs(); //<<<<<<<<<<<<<<<<<<<<<<<<< ELs (EventListeners)
gotoAndPlay(6);
} else {
trace("wall: MISS");
}
}
// This probably won't work if you try 'removeEventListener'
// from within the collision functions.
function removeELs() {
player.removeEventListener(Event.ENTER_FRAME, handleCollision)
player.removeEventListener(Event.ENTER_FRAME, handleCollision_final)
}
As you can see, I added the 'removeELs()' function at the bottom of your code.
Good luck

Actionscript 3.0 Error #2025: The supplied DisplayObject must be a child of the caller

I have a warning about this:
ArgumentError: Error #2025: The supplied DisplayObject must be a child
of the caller. at flash.display::DisplayObjectContainer/removeChild()
I don't what is wrong with the code.
public function Laser() {
}
You add laser to the stage, so remove it also from the stage.
if(lasers[l].y < 0)
{
stage.removeChild(lasers[l]); // <- Here
lasers[l] = null;
lasers.splice(l, 1);
trace("Remove laser from screen");
}

AS3 #1034: Type Coercion failed: cannot convert to flash.display.DisplayObject

I'm working on a Flash racing game in AS3 where the player navigates through a course marked by cones. The cones and the car are contained within a movie clip 'gamesprite'. I created an array for the cones in order to check for collision between the car and each of the cones.
public var cones:Array;
public function findCones()
{
cones = new Array();
for(var i=0;i<gamesprite.numChildren;i++)
{
var mc = gamesprite.getChildAt(i);
if (mc is Cone)
{
cones.push(Cone);
trace(cones);
}
}
}
Later in my game loop function I have this to check for collisions between the gamesprite.car movie clip and each of the cones in the array.
for(var j:Number=0;j<cones.length;j++)
{
if (gamesprite.car.hitTestObject (cones[j]))
{
trace("cones and car colliding");
}
}
I receive this error when testing the game.
TypeError: Error #1034: Type Coercion failed: cannot convert Cone$ to flash.display.DisplayObject.
at Racing/gameLoop()
I'm probably missing something simple but can't figure it out.
You're pushing a wrong thing into your array. You have there cones.push(Cone); but instead you should cones.push(mc);

TRIED ALMOST EVERYTHING: ypeError: Error #1009: Cannot access a property or method of a null object reference

I am having this problem and cant get around it at all
im making a platform game for uni
i have 2 files
flashgame.fla and Coin.as (this is code for the Coin class)
i have code stating that once the player has collected all the coins the frame will change from frame 1 to frame 2.
When i do i receive this message
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at Coin/update() TypeError: Error #1009: Cannot
access a property or method of a null object reference. at
flashgame_fla::MainTimeline/loop()
i have tried Try and Catch and various other things
I think it does this because the Coin.as extends MovieClip so when it goes to the next frame it is still trying to find a coin when nothing is there.
here is the code for Coin.as
package {
import flash.display.MovieClip;
import flash.events.*;
public class Coin extends MovieClip {
var player:MovieClip;
var mainTimeLine = MovieClip(root);
public function Coin() {
this.addEventListener(Event.ENTER_FRAME, update);
}
function update(event:Event):void
{
player=MovieClip(root).player;
if(this.hitTestObject(player))
{
this.removeEventListener(Event.ENTER_FRAME, update);
parent.removeChild(this);
mainTimeLine.coinCount++;
}
}
}
}
i have an array in flashgame.fla that records all coins in the game. when the player hits them they are spliced from the array. could also be causing the problem when going to frame 2
important stuff from flashgame.fla
var coin:Array = new Array();
for (i=0; i<numChildren; i++)
{
if (getChildAt(i) is Coin)
{
coin.push(getChildAt(i).getRect(this));
}
}
splicing coins
for (i=0; i<coin.length; i++)
{
if (player.getRect(this).intersects(coin[i]))
{
coinSnd.play();
coin.splice(i,1);
}
}
Thanks for any help given
if you need anything more from me please ask :)
all g with screenshots
It's most likely because the player cannot be found in the update function in the Coin class. This is because ENTER_FRAME events are run even if the display object is not on the stage, which you often don't want. In this case you try to do a hittest against the player.
To solve this, you can start running update as soon as the Coin is attached to the stage and stop running it as soon as it's removed from the stage.
public function Coin() {
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
function onAddedToStage(event:Event):void {
this.addEventListener(Event.ENTER_FRAME, update);
}
function onRemovedFromStage(event:Event):void {
this.removeEventListener(Event.ENTER_FRAME, update);
}
Which line number is the error occurring on? That should tell you which variable is null.
My guess, based on this part of the error: "TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()", is that the parent of the coin is null because you've moved to a new keyframe and the instance has been removed from the stage.
That is, I suspect the error is occurring on the line with parent.removeChild(this); - is this correct?
If that's the case then check whether parent is not null before calling removeChild on it.

AS3: Cannot access a property of a null object reference

I'm trying to trace the mouse X position with a movieclip.
What I have done so far:
public class LuckyHitBeta extends MovieClip {
var ballReady:ballReady_mc;
private function liveIcon():void {
ballReady=new ballReady_mc();
addChild(ballReady);
ballReady.y=1;
}
private function onEnterFrm(e:Event):void
{
ballReady.x=mouseX;
}
}
Runtime error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at LuckyHitBeta/onEnterFrm()
I don't see you calling the function liveIcon(). It seems like the enterFrame event happens before ballready is initialized.
I think it means ballReady is NULL
I would do something like
private function onEnterFrm(e:Event):void
{
if (ballReady) {
ballReady.x=mouseX;
}
}