Archive

Archive for the ‘Flash’ Category

Time conversion, Seconds to minutes, hours

February 25, 2010 Leave a comment

Here is the function that will help you to convert the given seconds to time format.

public static function formatTime ( time:Number ):String
 {
 var remainder:Number;            
 var hours:Number = time / ( 60 * 60 );            
 remainder = hours - (Math.floor ( hours ));            
 hours = Math.floor ( hours );            
 var minutes:Number = remainder * 60;            
 remainder = minutes - (Math.floor ( minutes ));            
 minutes = Math.floor ( minutes );            
 var seconds:Number = remainder * 60;            
 remainder = seconds - (Math.floor ( seconds ));            
 seconds = Math.floor ( seconds );            
 var hString:String = hours < 10 ? "0" + hours : "" + hours;    
 var mString:String = minutes < 10 ? "0" + minutes : "" + minutes;
 var sString:String = seconds < 10 ? "0" + seconds : "" + seconds;                        
 if ( time < 0 || isNaN(time)) return "00:00";                        
 if ( hours > 0 )
 {            
 return hString + ":" + mString + ":" + sString;
 }else
 {
 return mString + ":" + sString;
 }
 }
Categories: AS3, Flash, Flex

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

Compare two xml

December 5, 2009 Leave a comment

package{
import flash.display.Sprite;

public class Main extends Sprite{
public function Main(){
var msg1:XML = <GREETING>
<TO>A</TO>
<FROM>Programmer</FROM>
<MESSAGE>Hello</MESSAGE>
</GREETING>;

var msg2:XML = <GREETING>
<TO>A</TO>
<FROM>Programmer</FROM>
<MESSAGE>Hello</MESSAGE>
</GREETING>;

trace(msg1.* == msg2.*);  // Displays: true
}
}
}

Categories: AS3, Flash

Comparing Two Objects in Actionscript

December 5, 2009 Leave a comment
Use this function to compare two objects that contains large number of properties.

public function compareObject(obj1:Object,obj2:Object):Boolean
{
    var buffer1:ByteArray = new ByteArray();
    buffer1.writeObject(obj1);
    var buffer2:ByteArray = new ByteArray();
    buffer2.writeObject(obj2);
 
    // compare the lengths
    var size:uint = buffer1.length;
    if (buffer1.length == buffer2.length) {
        buffer1.position = 0;
        buffer2.position = 0;
 
        // then the bits
        while (buffer1.position < size) {
            var v1:int = buffer1.readByte();
            if (v1 != buffer2.readByte()) {
                return false;
            }
        }    
        return true;                        
    }
    return false;
}
Categories: AS3, Flash
Follow

Get every new post delivered to your Inbox.