ActionScript 3: eventlisteners give 1009 error when not in frame1 - actionscript-3

Thanks for the responses
*Note when the buttons and code are on Frame 1 it works perfectly *
All the script is in Frame 2, as are the two buttons. The only things not in frame to are the sprites I'm calling out of the library.
Ideally On frame 1 are navigation buttons - each button with a gotoAndPlay() call attached. When you click each one of these navigation buttons it takes you to a different page.
Each page has a a bunch of buttons. Each button, when clicked, plays a an audio, and adds/deletes children to the stage
My problem is any frame other than 1, (in this case 2) as soon as it lands on the frame even with appropriate buttons present it says it doesn't see them and balks at the even listeners.
I hope this makes sense.
----- Original post --------
I'm trying to understand how these things work. I have an empty frame in frame one. In that frame I have the code:
gotAndStop(2);
On frame 2 I have two buttons. I've added event listeners to them. This works fine. The problem is as soon as it hits frame two I get this error :
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at TesT_2_fla::MainTimeline/frame2()
The confusing thing is, when I have everything in Frame 1 it works like a charm.
On frame 2 the buttons are there already, if I put them in frame 1 it works, but in frame 2 no dice.
Can anyone explain what is happening and how I might be able to remedy this?

Not sure why you are writing the script in Frame 1. If nothing were in Frame 1, the user would automatically go to Frame 2, if you placed a stop(); on Frame 2. If you want to stop(); on Frame 1, you could add a button gotoAndStop(2) with an event listener. When placing script on a frame that may be also referenced in a later frame, use a separate script layer and extend that layer from the (key)frame on which the script first appears to the (key)frame on which it is also referenced. HTH

Ya know - after much searching I found a solution it does seem awfully cumberome. I added the following code (which I copied from someone elses post) Just in case any other noobs have a similiar problem
enter code here
//listen for the Flash player's ENTER_FRAME event...
this.addEventListener(Event.ENTER_FRAME, onEnterFrame, false);
//and call this checker function continually until all of the buttons are accounted for
function onEnterFrame(e:Event):void
{
if(and_btn != null && big_btn != null )
{
and_btn.addEventListener(MouseEvent.CLICK, fAnd);
big_btn.addEventListener(MouseEvent.CLICK, fBig);
//clean up the enter frame listener so that this function no longer gets called
this.removeEventListener(Event.ENTER_FRAME, onEnterFrame, false);
}
}
It seems the code was running so fat the buttons didn't have a chance to populate the stage

Related

Flash CS6 Button Not Working Despite No Compiler/Output Errors

I'm using Flash CS6, AS3 to create buttons for my project. Below is my code:
Intro_btn.addEventListener(MouseEvent.MOUSE_DOWN, Intro_func);
function Intro_func(event:MouseEvent):void {
gotoAndStop("Intro");
}
No errors appear when I run it on the output and compiler panel, and the same happens when I run it through the debugger. Also, I have used the exact same code for five other buttons, and they have no problems working. Please can someone tell me what is wrong with my code???
I would need to know more info of your code and the type of buttons you're using, however I would try this steps:
1.- add a Button component from the component inspector, set its instance name, and add that same function to the Click event:
newButton.addEventListener(MouseEvent.CLICK, Intro_func);
function Intro_func(event:MouseEvent):void {
trace("function executed");
gotoAndStop("Intro");
}
2.- run your app and test if the action is executed with this new button. If this test pass, then your problem is your button.
check this on your button:
check the base class of the button (be sure its a MovieClip or a Button class)
check if your button is enabled/disabled.
check if your button or its children has mouseEnabled and mouseChildren set as
true.
check there is no other object on top of the button either in your artboard or added by code at runtime.
I finally would suggest you to set useHandCursor = true; this will replace your mouse pointer arrow to a hand when you move over your button (this is just to test that your button is actually interacting with mouse).
hope this helps.
sorry for the delay, But I've found your solution, It's easier than you thought: your frame is delayed.
your Intro_btn script is on frame 10, however in frame one you're telling it to go to frame 9 (the "Erhu" frame label) so the actions to setup the event listener are never being called.
Also keep in mind the following:
objects exists only in their frames life time, if you have an object from frame 5 to 10, it will only exist in there, so moving to a previous or later frame (e.g. frames 4 and 11) will internally delete the object in memory along with their asociated actions, in english:
you placed the button in frame 10 and added its MOUSE_DOWN listener however, if you go back to frame 0, since the button doesn't exists in this frame it will be deleted from memory along with its listeners, so if you go from frame 0 to any other frame different than 10, your button will never have its listeners associated.
So my suggestion:
1.- add your function into frame 0:
function Intro_func(event:MouseEvent):void {
trace("function executed");
gotoAndStop("Intro");
}
2.- create a new layer. In this layer add a keyframe at the same position where your Intro_btn is(frame 10), and fill the rest of the timeline of this layer with empty frames (no keyframes), finally in the same layer add in frame 10 your listener Intro_btn.addEventListener. this way, the action is available for every subsecuent frame from frame 10.
hope this solves your problem.

