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

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.

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

removechild not working when the swf is loaded externally

my problem is that the close button (removechild) which is placed in a swf works perfectly alone but when the swf is loaded from another one the button no longer works.
Here's the external swf code:
eti_scroll.scrollTarget = box_eti ;
hab_scroll.scrollTarget = box_hab ;
com_scroll.scrollTarget = box_com ;
descr_scroll.scrollTarget = box_descr ;
exit.addEventListener(MouseEvent.CLICK, exitBtn_clickHandler);
function exitBtn_clickHandler(event:MouseEvent):void {
if(this.parent) this.parent.removeChild(this);
}
And here's the button code from the main swf:
menu_button_2.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler2);
function fl_MouseClickHandler2(event:MouseEvent):void
{
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("pages/page_template.swf");
myLoader.load(url);
addChild(myLoader);
}
In the main swf there is no packages imported or loaderclass
Here is the link to the fla version and the example version:
http://www.mediafire.com/file/5dzqnq3kth6n6dt/examples.rar
Error thrown when I use trace code by #organis and loaded from an external SWF file:
The error thrown:
I am here! Exit Button: [object SimpleButton] object Event Handler: function
Function() {} function Exit.addEventListener: function Function() {} function
MouseEvent.CLICK This: [object MainTimeline] object Parent: [object Loader]
object Parent.removeChild: function Function() {} function Error: Error #2069:
La clase Loader no implementa este método(The Loader Class doesn't implement
this method). at Error$/throwError() at flash.display::Loader/removeChild() at
page_template_fla::MainTimeline/onClick()[page_template_fla.‌​MainTimeline::frame1‌​
:65] –
Let me explain how to deal with this kind of problem. Once something that is supposed to work does not, the first thing to do is to pinpoint where the problem starts exactly, so you can probably diagnose what the problem is rather than have a vague understanding there is a problem somewhere.
So you read all the traces from standalone run, then from loaded run and there certainly must be a difference. Upon finding it, you act with the regard to what that difference is.
// If this does not work, that means scripts do not work in the loaded SWF at all.
trace("I am here!");
// If this doesn't work the same as standalone, that means
// something breaks while constructing the loaded content.
trace("Exit Button:", exit, typeof(exit));
// Lets check id the event handler is doing fine.
trace("Event Handler:", onClick, typeof(onClick));
// If the method is not present on the object,
// something is deeply wrong with the whole thing.
trace("Exit.addEventListener:", exit.addEventListener, typeof(exit.addEventListener));
exit.addEventListener(MouseEvent.CLICK, onClick);
function onClick(event:MouseEvent):void
{
// If it doesn't work that means there's no mouse event.
trace("MouseEvent.CLICK");
// Just to check things out.
trace("This:", this, typeof(this));
trace("Parent:", this.parent, typeof(this.parent));
trace("Parent.removeChild:", this.parent.removeChild, typeof(this.parent.removeChild));
if(this.parent) this.parent.removeChild(this);
}
UPD: Now, you're getting the error (you should have mentioned it in your question in the first place, actually)
Error: Error #2069: La clase Loader no implementa este método(_The Loader Class doesn't implement tihs method_). at Error$/throwError() at flash.display::Loader/removeChild() at page_template_fla::MainTimeline/onClick()[page_template_fla.‌​MainTimeline::frame1‌​:65]
Now take a look here (I like this link as it explains quite a lot): https://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7e26.html
Your script is on the main timeline. So, when you run it in a standalone mode, this.parent points to stage which can add and remove children, no problem.
Then, when you run it in the loaded mode, the hierarchy goes main SWF stage -> main SWF root -> ... -> Loader -> loaded SWF root. As you can see, when you address this.parent you get the Loader instance, the very one you use in main SWF to load the other one. Loader is a DisplayObjectContainer but is not intended for adding/removing children, thus it throws the exception mentioned above.
With all this I advise another way of removing content out of sight:
exit.addEventListener(MouseEvent.CLICK, onClick);
function onClick(event:MouseEvent):void
{
// Unsubscribe to help Garbage Collector do its job.
exit.removeEventListener(MouseEvent.CLICK, onClick);
// Hide the content.
visible = false;
// Remove all of its children.
removeChildren();
}
Or you can figure the case your script is running in and act accordingly:
exit.addEventListener(MouseEvent.CLICK, onClick);
function onClick(event:MouseEvent):void
{
// Unsubscribe to help Garbage Collector do its job.
exit.removeEventListener(MouseEvent.CLICK, onClick);
// Checking for
// if (parent == null)
// is unnecessary here because if that was the case
// there won't be a mouse event in the first place.
var aParent:DisplayObjectContainer = parent;
if (aParent is Loader)
{
// Loaded case.
aParent.parent.removeChild(aParent);
}
else
{
// Standalone case.
parent.removeChild(this);
}
}
However, that figuring will only work in the simple cases and will not in more complicated setups (cross-domain content, security sandboxes, etc).

