as3 blog

thoughts on actionscript 3 development

   Oct 18

new iPad game!

Apple just approved our game for the iPad

Toyota Playground is a fun, creative app that lets kids play their favourite games with the My Family™ characters. With five interactive activities, kids create, explore and use their problem solving skills while having fun with characters based on their own family.

check it out..
App Store | Google Play


   Jun 08

Gyroscope Native Extension

Adobe released a native extension for accessing a devices Gyroscope which measures angular momentum. a great thing for games eg- steering wheel in car game.
a lot of people were having trouble getting it working on iOS (i haven’t tested on droid yet but i think it’s the same deal) with the different packaging commands available with ADT (Air Developer Tool).

http://help.adobe.com/en_US/air/build/WS901d38e593cd1bac1e63e3d128cdca935b-8000.html

the problem i and others were having was that it worked when packaging with ipa-debug-interpreter but not with ipa-ad-hoc or ipa-app-store which are the final optimized packaging options used for testing and releasing a final version. the packaging options that have ‘interpreter’ in their name are not converted to iOS machine code. they compile more quickly and send traces to the debugger but the ActionScript bytecode is interpreted not translated. the other packaging options like ipa-ad-hoc translate the AS bytecode into machine code and it runs much much better but as soon as the Gyroscope class was instantiated the app would crash.

anyway… i’ve got it working! solution is simple. for ipa-ad-hoc or ipa-app-store (translated code) the Gyroscope.swc should be removed from exports. the ane should of course still be referenced in the application descriptor file. in FDT this is done by setting the swc to “Use as Runtime Shared Code” under project properties > FDT Build Path > Build Order. i imagine there’s a similar option in Flash Builder and Flash CS5 or 6. as i understand it, this is how all ANE’s work.

i saw this information in the description of a variant of the Adobe Gyroscope developed by Christoph Ketzler found here..

http://code.google.com/p/air-native-extension-euler-gyroscope/downloads/detail?name=EulerGyroscope-2012-03-18-RELEASE.zip&can=2&q=


   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.