Search code examples
actionscript-3airtweenautoscrollflash-cs6

How to loop a tween/autoscroll TextField across screen


I'm working in Flash AS3, AIR 3.2 for iOS SDK. I'm trying to make a TextField appear and tween from the right side of the screen automatically to the left side, disappear, then reappear from the right again in a loop.

Trying to work out how I would even start to code this and the logic behind it. At the moment, I have a class called MarqueeTextField which is doing the scrolling. But it only scrolls/marquees letter character to character, and not from point to point on-screen. It also "stutters" when scrolling at slow speeds and not smooth.

public class MarqueeTextField extends TextField
{
    /** Timer that ticks every time the marquee should be updated */
    private var __marqueeTimer:Timer;

    /**
    *   Make the text field
    */
    public function MarqueeTextField()
    {
        __marqueeTimer = new Timer(0);
        __marqueeTimer.addEventListener(TimerEvent.TIMER, onMarqueeTick);
    }

    /**
    *   Callback for when the marquee timer has ticked
    *   @param ev TIMER event
    */
    private function onMarqueeTick(ev:TimerEvent): void
    {
        this.text = this.text.substr(1) + this.text.charAt(0);
    }

    /**
    *   Start marqueeing
    *   @param delay Number of milliseconds between wrapping the first
    *                character to the end or negative to stop marqueeing
    */
    public function marquee(delay:int): void
    {
        __marqueeTimer.stop();
        if (delay >= 0)
        {
            __marqueeTimer.delay = delay;
            __marqueeTimer.start();
        }
    }
}
}

I've searched and there doesn't seem to be any that uses ONLY AS3 (they use the Flash GUI). Anyone able to explain to me how I'd begin to code this and make it smooth even at slow speeds?


Solution

  • If you really must, you can always use GreenSock's Tweening engine. Just import the package and simply use TweenLite.to(). Also, it looks like you're just inserting text instead of actually changing the textbox's position.