Flex Blog

Search:
  • Home
  • Examples
    • Thumb

      Flex Examples

      Check out our Flex Examples!

    • Thumb

      Flash Builder Examples

      Check out our Flash Builder Examples!

    • Thumb

      AIR Examples

      Check out our AIR Examples!

    • Thumb

      Flex Mobile Examples

      Check out our Flex Mobile Examples!

    Adobe® Flex, Adobe® Flash Builder and Adobe® AIR are registered trademarks of Adobe Systems.
  • Components
    • Thumb

      WP Flex Contact Form

      Check out our WP Flex Contact Form!

    • Thumb

      Flash CountDown Plugin

      Check out our Flash CountDown Plugin!

    This is an overview of all our Flash/Flex based Components.
  • Jobs
  • Flex Books
  • Forum
  • Contact Us
Subscribe to Flex BlogSubscribe
  • Examples
  • iOS
Browse > Home / Examples / Flex 4 Effect Example: Sliding text using the Move Effect

Flex 4 Effect Example: Sliding text using the Move Effect

19 April 2010

Line Break

Author: Arjan (47 Articles) - Author Website

Arjan is a SAP Consultant specialized in ABAP and Front End development techniques like Web Dynpro, Adobe Interactive Forms, Flex and AIR. In his free time he likes to create examples for Flex-Blog and other applications using Flex, AIR and PHP. Other hobbies are movies and music. He is also the co-owner of Flex-Blog.com.


Flex 4 comes with a bunch of standard effects you can use to create some cool animations in your application. In this example, I wanted to slide a text in and out again.

Here’s a preview of what we are going to create:


First of all, we are going to use 2 different instances of the Move effect to get the bahaviour we want. The first one is going to take care of sliding the text in, the second one is going to slide the text out:

1
2
3
4
5
6
<fx:Declarations>
    <s:Move id="moveIn" duration="1000" xFrom="500" xTo="90"
            effectEnd="moveInEnd(event)"/>
    <s:Move id="moveOut" duration="1000" xFrom="90" xTo="-500"
            effectEnd="moveOutEnd(event)"/>
</fx:Declarations>

As you can see, both instances have event listeners on the effectEnd event, lets create those:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * Triggered when moveIn effect finishes
 *
 */

private function moveInEnd(event:EffectEvent):void
{
    // Create new timer with showingTime duration (2000ms / 2sec)
    timer = new Timer(showingTime);
    // Add event listener to the TimerEvent.TIMER event
    timer.addEventListener(TimerEvent.TIMER, handleTimerTick);
    // Start the timer
    timer.start();
}

/**
 * Triggered when moveOut effect finished
 *
 */

private function moveOutEnd(event:EffectEvent):void{
    // Create new timer with notShowingTime duration (10ms)
    timer = new Timer(notShowingTime);
    // Add event listener to the TimerEvent.TIMER event
    timer.addEventListener(TimerEvent.TIMER, handleTimerTick);
    // Start the timer
    timer.start();
}

Some global variables and the initialize function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import mx.events.EffectEvent;

// Timer variable
private var timer:Timer;
// Time to show the text
private var showingTime:Number = 2000;
// Time to wait until the text is shown again
private var notShowingTime:Number = 10;

/**
 * Initialize app.
 *
 */

private function init():void
{
    // Set message visible
    myMessage.visible = true;
    // Create first timer
    timer = new Timer(showingTime);
    // Add event listener
    timer.addEventListener(TimerEvent.TIMER, handleTimerTick);
    // Start timer
    timer.start();
}

The event listener for the timer variable, triggered when the timer “ticks”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Triggered when the timer ticks
 *
 */

private function handleTimerTick(event:TimerEvent):void
{
    // Remove event listener, will be added again
    // (we don't need more than one, will cause strange behaviour)
    timer.removeEventListener(TimerEvent.TIMER, handleTimerTick);
   
    // Toggle message visibilty based on current visibility
    // (visible == true becomes visible is false and
    // visible = false becomes visible is true)
    myMessage.visible = !myMessage.visible;
}

The layour for this example, just a bordercontainer with rounded corners with a Label inside it. Notice that the label has the showEffect and hideEffect declared, they’re pointing at the Move effect instances we declared in the beginning. By toggling the visibility of the label, these effects are being triggered. Furthermore, the label is clickable and opens a URL when the user clicks on it.

1
2
3
4
5
6
7
8
<s:BorderContainer cornerRadius="5" width="450" height="30">
   
    <s:Label x="90" id="myMessage" text="Hello from www.Flex-Blog.com! I'm Clickable!"
             fontWeight="bold" width="100%" height="100%" verticalAlign="middle"
             buttonMode="true" useHandCursor="true" visible="false" maxDisplayedLines="1"
             click="navigateToURL(new URLRequest('http://www.flex-blog.com'), '_blank')"
             showEffect="{moveIn}" hideEffect="{moveOut}"/>
</s:BorderContainer>

Here’s the result again, view source is enabled.