Flash CS3 null reference to button/mc after returning to Frame

I've been researching this issue for hours and while I've found somewhat similiar situations, i have yet to find a simple fix. Basically I have a timeline where some animation plays. Eventually I get to my main game screen (frame 256) where a stop(); is called and the user then gets to click on one of 3 doors. Clicking on any door takes the user ahead a bunch of frames and they get to play a game. Once the game is done or the user clicks back, it takes the user back to the original frame (frame 256) and every single time it does this, it says my "upDoor" is a null reference and then the upDoor button(instance of upStairsDoor button) is no longer on the stage.
This seems to happen regardless of which door the user picks first. If the user picks the upDoor and plays the minigame there or picks the outsideDoor and plays that specific minigame, when the user returns to this frame (frame 256), it throws this error on the door and then of course because it throws an error, nothing else works at that point and I have to exit the game.
It's not a typo! Please don't suggest I check my instance names. As I mentioned, the door works fine the first time you get to the frame, it's just when you go back to it. I've read that it might have to do with the garbage collector but when we return to the frame, shouldn't it recreate all of the instances that I've placed to the stage? It doesn't error on ANY other button or movieClip that I've dragged to the stage, only this one particular door.
I forgot to mention that it's error'ing on a line of code that references the upDoor button. I have these lines of code here...(frame 256)
if (downDoor.enabled) {
downDoor.enabled = false;
}
if (upDoor.enabled) {
upDoor.enabled = false;
}
if (outDoor.enabled) {
outDoor.enabled = false;
}
What these do is disable the doors until the user clicks another object on the screen which then runs a function that sets all the doors to enabled. The error in question is saying I can't access a property of a null reference.
Thanks for the input guys. What ended up being the solution for me was to implement all of the movieclips and buttons programatically when the frame loads and then remove them all when I switch frames. That way, everytime the frame is reloaded from a gotoAndPlay, everything gets re-created again.

how to set actionscript code to execute only once

Okay so I have a movieclip called a_mc, if you click the movieclip, it goes to frame 5, and then on frame 5 there is a button called close_btn where if you click the button, it goes back to frame 1 and it is supposed to make a_mc invisible. Here is the actionscript code for frame 1.
stop();
a_mc.addEventListener(MouseClick.CLICK, aClicked);
function aClicked(event:MouseEvent):void {
gotoAndStop(5);
}
and on frame 5, the actionscript code is
stop();
close_btn.addEventListener(MouseEvent.CLICK, closeCLicked);
function closeClicked(event:MouseEvent):void {
gotoAndStop(1);
a_mc.visible = false;
a_mc.removeEventListener(MouseEvent.CLICK, aClicked);
}
see, the problem is, in frame 5, I make a_mc invisible and remove the event listener and go back to frame 1 and on frame one, it always executes the actionscript code so it again creates the event listener and makes a_mc visible. Any idea on how to stop this from happening?
I tried putting the code from frame 1 into a package and then a class and then a constructer method but it is saying
"Syntax error: package is unexpected"
Could you put all the code that you want to execute once in frame 1? - don't call stop() and let it run to the next frame.
Then put the rest of your code in other key frames and don't use gotoAndStop(1) so frame 1 is only called once?
You can try not removing the event listener on a_mc in frame 5, and then in frame 1 check if the event listener is already present (a_mc.hasEventListener()) as a signal that frame 1 has already been shown. Not exactly a 'bets practices' solution, but it might work.
Unfortunately, depending on the actual conents of those clip, and what happens in other frames, it may be the problem you're having is a consequence of the way movieclip object works in flash. When a frame is changed, flash instantiates new objects on the stage (added in new frame), and removes the ones not needed anymore (depending on the contents, but generally it's true). The 'a_mc' object that you manipulate in frame 5 may not be the same 'a_mc' object that is on the stage when you go back to frame 1. It may have been deleted and reinstantiated in the meantime.
To avoid things like that, it would be a better solution to have controlling code in a class outside of the timeline of the animating clip, or at least to keep the state in a separate object. I work in Flash Builder so I can't help you with the details of such organization in Flash Pro (which I presume you're using), but you could probably have all code on the frame 1 of the main clip, and then put the other movieclips with buttons and stuff as children of the main clip. That way main clip can control the state, and know what to show when.

