Flash CS 5.5 Actionscript 3 - Buttons appearing on all frames - actionscript-3

I have a program that lets the user go to various functions, that all work. However when I use the music player function that i made the play and stop buttons remain on screen when i click back to return to the menu of options. Here is my code:
play_button.addEventListener(MouseEvent.CLICK,clickhandler);
stop_button.addEventListener(MouseEvent.CLICK,clickhandler);
function clickhandler(event:MouseEvent): void{
swapChildren(play_button, stop_button)
}
The music file is embedded in the buttons.
The following error appears every time:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at SmartHouse_Scene1_fla::MainTimeline/frame10()
at flash.display::MovieClip/gotoAndStop()
at SmartHouse_Scene1_fla::MainTimeline/fl_ClickToGoToAndStopAtFrame_27()
It should be noted that the error only occurs when the music has been played. I can go to the page and leave without the problem if i dont touch the play or stop button. If anyone could help i would really appreciate it.

if reading this correct the sounds are inside the button on the timeline, are you using buttons or movieclips, make sure that frame 27 exist on the movieClip timline.
//you can use// myMovieClip.visble = false;
example:
play_button.addEventListener(MouseEvent.CLICK,clickhandler);
function clickhandler(event:MouseEvent): void{
stage.myMovieClip.visible = false; // the name of your button clip
}

Related

adobe flash show/hide different movieclips in different frames with one button Error #1009

I created a button to a layer and I am trying to show movieclip com7 in frame 1 when I click the button named quest. Then, I would like to show a different movieclip com9 in frame 2. I put the movieclips in another layer each one in frames1 and 2.
In frame1 the code is:
quest.visible=true;
com7.visible=false;
quest.addEventListener(MouseEvent.CLICK, q7_clicked);
function q7_clicked(event:MouseEvent):void
{
if (com7.visible==false)
{com7.visible=true
}
else
{
com7.visible=false;
}
}
in frame 2:
quest.visible=true;
com9.visible=false;
quest.addEventListener(MouseEvent.CLICK, q9_clicked);
function q9_clicked(event:MouseEvent):void
{
if (com9.visible==false)
{com9.visible=true
}
else
{
com9.visible=false;
}
}
Flash creates the swf without errors but when I click the button in frame2 there is a TypeError:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at meli_fla::MainTimeline/q7_clicked()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at meli_fla::MainTimeline/q9_clicked()
The issue is that when you add event listeners on the timeline, those listeners do not automatically go away when you change frames (no code does).
So on frame 1, you just have the one listener and it probably works fine.
On frame 2, you create a new listener, but the one from before on frame 1 is also still hanging around, so when you click the quest button, it actually calls q7_clicked and q9_clicked. No matter what frame you are one, at this point clicking the button you added the listeners to will always call both functions.
Your error, is because the objects you're referencing (com9, com7) are likely not around on both frames you are visiting (confirmed from you comments on the question).
To remedy the problem, you need to remove the appropriate event listener when you move to a new frame.
So, wherever in your code you do nextFrame(); or gotoAndStop(2); or however you move the user to another frame, at that time remove the listener on the button:
quest.removeEventListener(MouseEvent.CLICK, q7_clicked);
gotoAndStop(2);
Or, if returning to frame 1:
quest.removeEventListener(MouseEvent.CLICK, q9_clicked);
gotoAndStop(1);

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.

ActionScript 3: eventlisteners give 1009 error when not in frame1

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

addEventListener and gotoAndStop for a menu in a game gives error

I'm new at flash as3. Today, I am trying to code a menu in a game which will refer to a startscreen for a game to show the buttons for the game.
In this startscreen, there is a button to link back to the menu. It doesn't work. I am getting this,
Error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at SuperSpill_fla::MainTimeline/frame1()
It looks like to me the only possible thing that could have happened is that mus doesn't exist anymore. This suggests that the code is running in a frame where mus is no longer on stage.
I would expect that in most versions of the Flash Player that the frame wouldn't fully render between goToAndStop and the next line, but my suggestion would be to either swap the order of those lines or change it to
event.currentTarget.removeEventListener(MouseEvent.CLICK, musSpill)

Actionscript 3: gotoAndStop

I have a project in Flash CS3 that's giving me a bit of trouble. I have a movieclip, and in that movieclip, I have a button. The movieclip is named bg and the button tohallway_btn. My coding is on the stage on a layer, not on classes or in a package, or anything of that sort. This is my coding:
bg.tohallway_btn.addEventListener(MouseEvent.CLICK, tohallwayClick);
function tohallwayClick(event:MouseEvent):void{
gotoAndStop (141);
}
It seems simple enough, yet when I debug and the button is clicked, the flash player freezes over. I have absolutely no idea what's causing it to do this.
I get a type error in output as well:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Camille_fla::MainTimeline/enterF()[Camille_fla.MainTimeline::frame140:130]
Any help is appreciated.
An onEnterFrame listener was called and not removed that was referencing an object (bg) that was not on the stage after the goto call.
function tohallwayClick(event:MouseEvent):void {
**removeEventListener(Event.ENTER_FRAME, enterF);**
gotoAndStop(141);
}
First make sure your button and your code are on the same frame, they can be on different layers, but make sure they are lined up.
If you want it to go to the frame on your main timeline, or stage, instead of writing:
gotoAndStop (141)
try:
stage.gotoAndStop(141);