Here’s the complete source code for this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               initialize="init()" width="450" height="50"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               viewSourceURL="srcview/index.html">
   
    <fx:Declarations>
        <s:Move id="moveIn" duration="1000" xFrom="500" xTo="90"
                effectEnd="moveInEnd(event)"/>
        <s:Move id="moveOut" duration="1000" xFrom="90" xTo="-500"
                effectEnd="moveOutEnd(event)"/>
    </fx:Declarations>
   
    <s:layout>
        <s:VerticalLayout paddingLeft="0" paddingTop="5" paddingBottom="5"/>
    </s:layout>
   
    <fx:Script>
        <![CDATA[
            import mx.events.EffectEvent;
           
            // Timer variable
            private var timer:Timer;
            // Time to show the text
            private var showingTime:Number = 2000;
            // Time to wait until the text is shown again
            private var notShowingTime:Number = 10;
           
            /**
             * Initialize app.
             *
             */

            private function init():void
            {
                // Set message visible
                myMessage.visible = true;
                // Create first timer
                timer = new Timer(showingTime);
                // Add event listener
                timer.addEventListener(TimerEvent.TIMER, handleTimerTick);
                // Start timer
                timer.start();
            }
           
            /**
             * Triggered when the timer ticks
             *
             */

            private function handleTimerTick(event:TimerEvent):void
            {
                // Remove event listener, will be added again
                // (we don't need more than one, will cause strange behaviour)
                timer.removeEventListener(TimerEvent.TIMER, handleTimerTick);
               
                // Toggle message visibilty based on current visibility
                // (visible == true becomes visible is false and
                // visible = false becomes visible is true)
                myMessage.visible = !myMessage.visible;
            }
           
            /**
             * Triggered when moveIn effect finishes
             *
             */

            private function moveInEnd(event:EffectEvent):void
            {
                // Create new timer with showingTime duration (2000ms / 2sec)
                timer = new Timer(showingTime);
                // Add event listener to the TimerEvent.TIMER event
                timer.addEventListener(TimerEvent.TIMER, handleTimerTick);
                // Start the timer
                timer.start();
            }
           
            /**
             * Triggered when moveOut effect finished
             *
             */

            private function moveOutEnd(event:EffectEvent):void{
                // Create new timer with notShowingTime duration (10ms)
                timer = new Timer(notShowingTime);
                // Add event listener to the TimerEvent.TIMER event
                timer.addEventListener(TimerEvent.TIMER, handleTimerTick);
                // Start the timer
                timer.start();
            }
           
        ]]>
    </fx:Script>
   
<s:BorderContainer cornerRadius="5" width="450" height="30">
   
    <s:Label x="90" id="myMessage" text="Hello from www.Flex-Blog.com! I'm Clickable!"
             fontWeight="bold" width="100%" height="100%" verticalAlign="middle"
             buttonMode="true" useHandCursor="true" visible="false" maxDisplayedLines="1"
             click="navigateToURL(new URLRequest('http://www.flex-blog.com'), '_blank')"
             showEffect="{moveIn}" hideEffect="{moveOut}"/>
</s:BorderContainer>
   
</s:Application>

Related posts:

  1. Flex 4 Resize Effect Example
  2. Flex Timer Example
  3. Progressbar in Datagrid Example

Written by Arjan · Filed Under Examples 

Was this post useful to you?

Please rate this post, follow us @ twitter, or link to this page from your website!

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 5.00 out of 5)
Loading ... Loading ...

373a03b24804d3593e630be49f57fa1edelicious

Comments

One Response to “Flex 4 Effect Example: Sliding text using the Move Effect”

  1. Roelof on April 28th, 2010 3:20 pm

    Nice example Arjan :-) !

Get Adobe Flash player

  • +1?

  • Support Flex Blog!

  • $ 13 raised
    • 2012/01/13 8:22 PM Russell Brown donated $ 3.00
    • 2011/10/31 4:43 PM Steve Dakin donated $ 5.00
    • 2011/05/11 3:37 PM Roelof Albers donated $ 5.00
  • Stay in touch!

  • Popular Tags

    • AdvancedDataGrid
    • AIR
    • ArrayCollection
    • baseColor
    • Button
    • CursorManager
    • DataGrid
    • Dynamic
    • Effects
    • File
    • FileStream
    • Flash Builder
    • Flash Builder 4
    • Flex 4
    • Flex Mobile
    • Framework
    • Icon
    • Image
    • itemRenderer
    • LinkBar
    • Mobile
    • PHP
    • ProgressBar
    • Repeater
    • Style
    • SWIZ
    • Timer
    • Tree
    • Twitter
    • ViewStack
  • Advertisements

  • Recent Posts

    • Spooky Frenzy – iPad Game
    • Fountain Example
    • Reading & Writing files in Adobe AIR
    • CheckBox in List using MobileIconItemRenderer for Flex Mobile
    • Data Dependent decoratorClass in MobileIconItemRenderer Example
    • Flex 4 Resize Effect Example
    • Jump to next field using the Focus Manager
    • Searching Data using a Class Example
    • Flex Mobile: Two finger tap gesture to toggle actionBar visibility in a View (AIR for Android)
    • TabbedMobileApplication Example in Flex Mobile (AIR for Android)
  • Categories

    • Examples
    • Guest Poster
    • iOS
  • Archives

    • September 2011
    • July 2011
    • May 2011
    • March 2011
    • February 2011
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • March 2009
    • February 2009
  • Blogroll

    • Adobe Flex Jobs
    • NL for Business
  • Meta

    • Register
    • Log in
    • WordPress
    • XHTML

Copyright © 2010 Flex Blog · AdobeĀ® and AdobeĀ® Flex are registered trademarks of Adobe Systems.

WordPress Adobe Flex Adobe Flash Builder Adobe AIR Creative Commons License

  • Popular Posts

    • Progressbar in Datagrid Example 13 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Data Dependant Tree Icon with Tree in AdvancedDataGrid with iconFunction 8 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 5 (5.00 out of 5)
    • List Directory with AIR in Flex 4 7 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Flex FlashVars in AS3 Example 7 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Flex Dynamic Chart Example 4 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 5 (5.00 out of 5)