SImple Coloring Book: Error #1009: Cannot access a property or method of a null object reference

I am creating a simple flash coloring book and am not very familiar with as3 programming language.
I entered the following code,and when I attempted to press the back button in the test movie I got that error.
stop();
back_btn.addEventListener(MouseEvent.CLICK, GoToChooseA);
function GoToChooseA(event:MouseEvent):void
{
gotoAndStop("Choose");
}
color_scroll.mask = myMask;
var goY: Number = color_scroll.y;
stage.addEventListener(Event.ENTER_FRAME, scrollManage);
function scrollManage(Event): void {
color_scroll.y += (goY - color_scroll.y) / 20;
}
up_btn.addEventListener(MouseEvent.MOUSE_DOWN, scrollUP);
down_btn.addEventListener(MouseEvent.MOUSE_DOWN, scrollDown);
function scrollUP(MouseEvent): void {
goY += 20;
}
function scrollDown(MouseEvent): void {
goY -= 20;
}
*
It seems to indicate the error is here
color_scroll.y += (goY - color_scroll.y) / 20;
But I'm really bummed because I'm not really sure how to proceed from there.
Whenever you gotoAndStop() to a different keyframe, your current frame is invalidated and all its members destroyed. Listeners persist, if they are attached to an object that persists. So, right after you call GoToChooseA(), your color_scroll is destroyed, and then the listener attached to stage is called and tries to modify a destroyed object, there goes your 1009. The solution is either manually remove the event listeners "scrollManage", "scrollUp", "scrollDown" before you change the frame, at least "scrollManage" because it's attached to stage, or stop using frames altogether, but even then you'll have to control your event listeners.
You could add some logic to your function to check if you are in the right frame and then proceed. I am not familiar with frames so the condition would be something like this._currentframe == 2 or timeline.currentFrame == 2.
function scrollManage(Event): void {
if ( condition ) {
color_scroll.y += (goY - color_scroll.y) / 20;
}
}
If you are not at the right frame (in my example that is frame 2), the function does not execute any code.
This error means you are trying to modify something that is no longer existent.

How to fix AS3 TypeError: Error #1009: Cannot access a property or method of a null object reference?

I am getting this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Skool_fla::MainTimeline/frame1()[Skool_fla.MainTimeline::frame1:10]
at flash.display::MovieClip/gotoAndStop()
at Skool_fla::MainTimeline/goback()[Skool_fla.MainTimeline::frame2:22]
What is causing this error and how do I fix it?
This is my code for both the frames:
Frame 1: This is the main menu screen where you can access the credit section
import flash.events.MouseEvent;
//setting up the variables
//events
//stop the timeline
stop();
//the play button
play_btn.addEventListener(MouseEvent.CLICK, playani);
function playani(e:MouseEvent)
{
//asking it to progress to the load menu
gotoAndStop(3);
}
//the credits button
credit_btn.addEventListener(MouseEvent.CLICK, creditslide);
function creditslide(e:MouseEvent)
{
//asking it to go the credits frame
gotoAndStop(2);
}
Frame 2: This is where the credits appear
//
//
//all the imports
//events
var credit:credits_bck = new credits_bck ();
var credits_name: credit_nm = new credit_nm ();
var back_butn: back_button = new back_button ();
addChild (credit);
addChild (credits_name);
addChild (back_butn);
back_butn.addEventListener(MouseEvent.CLICK,goback);
function goback(G:MouseEvent)
{
removeChild (credit);
removeChild (credits_name);
gotoAndStop(1);
}
Either play_btn or back_butn is null. Your error message's line numbers don't correspond to your code so it's hard to say. But the gist is you're trying to access a property of something that isn't anything. Check to make sure you're initializing your variables/references properly.
Maybe your problem is Flash bug too.
In my FLA there was a layer with one empty keyframe. If I puted a vector graphics on it, the error was gone. If there was one or multiple MovieClips and there was no vector graphic - the error was there again.
Then I made a new layer and copy pasted all the objects from damaged layer to new and deleted the damaged layer. It solved the problem.
NOTE: Don't copy the keyframes. Only copy the contents.
Now my project is much more complicated and sadly the error came back again.
Test movie frequently and if the error comes back, check the last keyframes and layers you created.

Parameter child must be non-null error in AS3

I have a code to pause the game, and it runs the pause function as shown:
public function onKeyPress(keyboardEvent:KeyboardEvent) :void
{
//Check for pause
if(keyboardEvent.keyCode == Keyboard.P)
{
//If the timer is still running
if(gameTimer.running)
{
gameTimer.stop();
Mouse.show();
pauseText = new PauseText();
pauseText.x = 150;
pauseText.y = 100;
addChild(pauseText);
//If the player is using the mouse, resume by clicking on the player
if(mouseControl)
{
player.addEventListener(MouseEvent.CLICK, resumeGame);
pauseText.pauseInformation.text = "click on yourself";
}
else
{
pauseText.pauseInformation.text = "press 'p'";
}
}
else
{
//Only allow the player to resume with P IF he is using the keyboard
//This prevents cheating with the mouse.
if(!mouseControl)
{
gameTimer.start();
removeChild(pauseText);
pauseText = null;
}
}
}
}
The game runs perfectly fine. On my first playthrough, the pause functions work. However, if later I die and restart the game, then pause it, I get the following message:
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at Game/onKeyPress()
The game still runs fine though. However, everytime I pause, or unpause, this error appears. If I die again, restart, then pause, TWO of these errors appears. From what I can gather it seems as if it attempts to remove the pauseText…but I’ve been removing it just fine on the first playthrough, I’ve used removeChild() then set as null for other parts of my code and it works fine. Additionally, if I add a trace(“a”); statement right after the function header, I get the error before the “a” appears on the output panel.
What’s wrong?
Additional notes:
If I don’t use the pause function at all for my first playthough, there is no error when I call it up on my second playthrough.
put removeChild into 'if' ,this will solve error :
if(pauseText.parent){
pauseText.parent.removeChild(pauseText);
}
but You should anyway check what is the source of problem , maybe 'gameTimer.running' is false on beggining ?
You probably instantiate another Game object (the one that contains the whole game) while not removing the previous game's event listener. That would explain such behavior, since you have more than one KeyboardEvent.KEY_DOWN listener active, and note that when you're stopping the game, you most likely stop the timer in it, so the "else" clause of your "if (gameTimer.running)" statement is executed, but the timer was effectively stop without pauseText to be generated. So, you miss a
removeEventListener(KeyboardEvent.KEY_DOWN,onKeyPress);
in your game destruction code.
if (!mouseControl) {
gameTimer.start();
if (pauseText && contains(pauseText)) {
removeChild(pauseText);
pauseText = null;
}
}