as3 - Error #1009, an object is null and bugged with timer - actionscript-3

When the player touches the door, the next level is supposed to be added and the previous level should be removed. However in the game, the next level does get added and everything works, but the output shows this issue.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.utils::Timer/tick()
This is the enterframe:
if (player.collisionArea.hitTestObject(door0))
{
var timer: Timer = new Timer(1000, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, fade);
timer.reset();
timer.start();
}
This is the timer:
function fade(event: TimerEvent)
{
removeEventListener(TimerEvent.TIMER_COMPLETE, fade);
var pageTwo: PageTwo = new PageTwo;
parent.addChild(pageTwo);
this.parent.removeChild(this);
}
This is in the "previous level" class. "this" is itself (previous level) and "pageTwo" is (next level).
So, the output indicates the error is at "parent.addChild(pageTwo);". However if I remove that, the output indicates the issue is from "this.parent.removeChild(this);
I assume it's because the child is removed that's why there is a null issue. But how do I solve this error coming from the output? Am I removing the movieclip the wrong way?

Your problem is coming from this line :
parent.addChild(pageTwo);
because in your code as this condition
if (player.collisionArea.hitTestObject(door0))
is true, you will create another Timer object which when its TimerEvent.TIMER_COMPLETE event is fired, it will try to add a new PageTwo instance to a null parent and that's why the error is fired.
So to avoid that, you can remove the Event.ENTER_FRAME event listener at the first time when that condition is true or if you still need it (the Event.ENTER_FRAME event listener) you have to verify your condition to avoid such behavior.
Also, your timer var should be declared as global to be accessible from the fade() function to be able to remove its TimerEvent.TIMER_COMPLETE event listener which you are trying to remove from your current object.
Hope that can help.

Related

How to fix 'TypeError: Error #1009' error in ActionScript3.0 Adobe Animate

I'm setting up a button on the first frame which when clicked will transfer the user to the 2nd frame using this code:
stop();
Btn_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_2);
function fl_ClickToGoToAndPlayFromFrame_2(event:MouseEvent):void
{
gotoAndPlay(2);
}
and on the second frame, I set up a dynamic text that will perform a countdown using this code:
var myTimer:Timer = new Timer(1000,60); // every second for 60 seconds
myTimer.addEventListener(TimerEvent.TIMER, onTimer);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
myTimer.start();
function onTimer(e: TimerEvent):void {
countdown_text.text = String(myTimer.repeatCount - myTimer.currentCount);
}
function onComplete(e: TimerEvent):void{
gotoAndStop(3);
}
the thing is keep getting TypeError: Error #1009 message after debugging it. I know the fault is in line 7 of the 2nd code but I have no idea what is wrong with it. Pls help!
I should see your source fla, but it is most likely related to countdown_text not being accessible in that frame. Error description is "Cannot access a property or method of a null object reference", that means it cannot find the reference which is "countdown_text".
It is very very bad practice to write AS directly in frames. Convert code into a class and assign it as a document class.
You can find Adobe documentation for document class here: https://helpx.adobe.com/animate/using/actionscript-publish-settings.html

how to fix error 1009 in as3

