Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Simple YOYO for text in a textfield without tween and mask

leave a comment »

This script will allow a text to do Yoyo effect without using any Tween/mask

private var textData:String = "This is sample text to have YOYO effect";
private var textField:TextField;
private var textCount:int = 0;
private var direction:String = "LeftToRight";
private var textYoyoTimer:Timer;

public function startTextYoyo():void
{
    textField.text = textData;
    if( textField.textWidth > textField.width ){
        textCount = 0;
        textYoyoTimer = new Timer( 90 );
        textYoyoTimer.addEventListener( TimerEvent.TIMER, moveTextHandler );
        textYoyoTimer.start();
    }
}

public function stopTextYoyo():void
{
    textField.text = textData;

    direction = "LeftToRight";
}

private function moveTextHandler( event:TimerEvent ):void
{
    switch( direction ){
        case "LeftToRight":
            moveLeftToRight();
            break;

        case  "RightToLeft":
            moveRightToLeft();
            break;
    }
}

private function moveLeftToRight():void
{
    textCount++;
    textField.text = ( textData ).substring( textCount );

    if( ( textField.textWidth + 30 ) < textField.width ){
        direction = "RightToLeft";
    }
}

private function moveRightToLeft():void
{
    textCount--;
    if( textCount < 0 ){
        direction = "LeftToRight";
        return;
    }

    textField.text = ( textData ).substring( textCount );
}
Advertisements

Written by Deepanjan Das

September 20, 2010 at 4:49 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: