帧到帧与AS3的帧间转换

时间:2011-08-13 08:54:13

标签: flash actionscript-3

我在“第1帧”中有一个按钮,指向“第2帧”。该文件具有简单的代码:

myButton.addEventListener(MouseEvent.CLICK, gotoFrame02);

function gotoFrame02(event:MouseEvent):void {
    gotoAndStop(2);
}

当帧发生变化时,问题不是转换。是否可以在帧更改时应用Tween转换?

3 个答案:

答案 0 :(得分:2)

您可以轻松地从舞台上获取bitmapData,并在放入MovieClip时淡化alpha。

这是我制作的github回购:

https://github.com/Abrahamh08/Frame-Transitions-AS3

Fade.as,将此文件导入 com 文件夹 videogamecheatsultra ,并使用函数Fade.fadeIntoFrame()淡化帧:

package com.videogamecheatsultra
{
    import flash.display.Stage;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.events.Event;
    import flash.display.MovieClip;
    import flash.display.DisplayObject;

    /*
    This code fades a frame into the frame and/or Scene you define.

    Code by Cornelia Rose LLC (http://www.videogamecheatsultra.com)
    This file is not to be sold and not to be claimed as your file or your code.  Have a nice day!
    */
    public class Fade
    {
        public static function fadeIntoFrame(root, stage, frame:int, Scene:String = null, alphaPerFrame:Number = 0.05)
        {
            var data:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0);
            data.draw(stage);

            var stageData:Bitmap = new Bitmap;
            stageData.bitmapData = data;

            var stageClip:MovieClip = new MovieClip();
            stageClip.addChild(stageData);

            if(Scene != null)
            {
                MovieClip(root).gotoAndStop(frame, Scene);
            }else
            {
                MovieClip(root).gotoAndStop(frame);
            }

            stageClip.x = 0;
            stageClip.y = 0;

            stage.addChild(stageClip);

            var highIndex:int = stage.getChildIndex(stage.getChildAt(stage.numChildren - 1));
            stage.setChildIndex(stageClip, highIndex);

            stageClip.addEventListener(Event.ENTER_FRAME, fadeTo);

            function fadeTo(e:Event)
            {
                e.currentTarget.alpha -= alphaPerFrame;
                if(e.currentTarget.alpha <= 0)
                {
                    e.currentTarget.removeEventListener(Event.ENTER_FRAME, fadeTo);
                    e.currentTarget.parent.removeChild(e.currentTarget);
                }
            }
        }

    }

}

答案 1 :(得分:1)

第1帧:

stop();
myButton.addEventListener(MouseEvent.CLICK, gotoFrame02);
function gotoFrame02(event:MouseEvent):void {
    gotoAndStop(2);
}

第2帧:

stop();
import fl.transitions.Tween;
import fl.transitions.easing.*;
var myTween:Tween = new Tween(myButton, "x", Elastic.easeOut, 0, 300, 3, true);

请参阅此链接了解更多信息。 http://www.zedia.net/actionscript-3-tweens-tutorial/

答案 2 :(得分:1)

这不完全可能,不。使用时间轴从一个帧移动到另一个帧就是这样,并且由于时间轴上的对象(及其状态)在您已经存在之前不可用于代码,因此您无法轻松地向前看以创建补间。我会说你有两个选择,虽然我不知道在不知道第二帧存在的情况下哪个更好。

<强> 1。时间轴动画

不是使用gotoAndStop在第2帧上转到完全不同的状态,而是使用gotoAndPlay,然后在第2帧开始的两个状态之间创建所需的动画。将关键帧放置在动画的结尾包含stop()

<强> 2。忘记时间表

ActionScript 3是面向对象的,当您从时间轴中解脱出来时效果最佳。将第2帧的内容创建为库中的符号,并将符号设置为“Export for Actionscript”。现在,您可以附加此符号,并在单击按钮时将编码的补间应用于此符号。对于基于代码的补间,如果您有文件大小问题,我强烈建议您使用免费的TweenMax库或TweenLite。它们非常易于使用,并且比内置的补间类更高效,功能更强大。您还可以使用TimelineMax将多个补间组合在一起,并将它们作为一个组进行控制。

例如:

import com.greensock.TweenMax;
import com.greensock.TimelineMax;

myButton.addEventListener(MouseEvent.CLICK, gotoFrame02);

var frame2State:Frame2State;

function gotoFrame02(event:MouseEvent):void 
{
    //remove listener for memory management purposes
    myButton.removeEventListener(MouseEvent.CLICK, gotoFrame02);
    //create instance of your next state from the library
    frame2State = new Frame2State();  //assuming that the class name you set for the symbol is Frame2State
    addChild(frame2State);
    //create timeline
    var timeline:TimelineMax = new TimelineMax({onComplete:onTimelineComplete});
    //add button fade down animation
    timeline.append(new TweenMax(myButton,0.4,{alpha:0}));
    //add content fade up animation
    timeline.append(new TweenMax(frame2State,0.4,{alpha:1}));
    timeline.play();
}

function onTimelineComplete():void
{
    //remove button
    removeChild(myButton);
    myButton = null;
}