hi i have been searhing the net for hours and have not found a solution to my problem and i have no idea how to fix it as i am only new to flash so if you know anything that might help me just comment below please all help is greatly appreciated
so here is the code
Quit.addEventListener(MouseEvent.CLICK, func2);
function func2(event:MouseEvent):void
{
gotoAndStop(2);
}
Help.addEventListener(MouseEvent.CLICK, func4);
function func4(event:MouseEvent):void
{
gotoAndStop(4);
}
var myTimer:Timer = new Timer(2000,0);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener(e:TimerEvent):void {
Hungry_bar.scaleX-=0.05;
if(Hungry_bar.scaleX<=0.05){
gotoAndStop(12)
}
}
myTimer.start();
var myTimer2:Timer = new Timer(3000,0);
myTimer.addEventListener(TimerEvent.TIMER, timerListener2);
function timerListener2(e:TimerEvent):void {
Fun_bar.scaleX-=0.05;
if(Fun_bar.scaleX<=0.05){
gotoAndStop(13)
}
}
myTimer2.start();
Feed.addEventListener(MouseEvent.CLICK,feed)
function feed(e:MouseEvent){
Hungry_bar.scaleX+=0.05
if(Hungry_bar.scaleX>=1.5){
gotoAndStop(14)
}
}
Fun.addEventListener(MouseEvent.CLICK,happy)
function happy(e:MouseEvent){
Fun_bar.scaleX+=0.05
if(Fun_bar.scaleX>=1.5){
gotoAndStop(15)
}
}
And here is the error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at petgame_fla::MainTimeline/timerListener()[petgame_fla.MainTimeline::frame5:19]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at petgame_fla::MainTimeline/timerListener2()[petgame_fla.MainTimeline::frame5:29]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
You use gotoAndStop() to move off a frame, in the meantime you have timers that refer to objects that do not exist on other frames than your current frame (5). gotoAndStop() triggers destruction of previously current frame, thus, once you go to another frame, your Hungry_bar becomes void, but timers still tick, because they are frame independent, and when they trigger the timer event, your functions assume that your MC's components are there while they no longer are in place. You should stop the timers and remove their listeners when you change the frame via gotoAndStop().

AS3 gotoAndStop(2); causes a 1009 error second time the frame runs

