在主时间轴上创建一个随机帧(来自数组)的按钮。如何从影片剪辑?

时间:2017-09-20 21:47:33

标签: actionscript-3 flash random actionscript adobe

我看到很多问题,询问如何在影片剪辑中创建一个按钮,该按钮导致主时间轴中的一个框架,并查看它们。但是如果你想在一组特定的帧中去一个随机帧,它会有所不同吗?我从来没有真正使用AS3,除了像stop()之类的简单事情;或gotoAndPlay。

这是我目前的主要时间表: Figure 1

这是我到目前为止使用Google获得的代码:

var frameB:Array=[1,28,45,56,71,91,106,126];
blue_circle1.addEventListener(MouseEvent.CLICK, choose);

function choose1(event:MouseEvent):void {
    var randomFrame:Number = frameB[Math.floor(Math.random() * frameB.length)];
    trace(randomFrame);
    gotoAndPlay(randomFrame);
}

当我在主时间轴中的旋转按钮上使用它时,代码工作正常。但是当我把它放在电影剪辑中的按钮时它不起作用。我需要改变它以便它起作用。如果有更好的方法,我愿意尝试。

编辑:我应该澄清一些事情。车轮上的矩形从左向右移动。它在影片剪辑中执行此操作。我希望按钮随之移动。但是当我将按钮放入所述影片剪辑时,按钮上的代码停止工作。我希望我最终不会让事情变得更加混乱。

2 个答案:

答案 0 :(得分:1)

当您将按钮和/或代码放在movieClip中时,它会更改gotoAndPlay()所指的movieClip。您需要指定要调用gotoAndPlay()的movieClip。对于主时间轴gotoAndPlay()有效,但在movieClip中你必须使用它:

parent.gotoAndPlay(randomFrame);

或者您可能需要将父类型设置为MovieClip,如下所示:

MovieClip(parent).gotoAndPlay(randomFrame);

但是,最好使用外部.as文件,因为它可以让您最大程度地控制代码。

  • 将以下代码保存在名为' MyFlashAnimation.as'
  • 的文件中
  • 创建一个名为' mycodefolder'的文件夹。并将其放在与.fla。
  • 相同的目录/文件夹中
  • 使用' MyFlashAnimation.as'作为您的文档类。在Flash / Animate IDE中,找到“属性”面板,然后找到“发布”部分
  • 在“发布”部分中,其中显示了“' Class'输入:mycodefolder.MyFlashAnimation(不添加.as)

CODE:

package mycodefolder {

    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class MyFlashAnimation {

        private var animationClip:MovieClip;
        private var blueCircle1:Button;

        private var frameB:Array = [1,28,45,56,71,91,106,126];

        // constructor
        public function MyFlashAnimation() {

            // this your main robot/car animation
            // this assumes animation_clip is on the main stage
            animationClip = this.animation_clip;

            // this is your button. this assumes blue_circle1 is a child of
            // your animation_clip. update the path if necessary.
            // for example, it might be: animationClip.robot_body.blue_circle1
            blueCircle1 = animationClip.blue_circle1;

            // add listener
            blue_circle1.addEventListener(MouseEvent.CLICK, choose);
        }

        function choose1(event:MouseEvent):void {
            var randomFrame:Number = frameB[Math.floor(Math.random() * frameB.length)];
            trace(randomFrame);

            // tell animation clip to gotoAndPlay
            animationClip.gotoAndPlay(randomFrame);
        }

    }
}

那应该有用。如果您的所有movieClip路径都正确无误。

答案 1 :(得分:0)

  

"当我在主时间轴中的旋转按钮上使用它时,代码工作正常。但是当我把它放在一个按钮中时,它不起作用   电影剪辑。"

将按钮粘贴到的MClip的实例名称是什么?该MC名称将添加"到你按钮的最后一条路。

示例:

(1)如果您的 blue_circle1 在舞台上(您已经可以这样做):

blue_circle1.addEventListener(MouseEvent.CLICK, choose);

...对比

2)如果 blue_circle1 位于另一个MClip内(示例名称为: thingMC ):

thingMC.blue_circle1.addEventListener(MouseEvent.CLICK, choose);