从数组中设置动画文本

时间:2009-03-09 19:49:02

标签: flash actionscript-3 arrays

我有这个代码,它应该带有一些单词的数组,并将指定的超时时间逐个应用于它们。我想我必须从它们中制作一个空的movieclip,然后使用带有超时的foreach循环为它们制作动画,但是我很想知道我将如何做到这一点。

这就是我所在的地方:

var array:Array = new Array("word", "is", "here");

var bounce:Function = function(mc:MovieClip):void {
mc.bounce1 = new Tween(mc, "_y", Bounce.easeOut, 35, 75, 1, true);
mc.bounce2 = new Tween(mc, "_xscale", Bounce.easeOut, 0, 400, 4, true);
mc.bounce3 = new Tween(mc, "_yscale", Bounce.easeOut, 0, 400, 4, true);
mc.bounce4 = new Tween(mc, "_alpha", Regular.easeInOut, 100, 0, 2, true);
};
array.forEach(bounce, me);

真的需要一些帮助。

2 个答案:

答案 0 :(得分:3)

我没有CS3,但是我使用名为Tweener的免费补间库掀起了一个快速示例。你可以找到它here

import flash.events.Event;
import flash.display.MovieClip;
import flash.text.TextField;    
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
/* this is a free Tweening library. I don't have flash CS3 but this is analogous to fl.transitions.Tween */

import caurina.transitions.Tweener; 

public class Main extends MovieClip 
{
    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);

        /* your array of words */
        var array: Array = new Array("word", "is", "here")

        /*we'll use this to control the position of each word*/
        var x : int = 0

        var delay : Number = 0.0

        for each(var s : String in array)
        {
            var word: TextField = new TextField()
            word.autoSize = TextFieldAutoSize.LEFT

            /* here we can adjust text format */
            word.defaultTextFormat = new TextFormat(null, 75, 0xff0000)

            word.text = s
            word.x = x
            word.y = 75

            /* put the new word into our MovieClip */
            addChild(word)

            /* apply some tweens */
            bounceText(word, delay)

            /* adjust our animation and position variables */
            delay += 1.3
            x += word.width
        }
    }

    private function bounceText(a_textField : TextField, a_delay : Number) : void
    {
        /* duration of each tween */
        var t:Number = 0.75

        /* this is the "up" part of the tween, from y=75 to y=35 */
        Tweener.addTween(a_textField, { y:35, delay:a_delay, time:t, transition:"easeOutQuad" } );

        /* this is the "down" part of the tween. note the "delay" parameter is offset by the time of the first tween.
         * the "onComplete" function calls BounceText again when the second Tween is complete. If you are using fl.transitions.Tween,
         * you can add an event listener for a TweenEvent.MOTION_FINISH event, or set the "looping" property of the Tween to true.
         * */
        Tweener.addTween(a_textField, { y:75, delay:a_delay + t, time:t, transition:"easeInQuad", onComplete:bounceText, onCompleteParams:[a_textField, 0.25] } );
    }
}

答案 1 :(得分:0)

您可能能够链接 Tweens,具体取决于您的Tween引擎。 不确定你的Tween引擎到底是什么,这是一种更经典的方法。 animateNext()函数在开头调用一次,之后只能由最后一个Tweens onComplete处理程序(或Timer方法)调用:

var wordList:Array = new Array('one','two','three');

// Keeps track of the current showing word
var currentIndex:int = -1;

// Starts the animation
animateNext();

function animateNext():void
{
   // increments the word counter
   currentIndex ++;

   // resets the word count if all the words are done
   if(currentIndex >= wordList.length)
      currentIndex = 0;

   // Apply the right word here
   var word:String = wordList[currentIndex];
   trace(word);

   // animation tweens here :
   ...

   // place callback function onComplete to animateNext()

}