Archive for August, 2008

swapDepth in AS3 for drag n drop

The swapDepth is not available on AS3, rather much more simplest way is available. 🙂

The below commented code can be achieved through the single line:

event.target.parent.addChild(event.target);

private var _dragIcon:MovieClip;
dragIcon.addEventListener(MouseEvent.MOUSE_UP,onMouseUp, false, 0, true);
dragIcon.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown, false, 0, true);

/*
private var oldSwapDepth:int = 0;
private var tempSwapDepth:int = 0;
private function onMouseDown(event:MouseEvent):void
{
dragIcon.startDrag(false)
tempSwapDepth = dragIcon.parent.getChildIndex(dragIcon);
var newSwapDepth:int = dragIcon.parent.getChildIndex(swapDepthMc);
newSwapDepth>oldSwapDepth ? newSwapDepth = newSwapDepth : newSwapDepth =  oldSwapDepth;
dragIcon.parent.setChildIndex(dragIcon,newSwapDepth)
oldSwapDepth = dragIcon.parent.getChildIndex(dragIcon);
}
private function onMouseUp(event:MouseEvent):void         {
dragIcon.stopDrag();
dragIcon.parent.setChildIndex(dragIcon,tempSwapDepth);
} */
private function onMouseDown(event:MouseEvent):void
{
event.target.parent.addChild(event.target);
}

Advertisements

Steps to be followed before Unloading any SWF

1. Tell any loaded .swf child assets to disable themselves.
2. Stop any sounds from playing.
3. Stop the main timeline, if it is currently playing.
4. Stop any movie clips that are currently playing.
5. Close any connected network objects, such as instances of Loader, URLLoader, Socket, XMLSocket, LocalConnection, 6. NetConnections, and NetStream.
7. Release all references to cameras and microphones.
8. Remove all event listeners
9. Stop any currently running intervals (via clearInterval()).
10. Stop any Timer objects (via the Timer class’s instance method stop()).
11. Dispose all subobjects
12. Release all references to external objects
In future Flash Player 10, does these by calling the Loader class’s new method unloadAndStop().

States MXML tag converted into AS3 Code

<mx:states>
<mx:State name=”minimized”>
<mx:SetProperty target=”{this}” property=”height” value=”{this.minHeight+15)}”/>
<mx:SetProperty target=”{this}” property=”width” value=”{this.minWidth}”/>
<mx:SetProperty target=”{this}” property=”vScrollPolicy” value=”off”/>
<mx:SetProperty target=”{this}” property=”hScrollPolicy” value=”off”/>
</mx:State>
</mx:states>

private function buildStates():void{
var overrides:Array = new Array();
var newState:State = new State();
overrides.push(makeSetProp(this,”height”,this.minHeight +5));
overrides.push(makeSetProp(this,”width”,this.minWidth));
overrides.push(makeSetProp(this,”vScrollPolicy”,”off”));
overrides.push(makeSetProp(this,”hScrollPolicy”,”off”));
newState.name=”minimized”;
newState.overrides = overrides;
this.states = new Array(newState);
}
private function makeSetProp(target:UIComponent, property:String, value:*):SetProperty{
var sp:SetProperty = new SetProperty();
sp.target = target;
sp.property = property;
sp.value = value;
return sp;
}

AS3 remedy to overcome removed / replaced AS2 terms

on()/onClipEvent():
AS2:

on (release) {
this._parent.gotoAndPlay(1);
}

AS3:

replayBtn.addEventListener(MouseEvent.CLICK, replayBtnClickListener);
function replayBtnClickListener (e) {
gotoAndPlay(1);
}

or

replayBtn.addEventListener(MouseEvent.CLICK, function (e) {
gotoAndPlay(1);
});

Delegate Class:
The Delegate Class of AS2, is more simpler to write in AS3 as like the example below:
AS2:

myButton.addEventListener(“click”, Delegate.create(this, someMethod));
Delegate.create(this, someMethod)

AS3:

myButton.addEventListener(“click”, someMethod);

loadMovie

AS2:

theClip.loadMovie(“animation.swf”);

AS3:

var l:Loader = new Loader();
l.load(new URLRequest(“animation.swf”));
theParent.addChild(l);

Controlling Parent Movie Clips
AS2:

this._parent.play();