If statement in AS3 not working

This is an if stament on a frame on the root. I want to loop Carrera(a lenghthy movieclip) from frame 2 back to frame 1. (For testing purposes)
This is the code:
if (MovieClip(root).Carrera.currentFrame==2){
MovieClip(root).Carrera.gotoAndPlay(1);
}
The MovieClip keeps going, ignoring the if statement.
What I'm doing working?
There's no bug with the if statement, it's just not being evaluated when you expect it to be.
When you place code in a frame, it gets executed right away, when that frame gets entered. So, when the first frame starts, the if is executed, whose condition is false at that time. And it never gets re-executed, because you never tell it to be. There is no such thing as "standing orders" in AS3 ;-)
Instead, you can check on every frame by adding an event listener:
addEventListener(Event.ENTER_FRAME, function (e) {
if (MovieClip(root).Carrera.currentFrame==2){
MovieClip(root).Carrera.gotoAndPlay(1);
}
});
Or, you can just place gotoAndPlay(1); on the second frame of Carrera (not the root).
You have to understand that you are running this if statement only once. Even if the Carrera clip is at frame 2 in that exact moment, the clip will jump to play 1 and continue playing - there is nothing to make it do the jump again, and thus there can never be a loop.
In order for this to work, you have to run this same statement again and again - every time the clip jumps to a new frame.
For example, you can do this by a) attaching this script to frame 2 of the Carrera clip (not the root!):
gotoAndPlay(1);
or b) adding an event listener to it:
MovieClip(root).Carrera.addEventListener (Event.ENTER_FRAME,
function ( ev:Event ) : void {
var cl:MovieClip = ev.target as MovieClip;
if (cl.currentFrame == 2) cl.gotoAndPlay(1);
}
There are many more ways to do this, but unless you are going to do more complicated things than jumping to frames every now and then, I would advise you to go for the first option - it seems you should learn more about ActionScript before trying out event listeners.
Things to test...
Is MoveClip(root) defined at the point in execution?
Is MoveClip(root).Carrera defined at the point in execution?
Is MovieClip(root).Carrera playing (or have you called stop on it so it's just sittin in frame 1?

In actionscript 3, how do i address objects that aren't on frame 1?

I have an object (button) on frame 2 of my timeline, with the name btnMenu. When clicked, i want to return to frame 1 in the timeline. Actionscript 3 does not allow me to bind the eventlistener to the button from my code on frame 1.
Layout:
layer 1: Actions, only on frame 1
layer 2: btnMenu, only on frame 2 (with an empty frame in front)
Code:
stop(); // don't automatically go to frame 2
btnMenu.addEventListener(MouseEvent.CLICK, function() { gotoAndStop(1); }
(and other code to go to frame 2 obviously)
The error i get is "can't find method/property of object that is null" (roughly translated).
Please help?
You can only access objects that are on the same frame where your code lives. You have several ways to get around this. Depends how you want to structure you application.
You can move all your buttons to the frame where you have the code, toggle their visibility to false and once you reach the frame where the button should be visible just set it to true.
Other way is to move the addEventListener code to the same frame of your button. You can still access your code in the first frame, and call function if you need so.
Consider the following:
// code in first frame
stop();
function goto(evt:MouseEvent):void {
gotoAndStop(1);
}
// code in button frame
stop();
btnMenu.addEventListener(MouseEvent.CLICK, goto);