Peter Elst

Flash Platform Consultant
  • Home
  • About me
  • Articles
  • Downloads
  • Contact me

MXML custom effects

18 10 2005

I had an interesting comment on one of my blog posts asking about creating your own custom effects in MXML. I knew it was possible but wasn’t really up to speed on the best way to do it. Well, after a bit of research here is an example — its a very basic rotation effect that rotates an object by a given number of degrees over time.

First thing I did was create a new AS2 class called Rotate that subclasses mx.effects.TweenEffect and save that file as Rotate.as (obviously).

class Rotate extends mx.effects.TweenEffect {

   var rotateBy:Number;
   var rotateTween:mx.effects.Tween;

   function Rotate(targetObj:Object)   {
      target = targetObj;
   }

   function playEffect():Void   {
	rotateTween = new mx.effects.Tween(this, target._rotation,
        target._rotation+rotateBy, duration);
   }

   function onTweenUpdate(tweenValue):Void   {
      target._rotation = tweenValue;
   }

}

In the constructor we create a reference to the target object, the playEffect method is called when the effect is triggered and just created a new mx.effects.Tween instance from the target objects original rotation to its rotation plus the rotateBy value (which is passed through the MXML tag) over the time that is defined in the duration attribute value. The onTweenUpdate method is called by the Tween instance and passes the interpolated tween value to update the target object rotation.

That’s it for the AS2 class! Now on to the MXML - pretty much the same as before, with one twist I wasn’t aware of. In order for MXML to find your Rotate class you need to define an additional namespace in the Application tag.




  
    
  

  


In this MXML file I defined an Effect block as before and have a tag called Rotate (note that it doesn’t have the mx prefix) with the attributes we use in the Rotate AS2 class (rotateBy and duration) and a name of “myRotateEffect”. Further down in the document we once again have an Image tag that embeds an external SWF and a mouseDownEffect trigger that is hooked up to “myRotateEffect”.

When you run this file and click on the embedded SWF you’ll see it rotate by 360 degrees over 1 second. Really like this way of working with effects will definitely experiment with it further! :D

If you’re looking for something a little more challenging Macromedia has a more extensive example of a custom rotate effect here.

« Macromedia likes mondays … Open Source Flash conference »



Actions

Informations

  • Date : 18 October 2005
  • Categories : Flex

2 responses to “MXML custom effects”

18 10 2005
Andreas Weber (16:15:49) :

My first day with Flex, please bear with me…
Is it correct that with Flex Builder 2 Alpha only AS3 style classes can be edited,k?i.e. above ‘Rotate’ class cannot be compiled because it is not defined inside a package?

Cheers!

18 10 2005
Peter (16:43:42) :

yes true, this example can’t be compiled using the Flex Builder 2 alpha — I’ll see if I can get a custom effect up that will compile for Flash Player 8.5 ;)





Conferences

Flash on the Beach Speaker

Training Partners

multimediacollege

SkillsMatter

Badges

Alltop, confirmation that I kick ass

  • Categories

    • AIR RSS category feed
    • Ajax RSS category feed
    • CSS RSS category feed
    • Eclipse RSS category feed
    • Events RSS category feed
    • Flash RSS category feed
    • Flash Lite RSS category feed
    • Flex RSS category feed
    • Gadgets RSS category feed
    • General RSS category feed
    • JSFL RSS category feed
    • Mac RSS category feed
    • Open Source RSS category feed
    • PHP RSS category feed
    • Podcasts RSS category feed
    • Publications RSS category feed
    • Rants RSS category feed
    • Reviews RSS category feed
    • Thought of the Day RSS category feed
    • Training RSS category feed
    • Twitter RSS category feed
    • Video RSS category feed
  • Resources

    • Stefan Richter
    • Veronique Brossier
    • Peter Hall
    • Serge Jespers
    • AndrĂ© Michelle
    • Abdul Qabiz
    • Aral Balkan
    • Sas Jacobs
    • Marco Casario
    • LordAlex Leon
    • Edwin van Rijkom
    • Matthew David
    • Burak Kalayci
    • Guy Watson
    • Jesse Warden
    • Peldi Guilizzoni
    • Lee Brimelow
    • Scott Barnes
    • Ted Patrick
    • Patrick Mineault

     
     

    Adobe Community Expert

    See my profile on LinkedIn



    Harz Ferienwohnung Suchmaschinenoptimierung Geschenkideen Harz Ferienwohnung Pagerank Webkatalog Webhosting