AS3:

MovieClip(this.parent).play();

or

this.parent[“play”]();

Removal of getURL()
AS2:

getURL(“https://nsdevaraj.wordpress.com/&#8221;);

AS3:

navigateToURL(new URLRequest(“https://nsdevaraj.wordpress.com/));

AS3 class to use getURL:

package {
import flash.net.*;

public function getURL (url:String,
window:String = “_self”):void {
var u:URLRequest = new URLRequest(url);
navigateToURL(u, window);
}
}

Usage:

getURL(“https://nsdevaraj.wordpress.com/&#8221;);

or

getURL(“https://nsdevaraj.wordpress.com/&#8221;, “_blank”);

createTextField:
AS2

someClip.createTextField(“t”,0, 0, 0, 100, 100);
someClip.t.text = “Hello world”;

AS3

var t:TextField = new TextField();
t.text = “Hello world”;

eval Objects:
AS2:

for (var i = 0; i < 10; i++) {
parentClip.attachMovie(“Animation” + i, “instance” + i, i);
}

AS3:

var Symbol:Object;
for (var i:int = 0; i < 10; i++) {
Symbol = getDefinitionByName(“Animation” + i);
parentClip.addChild(new Symbol());
}

attachMovie:
AS2:

parentClip.attachMovie(“Animation”, “instance” , 1);

AS3:

package {
import flash.display.DisplayObjectContainer;
import flash.display.DisplayObject;
import flash.utils.getDefinitionByName;

public function addChildFromLibrary (parent: DisplayObjectContainer,
symbolName:String,
depth:int = -1): DisplayObject {
var Symbol = getDefinitionByName(symbolName);

if (depth < 0) {
return parent.addChild(new Symbol());
} else {
return parent.addChildAt(new Symbol(), depth);
}
}
}

Usage:

addChildFromLibrary(parentClip, “Animation”);

duplicateMovieClip()
AS2:

duplicateMovieClip (circle, “circle1”,  1);

AS3:

package {

import flash.display.DisplayObject;
import flash.geom.Rectangle;
import flash.system.Capabilities; // version check for scale9Grid bug

public function duplicateDisplayObject(target: DisplayObject, autoAdd:Boolean = false): DisplayObject {
var targetClass:Class = Object(target).constructor;
var duplicate: DisplayObject = new targetClass() as DisplayObject;

// duplicate properties
duplicate.transform = target.transform;
duplicate.filters = target.filters;
duplicate.cacheAsBitmap = target.cacheAsBitmap;
duplicate.opaqueBackground = target.opaqueBackground;
if (target.scale9Grid) {
var rect:Rectangle = target.scale9Grid;

if (Capabilities.version.split(” “)[1] == “9,0,16,0”){
// Flash 9 bug where returned scale9Grid as twips
rect.x /= 20, rect.y /= 20, rect.width /= 20, rect.height /= 20;
}

duplicate.scale9Grid = rect;
}

// add to target parent’s display list
// if autoAdd was provided as true
if (autoAdd && target.parent) {
target.parent.addChild(duplicate);
}
return duplicate;
}
}

Usage:

var circle1:MovieClip =  duplicateDisplayObject(circle, true);

Steps to be followed for AS3 migration

1. Declare types for all variables, parameters, and return values.
2. Declarations with no access specifier now default to package internal, not public.
3. Classes are sealed by default, meaning properties cannot be added dynamically at runtime.
4. Use package declarations to put a class definition into a package.
5. Import classes, even if references to the class are fully qualified.
6. Always mark method overrides. Declare return types in your functions.
7. Delegates are now built into the language, making event dispatching easier.
8. De referencing a null or undefined reference will throw an exception.
9. For debugging use the -verbose-stacktraces and -debug options.
10. Explicitly declare properties to be bindable.
11. Flash Player API has been reorganized into packages.
12. Use the new Timer class instead of setInterval/setTimeout.
13. Be sure to subclass events.
14. Visual elements must extend DisplayObject, and define them like any other class.
15. E4X (ECMAScript for XML) is used for manipulating XML.use toXMLString() method when using E4X.
16. for…in loop will not enumerate properties and methods declared by a class.

Getting Started With Flex 3

Order a free copy of book Getting Started With Flex 3 -from Adobe