Abdulcode

Pages

Showing posts with label AS3. Show all posts
Showing posts with label AS3. Show all posts

Aug 18, 2010

AS3 : with keyword

It can be used to write cleaner code when setting multiple properties of the same object.

Normal Code:
var circle:Sprite = new Sprite();
circle.graphics.beginFill(0xFFCC00);
circle.graphics.drawCircle(100,100,50);
circle.graphics.endFill();
addChild(circle);


Using with keyword Code:
var circle:Sprite = new Sprite();
with (circle.graphics)
{
beginFill(0xFFCC00);
drawCircle(100, 100, 50);
endFill();
}
addChild(circle);

Jul 26, 2010

Function parameters

Pass by Value
function passPrimitives(xParam:int, yParam:int):void
{
xParam++;
yParam++;
trace(xParam, yParam);
}

var xValue:int = 10;
var yValue:int = 15;

trace(xValue, yValue); // 10 15
passPrimitives(xValue, yValue); // 11 16
trace(xValue, yValue); // 10 15

Pass by Reference
function passByRef(objParam:Object):void
{
objParam.x++;
objParam.y++;
trace(objParam.x, objParam.y);
}

var objVar:Object = {x:10, y:15};

trace(objVar.x, objVar.y); // 10 15
passByRef(objVar); // 11 16
trace(objVar.x, objVar.y); // 11 16
The objParam parameter references the same object as the global objVar variable. As you can see from the trace statements in the example, changes to the x and y properties of the objParam object are reflected in the objVar object.

Jul 23, 2010

AS3 : Dynamic classes

A dynamic class defines an object that can be altered at run time by adding or changing properties and methods. A class that is not dynamic, such as the String class, is a sealed class. You cannot add properties or methods to a sealed class at run time.

You create dynamic classes by using the dynamic attribute when you declare a class. For example, the following code creates a dynamic class named "DynamicClass".

Code : /*Main.as*/
package
{
import flash.display.Sprite;

public class Main extends Sprite
{
public function DynamicClassCode()
{
var dClass:DynamicClass = new DynamicClass();

dClass.age = 25; // Dynamically add new variable.
// Dynamically add new Method.
dClass.getDetails = function():void
{
trace(dClass.nama);
trace(dClass.age);
}

dClass.getDetails(); // Method call
}
}
}

dynamic class DynamicClass
{
public var nama:String = "Abdul";
}

Jul 21, 2010

Number to String Conversion

Syntax:
toString(radix);

radix Specifies the numeric base (from 2 to 36) to use for the number-to-string conversion.
Sample:
package
{
import flash.display.Sprite;

public class Sample_AS extends Sprite
{
public function Sample_AS()
{
var num:int = 241;

trace(num.toString(2)); // Binary
trace(num.toString(8)); // Octal
trace(num.toString(16)); // Hexadecimal
}
}
}

Jul 12, 2010

AS3 Snippet: FlexEvent.IDLE

A FlexEvent.IDLE event will dispatch every 100 milliseconds when there has been no keyboard or mouse activity for 1 second.

Code :
this.systemManager.addEventListener(FlexEvent.IDLE, userIdle);
private function userIdle(e:FlexEvent):void {
if(e.currentTarget.mx_internal::idleCounter == 3000){
//do something!
}
}
If the event trigger means, the control pass to the method.
In this method, we check the condition for that the system is idle for 5mins.
(Five minutes is equal to 300000 milliseconds… divided by 100 ticks and
the number we need to check against is 3000).