as3 blog

thoughts on actionscript 3 development

   Mar 26

search and replace in strings

it’s been a while since i needed to do this. nice technique is to use ‘split’ to break the string into an array using the split’s arguments as the deliminator then ‘join’ to stick is back together using joins arg to put between each element. the effect is a search and replace

var str:String = "this is a string with some spaces that need replacing with _"
str = str.split(" ").join("_");
// simple :)
									


   Mar 12

Recursively delete .svn directories

if you’ve ever needed to delete all the hidden .svn directories from a project running a script like this in terminal is very helpful (will work in OSX and Linux)

cd to the root directory then run the following…

rm -rf `find . -type d -name .svn`

make sure to use the ` character. all .svn directories are now gone


   Nov 02

embeding json, xml and text files

you could load some json data at runtime and most of the time this is the best thing to do as it’s likely you’ll want to be able to change that data without recompiling. but what about some json data for a sprite sheet? it’s easy enough to embed images in actionscript but what about text files? it’s a little bit different..

package
{
  public class EmbedJsonTest extends Sprite
  {
    [Embed(source="data.json",mimeType="application/octet-stream")]
    private const JsonData : Class;

    public function EmbedJsonTest()
    {
      var ba : ByteArray = new JsonData() as ByteArray;

      trace("JsonData = " + ba.readUTFBytes(ba.length));
    }
  }
}
									


   Oct 24

file path of swf file

here’s something simple but useful.. getting the file path of your swf file at runtime

var filePath : String = this.loaderInfo.url;
var lastIndex : int = filePath.lastIndexOf("/");
trace(filePath.substr(0, lastIndex + 1));
									

but this will only work once the DisplayObject is added to the stage so you can’t put it in the constructor. you need to add an ADDED_TO_STAGE event listener then run it inside an init function.


   Oct 19

turn camera off

turning on the users web cam and displaying it is easy enough..

var camera : Camera = Camera.getCamera();
camera.setQuality(0, 90);
camera.setMode(720, 576, 25);

if (camera != null)
{
  video = new Video(720, 576);
  video.attachCamera(camera);
  addChild(video);
}
else
{
  trace("go buy a camera");
}
									

but if the user navigates to another part of your app and you don’t remove the camera object it will continue to run and use a whole lot of cpu. removing it’s pretty easy too but also easy to forget..

video.attachCamera(null);
video.clear();
removeChild(_video);

camera = null;
									

the key part there is video.attachCamera(null); that’s the part that will turn off the little light next to your notebook’s camera ;)


   Oct 14

Tracing Objects

simplest way to do this is to convert the object to a JSON string and trace that out..

var obj : Object = {uid:15, title:"hello"};
var jsonObj : String = JSON.encode(obj);
trace(jsonObj);
									

the JSON class here is part of a useful library of as3 utility class files from mike chambers available here..

https://github.com/mikechambers/as3corelib

another very useful one that’s part of this collection is a JPG encoder that takes a BitmapData object and returns a ByteArray that can then be sent to a server with AMF.


   Sep 22

CacheAsBitmapMatrix on Mobile Devices

cacheAsBitmap is a property that’s a good idea to set to true for display objects that will be moved around containing vector content. flashPlayer has to redraw vectors that are transformed and this hurts performance. by setting cacheAsBitmap to true you’re effectively converting the vector to a bitmap which flashPlayer can move round alot easier. this will work great if all you’re doing is moving it on the x and y but if you want to scale, rotate or change the alpha, cacheAsBitmap will make performace worse as it will have to redraw the vector, apply the transformation and cache it as a bitmap all over again. there is a nice extra available at the moment only for Air on mobile devices called cacheAsBitmapMatrix. this setting will allow scale, rotation and alpha transformations without flash player needing to redraw the vector..

var box : Sprite = new Sprite();
box.graphics.beginFill(0x009900);
box.graphics.drawRect(0, 0, 50, 50);
box.graphics.endFill();
addChild(box);

box.cacheAsBitmapMatrix = box.transform.concatenatedMatrix;
box.cacheAsBitmap = true;

// now move, scale, rotate and change the alpha of box much more efficiently on mobile devices
									

note: cacheAsBitmap and cacheAsBitmapMatrix are only for displayObjects that do not have nested animation. this will require flash player to redraw on each frame the contents on the displayObject that changed


   Sep 15

Blitting

i’m just writing this one little thing here about blitting in as3 so i never forget it..

var bmd : BitmapData;
// some other code here

 bmd.lock();

for(var i : uint = 0; i < 20; i++)
{
    // multiple calls to bmd.copyPixels
}

 bmd.unlock();

// bit faster
									

that’s the most important thing to know. before doing this i was wondering why the performance using blitting wasn’t any better than standard rendering.


   Sep 12

BitmapData smoothing

Turning on smoothing for Bitmap objects in as3 is easy enough..

// Bitmap(BitmapData, pixelsnapping, smoothing)
var bm : Bitmap = new Bitmap(bmd, "auto", true);
									

but what confused me is if i then draw something to the BitmapData property, smoothing gets set to false.

bm.bitmapData.draw(someDisplayObj);
// now smoothing for the Bitmap is set back to the default false. so..

bm.smoothing = true;
									

easy enough to fix


   Aug 12

FDT, ANT and FTP

if you’ve used ANT inside of FDT you might have like me thought.. can i use it to FTP my .swf to a server after compiling? answer is yes but you need to get some .jar files. i followed a nice tutorial on doing this here

http://www.funky-monkey.nl/blog/2010/09/29/using-ftp-with-ant-and-fdt/

that worked well but the server i’m working with at the moment is SFTP (SSH File Transfer Protocol) and it didn’t work. so after a bit more searching around i found how it’s done. before reading on go over the content at the link above then download this .jar file and copy it to..

FDT/plugins/org.apache.ant_1.7.1v20100518-1145/lib/

and add it to the Ant Home Entries Classpath as described in the above tutorial

then instead of using the FTP tag in ANT use SCP like this..

<scp remoteTodir="username@yourwebsite.com:/home/username/public_html" password="pword" trust="yes" sftp="true" port="22">
  <fileset dir="../bin">
    <include name="main.swf" />
    <include name="index.html />
  </fileset>
</scp>
									

info on SCP can be found here..

http://ant.apache.org/manual/Tasks/scp.html

hope this helps