简体   繁体   English

Flash CS5 + AS3时间线导航

[英]Flash CS5 + AS3 Timeline Navigation

New to CS5 and AS3 so if I am making a fundamental mistake please don't hesitate to correct me. CS5和AS3的新手,所以如果我犯了一个根本性的错误,请立即纠正我。

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. 我是Flash和AS3的新手,所以我开始做一些原型制作,并有两个按钮可以在时间轴上前后导航。 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. 现在的问题是,当我试图将其从“代码片段”(正确的术语?)区域带入我的主ActionScript文件时。 The buttons appear, but pressing them does not execute the MouseEvent. 出现按钮,但按它们不会执行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 Form.fla-框架1代码段

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 您必须使用setter函数将对主时间轴的引用传递给Form类

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: 在您的Form类(不是摘录,类是正确的术语)中,添加以下函数和变量:

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: 然后在前进/后退功能中将prevFrame()和nextFrame()更改为:

_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. 更好,更干净的方法是使用Document类。 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. 通过使Form类充当时间轴本身,Document Class将不需要将时间轴引用传递给Form类。

Here's how you would do it. 这是您的操作方式。 In your fla file deselect any object you have selected then view the properties toolbar. 在fla文件中,取消选择已选择的任何对象,然后查看属性工具栏。 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. 输入与fla相关的类的相对路径,在这种情况下,这只是类名,因为您没有定义包。 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. 文档类始终必须扩展DisplayObjectContainer,其中包括MovieClip和Sprite,因此您已经编写的Form类应该可以正常工作。

Once that is done you can call nextFrame() and prevFrame() as you did in your question and it should work fine. 完成后,您可以像在问题中一样调用nextFrame()和prevFrame(),它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM