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

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);

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

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

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.

AS3 - The supplied DisplayObject must be a child of the caller error while removing swf

please help i am getting this error and could not solve with any of the other methods described in all previous posts with similar topic.
Actually here i am loading a swf myMap onto another swf.
The swf loading works fine, but when try to remove this from stage i get the above said error...
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at actions.classes::MapInteractionManager/unloadSWF()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
Here's my as3 code...
var _swfLoader:Loader;
var _swfContent:MovieClip;
loadSWF("myMap.swf"); //loading the swf file here
function loadSWF(path:String):void {
var _req:URLRequest = new URLRequest();
_req.url = path;
_swfLoader = new Loader();
setupListeners(_swfLoader.contentLoaderInfo);
_swfLoader.load(_req);
}
function setupListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, addSWF);
dispatcher.addEventListener(ProgressEvent.PROGRESS, preloadSWF);
}
function preloadSWF(event:ProgressEvent):void {
var _perc:int = (event.bytesLoaded / event.bytesTotal) * 100;
// swfPreloader.percentTF.text = _perc + "%";
}
function addSWF(event:Event):void {
event.target.removeEventListener(Event.COMPLETE, addSWF);
event.target.removeEventListener(ProgressEvent.PROGRESS, preloadSWF);
_swfContent = event.target.content;
_swfContent.addEventListener("close", unloadSWF);
main.stage.addChild(_swfContent);
}
function unloadSWF(event:Event):void {
_swfLoader.unloadAndStop();
main.stage.removeChild(_swfContent); //getting error when trying to remove swf
_swfContent = null;
}
and close event is as,
_swfContent.dispatchEvent(new Event("close"));
Please help, I'm stuck.
here with some update,
i updated code as,
function unloadSWF(event:Event):void
{
if(main.stage.contains(_swfContent))
main.stage.removeChild(_swfContent);
}
Now the error is gone as it is not entering the if loop!!!???
But still i can see that swf on stage:( plz help
GOT SOLVED...
Thanks everyone for helping...
ToddBFisher did solve it:)
Simply added the _swfLoader to the stage, loaded it, and attached the close listener to it instead of even having a _swfContent. Cut out the middle man and it worked.... Hope this helps...
As I recall .unloadAndStop(); does a bunch of cleanup type things, which you are calling right before. It is possible part of the cleanup is removing it from the display list.
Try calling the removeChild() before calling unlodaAndStop()
function unloadSWF(event:Event):void {
stage.removeChild(_swfContent); //getting error when trying to remove swf
_swfLoader.unloadAndStop();
_swfContent = null;
}
EDIT
Try simply adding the _swfLoader to the stage, load it, and attach the close listener to it instead of even having a _swfContent. Cut out the middle man and see what happens.
The error means the child is already removed (or never added)
try to comment out
_swfLoader.unloadAndStop();
to see if it works.
main.stage.removeChild is already setting _swfContent to null because removeChild was fired so removing the line _swfContent = null might solve it.

Why does this not work? Flash As3, if added child is at frame something?

This is my code help me please its really frustrating!
I have a movieclip in my library and added it with AS3 to the stage.
That part was easy. But now i want to control that movieclip.
If introScene "introClass" Reaches frame 120 then i want to remove that movieclip
and replace it with another one. The problem is the if statement doesn't work.
I also tried getChildByName but that didn't work either.
var introClass = new introScene;
addChild(introClass);
introClass.x = 640;
introClass.y = 387;
/*******INTRO-SCENE*******/
introClass.addEventListener(Event, introLoaded);
function introLoaded(event):void{
if(introClass == 120 ){
trace("Frame Reached")
}
}
i tried this and this also doesn't work :(
introClass.addEventListener(Event, introLoaded);
function introLoaded (e:Event):void{
if(MovieClip(introClass).currentFrame == 120){
trace("120 complete")
}
}
This is wrong statement:
introClass.addEventListener(Event, introLoaded);
You need to pass a string to addEventListener. Event type name is converted to a string at runtime which adds a event listener to "flash.events.Event" or something. And your object obviously doesn't have this event. You need to use Event.ENTER_FRAME for example.

actionscript-3: check if movieClip exists

I have a movieclip created with the following code:
var thumbContainer:MovieClip = new MovieClip();
thumbContainer.name = "thumbContainer";
stage.addChild (thumbContainer);
If the window gets larger/smaller I want everything back in place. So I have an stage Event Listener. Now I want to see if this mc exists to put back in place. I've tried different ways but keep getting an error that does not exist.
1120: Access of undefined property thumbContainer.
if (this.getChildByName("thumbContainer") != null) {
trace("exists")
}
and
if ("thumbContainer" in this) {
trace("exists")
}
or
function hasClipInIt (mc: MovieClip):Boolean {
return mc != null && contains(mc);
}
stage.addChild (thumbContainer);
//...
if (this.getChildByName("thumbContainer") != null)
You are adding the thumbContainer to stage and checking for its existence with this. Change stage to this or this to stage.
That said, an even more appropriate way is to keep a reference to the added movie clip and check for existence with the contains method. It determines whether the specified display object is a child of the DisplayObjectContainer instance or the instance itself. The search includes the entire display list including this DisplayObjectContainer instance, grandchildren, great-grandchildren, and so on.
Hence you can easily check using stage.contains(thumbContainer);
if you are having trouble firing errors, you can always resort to a try catch
try{
/// do something that will blow up...
}catch( e:Error ){
trace( "we had an error but its not fatal now..." );
}
the problem was that 'stage' and 'this' are not the same...that's why I couldn't talk to the mc.
this works:
var thumbContainer:MovieClip = new MovieClip();
thumbContainer.name = "thumbContainer";
addChild (thumbContainer);
if (getChildByName("thumbContainer") != null) {
trace("exists")
}