Create a Custom Animated Cursor for your Flex app

In this Flex tutorial, let’s see how to replace the default arrow cursor with an animated cursor created in Flash using the CursorManager class.

View DemoDownload Source

1. Create a new flex project named AnimatedCursor, set the name of the main MXML application file to Main.mxml.

2. Our custom cursor is a swf file, so open Flash and create your own custom cursor. Set the framerate to 24 to match with the Flex framerate. Here we simply draw an arrow by hand and create a tween to change its color which is quite boring but we leave it to you to make a more creative one.
Publish your swf file.

3. Back into Flex, add a Checkbox component that for its change event call the toggleCursor function that we’are going to create.

<mx:CheckBox label="Custom Cursor" change="toggleCursor(event)"/>

4. Add a Script section. First, embed your swf file that will be used as the custom cursor.

[Embed(source="cursor.swf")]
private var AnimatedCursor:Class;

5. In the toggleCursor() function, we check whether the checkbox is selected.
If true, call the setCursor() method of the CursorManager class and pass it a reference to the class of your embedded swf cursor.
The setCursor() method returns an ID for the cursor being set that you need to store as we’ll need to pass the ID to the removeCursor() method.

 import mx.managers.CursorManager;

 private var cursorID:int;
                    
 private function toggleCursor(e:Event):void{
          if (e.currentTarget.selected)
            	cursorID = CursorManager.setCursor(AnimatedCursor);
          else CursorManager.removeCursor(cursorID);
 }

6. Here’s the final code, run the application to test it.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
	verticalAlign="middle" backgroundColor="#FFFFFF">

    <mx:Script>
        <![CDATA[
            import mx.managers.CursorManager;

            [Embed(source="cursor.swf")]
            private var AnimatedCursor:Class;

            private var cursorID:int;
                    
            private function toggleCursor(e:Event):void{
            	if (e.currentTarget.selected)
            		 cursorID = CursorManager.setCursor(AnimatedCursor);
            	else CursorManager.removeCursor(cursorID);
            }
        ]]>
    </mx:Script>
    
	<mx:CheckBox id="checkBox" label="Custom Cursor" change="toggleCursor(event)"/>
	
</mx:Application>

3 thoughts on “Create a Custom Animated Cursor for your Flex app

  1. Kubarium

    How about the attacted swf has stop in the first frame and once we click on an object with the custom cursor, it triggers the animation?

Leave a Reply

Your email address will not be published. Required fields are marked *