Posts Tagged ‘ as2 ’

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

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/”);

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/”);

or

getURL(“https://nsdevaraj.wordpress.com/”, “_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.

using root keyword in Flex (AS3 migration)

var root_mc:Object = parent.parent.parent.parent.parent.parent.parent.parent
root_mc.functionNAME();

 use the parent as per the level of components used, then u can refer the root_mc to control the objects or to call the functions of the root level elements.