Copy link to clipboard
Copied
I've set up three buttons in the first frame which are supposed to switch frames. When the program first runs, there are no errors, and i can click any of the buttons and end up where I want. However, when going back to the first frame, the buttons no longer work. The stage listener still works though. I added the "Clicked" output to check if the function was called, which it wasn't. I know I disable the buttons in the code, but only after that button is clicked, and the task is done. I should mention I don't have the code on the timeline, but on a separate document. Here is my code:
<code>
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.SimpleButton;
public class DocumentClass extends MovieClip
{
public var matteOppgave:Boolean = false;
public var engelskOppgave:Boolean = false;
public var flaggOppgave:Boolean = false;
public function DocumentClass()
{
stop();
btnBok.addEventListener(MouseEvent.CLICK, matte);
btnFlagg.addEventListener(MouseEvent.CLICK, flagg);
btnPc.addEventListener(MouseEvent.CLICK, engelsk);
stage.addEventListener(Event.ENTER_FRAME, sjekk);
btnBok.enabled = true;
btnPc.enabled = true;
btnFlagg.enabled = true;
function matte(evt:MouseEvent)
{
gotoAndStop(2);
frame2();
trace("Clicked");
}
function engelsk(evt:MouseEvent)
{
gotoAndStop(3);
frame3();
trace("Clicked");
}
function flagg(evt:MouseEvent)
{
gotoAndStop(4);
frame4();
trace("Clicked");
}
function sjekk(evt:Event)
{
if(matteOppgave == true && engelskOppgave == true && flaggOppgave == true)
{
gotoAndStop(5);
}
if(matteOppgave == true)
{
btnBok.alpha = 0.5;
btnBok.enabled = false;
låsEin.alpha = 0;
}
if(engelskOppgave == true)
{
btnPc.alpha = 0.5;
btnPc.enabled = false;
låsTo.alpha = 0;
}
if(flaggOppgave == true)
{
btnFlagg.alpha = 0.5;
btnFlagg.enabled = false;
låsTre.alpha = 0;
}
}
}
</code>
Copy link to clipboard
Copied
You constructor runs once, when the stage is drawn the first time on frame 1. When you navigate away from frame 1, if those instances are not on stage at the destination frame (and continuously in between, the original instances will be thrown away. When you navigate back to frame 1, new instances are constructed and placed into the variables. Hence, the listeners are listening to objects that no longer exist (technically those objects hold a reference to the callback function and those objects have probably been garbage collected, so in reality there's no listening going on).
Check out my presentation "Solving the frame 2 problem."
Copy link to clipboard
Copied
Thanks for answering!
I didn't get too much out of the presentation since I'm pretty much a beginner in coding. I think I understand the problem, but how do I fix it?
Copy link to clipboard
Copied
You have several choices.
There are plenty of tutorials out there that tell you how to do 1 and 2. To the best of my knowledge, the only person who can and will help you with #3 is me. So you're better off probably picking one of the others if you want to have access to a huge body of material by different people on the approach you want to follow.
Copy link to clipboard
Copied
I got it working from a solution posted on another forum. I made all the frames i had into movieclips, and changed back and forth using addChild() and removeChild(). Saved me alot of work.
Copy link to clipboard
Copied
Thanks a lot!
I got it working again by using just addChild();
Find more inspiration, events, and resources on the new Adobe Community
Explore Now