开发者

Flash CS5 + AS3 Timeline Navigation

开发者 https://www.devze.com 2023-02-11 08:23 出处:网络
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 di

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 butt开发者_运维技巧ons 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

var form:Form = new Form();
form.mainTimeline = this;
addChild(form);

in your Form class (not a snippet, class is the correct term), add the following function and variable:

private var _mainTimeline:Object;

public function set mainTimeline(mtl:Object):void
{
    _mainTimeline = mtl;
}

then in your move forward/backward functions change the prevFrame() and nextFrame() to:

_mainTimeline.prevFrame();
_mainTimeline.nextFrame();

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. :)


A better and cleaner way to do this is to use a Document class. A Document Class would remove the need to pass a timeline reference to the Form class by making the Form class act as the timeline itself.

Here's how you would do it. In your fla file deselect any object you have selected then view the properties toolbar. Under the "Publish" header you will see an editable text field with "Class:" written next to it. Enter the path to you class relative for your fla, which would be just the class name in this case because you don't have a package defined. So just type "Form" in there. A document class always has to extend a DisplayObjectContainer, which includes MovieClip and Sprite so the Form class you have already written should work perfectly.

Once that is done you can call nextFrame() and prevFrame() as you did in your question and it should work fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消