New to CS5 and AS3 so if I am making a fundamental mistake please don’t hesitate to correct me.
I am trying to build a fairly lengthy and complicated form. So this will require navigation through different pieces of it. I am new to Flash and AS3 so I started with some prototyping and got two buttons to navigate forward and backwards in the timeline. My problem is now when I am trying to bring this out of the “Code Snippet” (correct term?) area and into my main ActionScript file. The buttons appear, but pressing them does not execute the MouseEvent.
So two questions.
1. Am I doing this right?
2. Why doesn't MouseEvent work when the code is in the .as file?
Form.fla – frame 1 Code Snippet
var form:Form = new Form();
addChild(form);
Form.as
package
{
import flash.display.MovieClip;
import fl.controls.Button;
import flash.events.MouseEvent;
public class Form extends MovieClip
{
private var nextButton:Button;
private var prevButton:Button;
public function Form()
{
setupNavigation();
}
private function setupNavigation():void
{
nextButton = new Button();
nextButton.label = "Next";
// ... size and position code
nextButton.addEventListener(MouseEvent.CLICK, moveForward);
prevButton = new Button();
prevButton.label = "Previous";
// ... size and position code
prevButton.addEventListener(MouseEvent.CLICK, moveBackward);
addChild(nextButton);
addChild(prevButton);
}
// Setup Mouse events
private function moveForward(event:MouseEvent):void
{
nextFrame();
}
private function moveBackward(event:MouseEvent):void
{
prevFrame();
}
}
}
You have to pass a reference of your main timeline to your Form class, using a setter function
in your Form class (not a snippet, class is the correct term), add the following function and variable:
then in your move forward/backward functions change the prevFrame() and nextFrame() to:
There are several ways to accomplish what you are trying to achieve, meaning the method of changing sections. Your way is one way to do it. There are maybe some better approaches but your approach here is not glaringly wrong or anything. 🙂