Monday 20 February 2012

as3isolib, regular expressions

Today's roundup of experiments and snippets:

as3isolib

I downloaded as3isolib some time ago but never had time to play around with it. Today I went back to their site after a long while and realised that all their example swfs on their documentation site were broken. Most of the examples were originally for Flex but I sometimes like to work in Flash IDE itself so this is an example how to use it with as3iso. Credit goes to the documentation wiki as well as various commenters correcting each other on the wiki.

"As3isolib is an open-source ActionScript 3.0 Isometric Library developed to assist in creating isometrically projected content (such as games and graphics) targeted for the Flash player platform."

This is a simple example of an IsoScene with an IsoBox. It uses classFactory to create the shadow below itself, and TweenMax to handle the animation.



import as3isolib.core.ClassFactory;
import as3isolib.core.IFactory;
import as3isolib.core.IIsoDisplayObject;
import as3isolib.display.IsoView;
import as3isolib.display.primitive.IsoBox;
import as3isolib.display.renderers.DefaultShadowRenderer;
import as3isolib.display.scene.IsoGrid;
import as3isolib.display.scene.IsoScene;
import as3isolib.geom.IsoMath;
import as3isolib.geom.Pt;

import eDpLib.events.ProxyEvent;
import com.greensock.TweenMax;

var box:IIsoDisplayObject;
var scene:IsoScene;
var tm:TweenMax;

scene = new IsoScene();

var g:IsoGrid = new IsoGrid();
g.showOrigin = false;
g.addEventListener(MouseEvent.CLICK, grid_mouseHandler);
scene.addChild(g);

box = new IsoBox();
box.setSize(25, 25, 25);
box.moveBy(0, 0, 20);

scene.addChild(box);

var factory:as3isolib.core.ClassFactory = new as3isolib.core.ClassFactory(DefaultShadowRenderer);
factory.properties = {shadowColor:0x000000,shadowAlpha:0.15,drawAll:false};
scene.styleRenderers = [factory];

var view:IsoView = new IsoView();
view.clipContent = true;
view.setSize(stage.stageWidth, stage.stageHeight);
view.addScene(scene);
addChild(view);

scene.render();

function grid_mouseHandler(evt:ProxyEvent):void
{
 var mEvt:MouseEvent = MouseEvent(evt.targetEvent);
 var pt:Pt = new Pt(mEvt.localX,mEvt.localY);
 IsoMath.screenToIso(pt);
 var squareSize:int = 50;// 
 var gridX:int = Math.floor(pt.x / squareSize);
 var gridY:int = Math.floor(pt.y / squareSize);
 TweenMax.to(box, 0.5, {x:gridX * squareSize, y:gridY * squareSize, onComplete:completeCallback});

 if (! hasEventListener(Event.ENTER_FRAME))
 {
  this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
 }
}

function completeCallback():void
{
 this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
}

function enterFrameHandler(evt:Event):void
{
 scene.render();
}

Using regular expressions to parse loaderInfo url name:

I have a folder full of swfs which have an external file / xml entry where they must get their information from. Each swf file is named in the format xxx_0.swf where xxx is a 3 letter suffix and 0 is any number (can be any number of digits). The issue is that when you try to return the url name you will actually get the full directory path. Don't know if this is the most efficient way, but the solution I eventually went with involves putting each URL segment (divided by a slash) into an seperate array item and then going to the last one. Finally we slice off the front 4 characters (xxx_) and last 4 characters off (.swf):

var myFileNameArray:Array = new Array();
var num:Number;
var myFileNumber:String;
var myFileNum:Number;

var myFileName:String = this.loaderInfo.url.split("/").pop().replace(/%5F/g, "_").replace(/%2D/gi,"-");
myFileName = this.loaderInfo.url.split("/").pop().replace(/%5F/g, "_").replace(/%2D/gi,"-");
myFileNameArray = myFileName.split("/");
num = myFileNameArray.length;
myFileNumber = myFileNameArray[num-1].slice(4,-4);
myFileNum = Number(myFileNumber);

No comments:

Post a Comment