Archive

Archive for January, 2010

Changing MovieClip’s registration point using AS3

January 28, 2010 Leave a comment

Hey all,

here is the simple class that helps you to create a custom MovieClip and you can change its registration point at anytime,

package com.karthik.web.display {
 import flash.display.MovieClip;
 import flash.events.Event;
 import flash.geom.Point;    

 public class KMovieClip extends MovieClip {

 protected var _registrationX:Number;        // Registration point X
 protected var _registrationY:Number;        // Registration point Y
 protected var _x:Number;            // User-defined X
 protected var _y:Number;            // User-defined Y

 protected var setToUpdate:Boolean;        // Whether this instance is already set to update on the next Event.RENDER

 // CONSTRUCTOR 

 public function KMovieClip() {
 super();

 // Reads the current __values to keep them
 _x = super.x;
 _y = super.y;
 _registrationX = 0;
 _registrationY = 0;
 fixPosition();
 }

 // INTERNAL functions 

 protected function fixPosition(): void {
 // Using localToGlobal/globalToLocal is less precise than doing it mathematically, but the end result is more accurate inside Flash because it's in sync with Flash's positioning and rotating limitations
 var op:Point = new Point(0, 0);
 var rp:Point = new Point(_registrationX, _registrationY);
 rp = parent.globalToLocal(localToGlobal(rp));
 op = parent.globalToLocal(localToGlobal(op));
 super.x = _x - (rp.x - op.x);
 super.y = _y - (rp.y - op.y);
 }

 protected function requestPositionFix(): void {
 if (Boolean(stage) && !setToUpdate) {
 setToUpdate = true;
 stage.addEventListener(Event.RENDER, onRender, false, 0, true);
 stage.invalidate();
 }
 }

 // EVENT functions ------------------------------------------------------------------------------------------------

 protected function onRender(e:Event): void {
 stage.removeEventListener(Event.RENDER, onRender);
 setToUpdate = false;
 fixPosition();
 }

 // ACCESSOR functions ---------------------------------------------------------------------------------------------

 override public function get x(): Number {
 return _x;
 }
 override public function set x(__value:Number): void {
 _x = __value;
 requestPositionFix();
 }

 override public function get y(): Number {
 return _y;
 }
 override public function set y(__value:Number): void {
 _y = __value;
 requestPositionFix();
 }

 override public function set rotation(__value:Number): void {
 super.rotation = __value;
 requestPositionFix();
 }
 override public function set scaleX(__value:Number): void {
 super.scaleX = __value;
 requestPositionFix();
 }
 override public function set scaleY(__value:Number): void {
 super.scaleY = __value;
 requestPositionFix();
 }

 public function get registrationX(): Number {
 return _registrationX;
 }
 public function set registrationX(__value:Number): void {
 _registrationX = __value;
 requestPositionFix();
 }

 public function get registrationY(): Number {
 return _registrationY;
 }
 public function set registrationY(__value:Number): void {
 _registrationY = __value;
 requestPositionFix();
 }

 }
}

After creating an instance of this MovieClip class, you can change the registration point as below,

mc.registrationX = 10;
mc.registrationY = 10;
Categories: AS3, Flash

FLEX/AIR Dispatch Custom Events from Popup or between mxml components

January 19, 2010 Leave a comment

Here is the way how we can achieve the thing dispatching custom events between components.

PopUp window:

<mx:script>
dispatchEvent( new Event(MyEvent.MY_DATA_CHANGED, true));
</mx:script>

Other mxml component:

<mx:script>
     private function onClick():void {
       var win:MyForm = PopUpManager.createPopUp(this, MyForm, true) as MyForm;
       win.addEventListener(MyEvent.MY_DATA_CHANGED, onMyDataChanged);
     }
     private function onMyDataChanged(event:Event):void {
       trace("DATA CHANGED");
     }
</mx:script>

MyEvent:

public static const MY_DATA_CHANGED:String = "myDataChanged";

I hope this will help.

Categories: AS3, Flex

Flex/AS3 Convert string to uppercase on each word

January 19, 2010 Leave a comment

Here is the way, we can do convert a string to uppercase on each word starting letters as like in the PHP (ucword).

str.replace(/\b./g,function(...m):String{return m[0].toUpperCase()});
Categories: AS3, Flex
Follow

Get every new post delivered to your Inbox.