Posts Tagged ‘ eval ’

Eval mathematical Expression using AS3

The below code is example for how to use MathParser.as used for evaluating maths expressions using flex. Inspired from flashmaths

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; backgroundColor=”#333333″ creationComplete=”calculate()”>
<mx:Script>
<![CDATA[
[Bindable]
private var evalStr:String;
private var mpExp:MathParser = new MathParser([ ]);
private function calculate ():void {
var compobjVal:Object = mpExp.doCompile(mathStr.text);
evalStr = String(mpExp.doEval(compobjVal.PolishArray, []));
}
]]>
</mx:Script>
<mx:TextInput id=”mathStr” text=”((7*(1-8))*9)-6+7″ change=”calculate()” />
<mx:Text id=”resVal” color=”White” text=”{evalStr}” />
</mx:Application>

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/&#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);

eval in AS3

To do this, you need to include the keyword “dynamic” as part of your class declaration, e.g., public dynamic class Foo. Then to make the call, just say:

var functionName:String = “foo” + bar;
if (this.hasOwnProperty(functionName))
this[functionName]();

reference