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 / How to make a magnetic button in Flex 4

How to make a magnetic button in Flex 4

30 June 2010

Line Break

Author: Janez Feldin (6 Articles) - Author Website

Janez likes to experiment with flash in his own free time. His other hobbies are playing volleyball, listening to the music, watching movies and above all else, paragliding.


I have made a simple example on what most people refer to as magnetic (button) on the web. By extending the standard button you are able to do such a thing with Flex 4.

Finished result:

For this I have made a new class which extands spark button (you should extand the component you want to applay “magnetic” effect to it) and it looks like this:

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
102
103
104
105
106
107
package com.flexblog.components
{
    import flash.events.MouseEvent;
    import flash.ui.Mouse;
   
    import mx.core.FlexGlobals;
    import mx.events.FlexEvent;
    import mx.events.MoveEvent;
   
    import spark.components.Button;
    import spark.effects.Move;
    import spark.effects.easing.Elastic;
   
    public class MagneticButton extends Button
    {
        // xAncor and yAncor determine the point to which
        // the button returns when mouse cursor is too far away
        [Bindable] public var xAncor:Number = 0;
        [Bindable] public var yAncor:Number = 0;
       
        // Distance on x axis which determine maximum
        // distance from xAncor in one direction
        [Bindable] public var xDist:Number = 100;
       
        // Distance on y axis which determine maximum
        // distance from yAncor in one direction
        [Bindable] public var yDist:Number = 50;
       
        // Property that makes transitions between different positions
        private var _move:Move = new Move(this);
       

        public function MagneticButton()
        {
            super();
           
            /**
             * Tries to add event listener for mouse move event to stage.
             * If stage is null than adds event listener for application
             * complete and than adds event listener for mouse move
             */

            try
            {
                _move.easer = new Elastic();
                _move.duration = 1500;
               
                xAncor = this.x;
                yAncor = this.y;
                FlexGlobals.topLevelApplication.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouse_move);
            }
            catch(e:Error)
            {
                FlexGlobals.topLevelApplication.addEventListener(FlexEvent.APPLICATION_COMPLETE, app_complete);
            }
        }
       
        /**
         * Function that is called when Main application is complete.
         * It sets easer property and duration property for _move, set ancor
         * of the button to the buttens current x and y position and as
         * mentioned above adds an event listener for mouse move
         */

        private function app_complete(e:FlexEvent):void
        {
            _move.easer = new Elastic();
            _move.duration = 1500;
           
            xAncor = this.x;
            yAncor = this.y;
           
            stage.addEventListener(MouseEvent.MOUSE_MOVE, mouse_move);
        }
       
        /**
         * Sets the ancor of the button and also move it to that position.
         */

        public function setAncor(_x:Number,_y:Number):void
        {
            xAncor = _x;
            yAncor = _y;
            _move.xTo = _x;
            _move.yTo = _y;
            _move.play();
        }
       
        /**
         *  Function that actually calculates and moves the button to the right position.
         */

        private function mouse_move(e:MouseEvent):void
        {
            // Some basic math logic to calculate if mouse cursor
            // is close enough to move the button to that location
            if ( Math.abs((xAncor + this.width/2) - stage.mouseX) < 2*xDist &&
                 Math.abs((yAncor + this.height/2) - stage.mouseY) < 2*yDist )
            {
                _move.xTo = stage.mouseX - this.width/2;
                _move.yTo = stage.mouseY - this.height/2;
            }
            else
            {
                _move.xTo = xAncor;
                _move.yTo = yAncor;
            }
            _move.play();
        }
    }
}

With this we have acctualy created new component and to use it we need to add that component to stage in our mxml component just like we would any other component.

Our main.mxml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               xmlns:components="com.flexblog.components.*"
               backgroundColor="#B9B9B9" width="500" height="300"
               viewSourceURL="srcview/index.html">

    <components:MagneticButton label="I am magnetic button"
                               x="100" y="50" xDist="50" yDist="30"/>
    <components:MagneticButton label="I am also magnetic button"
                               x="300" y="50" xDist="50" yDist="30" />
    <s:Button label="I am normal button" x="150" y="150"/>
   
</s:Application>

Related posts:

  1. Image as Button in a DataGrid
  2. Using baseColor to Style Components in Flex 4
  3. SWIZ Framework in Flex 4 Example: Part I
  4. SWIZ Framework in Flex 4 Example: Part II

Written by Janez Feldin · 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 (10 votes, average: 4.40 out of 5)
Loading ... Loading ...

abcd0ea0640357479eefd946a071ca6ddelicious

Comments

6 Responses to “How to make a magnetic button in Flex 4”

  1. Joseph on March 4th, 2011 10:21 pm

    Very Cool… I was wondering if it would be possible to move the Cursor of the mouse to the location of the button if it comes into proximity?

  2. Joseph on March 4th, 2011 10:22 pm

    Instead of the button moving towards the Mouse

  3. Janez Feldin on March 6th, 2011 9:57 pm

    You can’t actually move the mouse cursor in flash. If you really need this you could create your own cursor and replace the original with yours. And than this could be done by instantly checking where your mouse is and if you clicked near button you would have to make sure you execute the same function as you would by clicking the button.

    But even if I really needed this I wouldn’t use it because it is a bad idea, it would require a lot of work to be done just for this… I would encourage you to find another way to complete your goal…

  4. matias on October 7th, 2011 6:40 am

    Awesome!!!!

  5. Fox on October 29th, 2011 5:15 pm

    How about a magnetic scrolling Spark List which snaps to its rows once the user stopps scrolling ? Somehow like a “Wheel of Fortune”?

  6. Janez Feldin on October 30th, 2011 8:14 pm

    For this you would first need to have an animated spark list. Without animations it would not look good. To achive this you would have to change the spart component a lot. I will look at the list component and maybe put together an example of how to do it (depends on the difficulty).

    I have a feeling that it would be easier to make your own component if you don’t need too much functionality of the spark list.

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)