Disclaimer: I'm really new/incredibly bad at AS3 so it's probably something really stupid that should never happen
Okay so, the first time my main menu frame runs, it runs fine and sends me to the gameplay frame when I press the button. After the gameplay is complete, it returns to the menu frame, and runs fine until I press the same button from before, which calls this error: .
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main_fla::MainTimeline/frame2()[Main_fla.MainTimeline::frame2:6]
at flash.display::MovieClip/gotoAndPlay()
at Main_fla::MainTimeline/easyPress()[Main_fla.MainTimeline::frame3:83]
at Main_fla::MainTimeline/mClickE()[Main_fla.MainTimeline::frame3:45]
My code for the button is as follows:
buttEasy.addEventListener(MouseEvent.CLICK, mClickE);
buttHard.addEventListener(MouseEvent.CLICK, mClickH);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mMove);
function mClickE(e:MouseEvent){
easyPress();
trace("easyP");
menuUsed = true;
}
function easyPress(){
trace("Waited for press and release");
sTime = 0;
sTempo = (6) ;
sBall = 0;
ballSpeed = 7;
gameIsOver = false;
menuUsed = true;
lvlArray0= new Array(1,0,0,2,0,0,1,0,0,3,0,0,1,0,0,2,0,0,1,0,0,3,0,01,0,0,2,0,0,1);
init2 = false;
buttEasy.removeEventListener(MouseEvent.CLICK, mClickE);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mMove);
gotoAndPlay(2);
}
I honestly have no idea why this is happening. I'm using mouse events instead of button press events and whatnot because my movieclips started disappearing and flashing and other unexplainable stuff...
yeah...
I just registered, so I can't post this as a comment.
Anyway the error occurs on frame 2, not in the script you've provided (which is on frame 3).
You can see this in the error message:
"at Main_fla::MainTimeline/frame2()[Main_fla.MainTimeline::frame2:6]"
-> frame 2 line 6.
There you're accessing something that doesn't exist anymore. (-> something that is now null)
Maybe an object on the stage that has been removed. (But there are a lot of other possibilities, so don't stick with that solution)
Post the script you have on frame 3 for further help.
The flashing and other unexplainable stuff happens, because of this error. It aborts the script and runs the flash normally. (this means that for example the stop(); method won't be executed -> the player runs through all your frames -> the objects on the stage appear to be flashing)
You're probably just addressing the "stage" before the reference is given. Start your code with:
addEventListener(Event.ADDED_TO_STAGE, init);
and a handler for this listener
private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// write your code after this
}
If you're framescripting (writing AS3 code in a frame) It's not really your problem.
But as the problem states - you're calling some objects property or method witch is null. Your debugger will be able to point to the null object that you try to call on frame 2.

actionscript error?

I have a problem, this works then at the " and" it dies and gives me an error
TypeError: Error #1009: Cannot access
a property or method of a null object
reference. at
Untitled_fla::MainTimeline/frameLooper()
at
flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
CODE
var string:String = "Welcome to PuppetWeb Inc\nMy name is Steve and I will be your host for this presentation!\n...\nOkay I think it is ready, let's go!";
var myArray:Array = string.split("");
var timer : Timer = new Timer (100, myArray.length);
timer.addEventListener (TimerEvent.TIMER, frameLooper);
timer.start();
function frameLooper(event:Event):void {
if(myArray.length > 0) {
text1.appendText(myArray.shift());
}else{
removeEventListener(Event.ENTER_FRAME, frameLooper);
}
}
It works for the start and then just dies at and, and then it shows that error about 50 times and restarts.
Any help?
I assume this is code written on a keyframe in the timeline, so my guess would be that your textfield goes away for some reason, most likely a keyframe animation of some kind.
It's also somewhat odd that you are removing an Event.ENTER_FRAME listener when the array is empty and not the TimerEvent.TIMER
At the end, you're trying to remove the listener from an implicit "this". Your statement is equivalent to:
this.removeEventListener(Event.ENTER_FRAME, frameLooper);
But "this" is a reference to the main timeline (if this is a frame script on the main timeline) or a reference to the instance that contains this code. It's not a reference to the Timer instance, which is what you need:
event.target.removeEventListener(TimerEvent.TIMER, frameLooper);
Why not use string.charAt()?
var string:String = "Welcome to PuppetWeb Inc\nMy name is Steve and I will be your host for this presentation!\n...\nOkay I think it is ready, let's go!";
var timer : Timer = new Timer (100, string.length);
timer.addEventListener (TimerEvent.TIMER, frameLooper);
timer.start();
function frameLooper(event:TimerEvent):void {
text1.appendText(string.charAt (event.target.currentCount-1);
}
Among all the other issues Stated in the other posts you are not stopping the time.
Dont forget timer.stop( );

Removing an event listener as well as a sprite at the same time AS3

I’m having trouble removing the an event listener as well as the sprite at the same time. I currently get an error:
TypeError: Error #1009: Cannot access
a property or method of a null object
reference.
And if I comment out removeChild, I have no error but, obviously, the sprite remains on the screen. Any ideas how I can rid myself of that error?
//Bullet extends Sprite Class
bullet:Bullet = new Bullet();
mc.addChild(bullet);
bullet.addEventListener(Event.ENTER_FRAME, shoot);
function shoot(e:Event):void {
var shot:Bullet = e.currentTarget as Bullet;
//check shot is outside the frame
if (shot.x < 0 - shot.width || shot.x > stage.stageWidth || shot.y > 525)
{
//trying to remove the thing and it's listener
e.currentTarget.removeEventListener(e.type,arguments.callee);
e.currentTarget.parent.removeChild(shot);
}
else
{
shot.setInMotion();
}
}
Apart from a missing var before bullet:Bullet, I don't see anything wrong in the example code. You should set a breakpoint right after:
var shot:Bullet = e.currentTarget as Bullet;
And figure out why shot is null. I suspect there is something amiss in a piece of code outside of the little bit you're providing as the example. If the code is working with only the removeChild line commented out, it tells me that e.currentTarget is not null, but that it's also not a reference to an instance of type Bullet (i.e. the "as" cast is returning null).
Try reversing these lines
Maybe the reference to e.currentTarget is getting lost through the object references
e.currentTarget.removeEventListener(e.type,arguments.callee);
e.currentTarget.parent.removeChild(shot);
to
e.currentTarget.parent.removeChild(shot);
e.currentTarget.removeEventListener(e.type,arguments.callee);