Tuesday, December 22, 2009

AS3: Embedding XML files

If you have a XML file called myfile.xml. Code to include this would be:

[Embed(source="mfile.xml", mimeType="application/octet-stream")]
[Bindable]
private var my_file:Class;

function some_func():void {
var my_xml:XML = XML(new my_file);
}


my_xml now contains valid XML. You can now access stuff it like normal XML variables.

Got this from the comments in this post: http://dispatchevent.org/roger/embed-almost-anything-in-your-swf/

Tuesday, November 10, 2009

AS3: Adding a Sprite as a child to a Flex Container for example a Canvas

From http://www.sebastiaanholtrop.com/archives/3 use:


import mx.core.*;

var my_canvas:Canvas = new Canvas();
var my_uic:UIComponent = new UIComponent();
var my_sprite:Sprite = new Sprite();

my_canvas.addChild(my_uic);
my_uic.addChild(my_sprite);

Sunday, November 8, 2009

Flex 3: Adding you custom component sources in the compiler

Rather then copying custom or external code libraries to your source, it is better to add it using mxmlc compiler option "--source-path=/your/path/here".

Thursday, November 5, 2009

AS3: Enabling scaling in AS3 for your games or movies

By default AS3 set scaling to NO_SCALE. If you want to override this so that your games or movies scale properly use:

stage.scaleMode = StageScaleMode.SHOW_ALL;
once your stage has been initialized.

Tuesday, October 27, 2009

AS3: Opening a window in the users browser

Use navigateToURL if you want to open a window in the users browser. For example:

 navigateToURL(new URLRequest("http://www.lazysquirrelgames.com"),  
"_blank");


Adobe documentation:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/URLRequest.html

Sunday, October 25, 2009

AS3: Customizing the preloader

By default Flash player displays a simple bar for loading progress. A little code snippet at http://www.flexdeveloper.eu/forums/actionscript-3-0/download-progress-bar-(downloadprogressbar)/ shows how to start modifying this initial screen. The code does not generate anything pretty but is a good start towards understanding what needs to be done.

The relevant adobe documents:
http://livedocs.adobe.com/flex/3/html/help.html?content=app_container_4.html
http://livedocs.adobe.com/flex/3/langref/mx/preloaders/DownloadProgressBar.html

Thursday, October 22, 2009

AS3: Latency aka delay in playing sound

Encountered a problem where I am trying to play short sounds in response to key presses and noticing a considerable delay. Googling for stuff other people seem to have encountered it. Need to test the swf on a windows box to see if what I am seeing is specific to my Linux box.

Some usefull threads on this:


http://stackoverflow.com/questions/227674?sort=oldest#sort-top

This thread indicates it may be specific to Pulseaudio: http://ubuntuforums.org/archive/index.php/t-1146361.html

No solution here but describes the problem well: http://forums.tigsource.com/index.php?topic=7927.0;wap2

EDIT: So tested this on windows XP, the lag still exist but is small enough that I can work around it.

Monday, October 12, 2009

Flex/AS3 :: Flash player debug version for Linux

Remember to use the debug flash player for Linux. There is a tarball on the adobe download page.

The standalone player is great for debugging and lets us leave our browsers flash player alone.

Thursday, October 8, 2009

AS3: Data binding is useful

Data binding is a useful and powerful concept. Its easy to do in Flex, AS3 is more complicated, the adobe docs help:

Adobe livedoc link.

Wednesday, October 7, 2009

AS3: Alpha for Sprite that has children

This one caught me out. I was wondering why I could not change the alpha of a bitmap that I had added as a child of a Sprite. Consider this code block:
var my_sprite:Sprite = new Sprite();

my_sprite.graphics.beginFill(0x000000);
my_sprite.graphics.lineStyle(1, 0x111111, 1, false,
"normal", null, null, 3);
my_sprite.graphics.drawRect(0, 0, 400, 500);
my_sprite.graphics.endFill();
my_sprite.alpha = 0.3; // This is not correct. Will affect all children

stage.addChild(my_sprite);

my_sprite.addChild(my_bitmap); // Some valid bitmap

my_bitmap.alpha = 1; // Does not work.
What I expected was that the sprite would be translucent and my bitmap would be solid. However I found the bitmap taking on the alpha of the sprite. The reason for that was I should not have been trying to change the Sprite's alpha rather I needed to use the alpha parameter of the beginFill call. So the correct code would be:
var my_sprite:Sprite = new Sprite();

my_sprite.graphics.beginFill(0x000000, 0.3); // Alpha of 0.3 for the fill
my_sprite.graphics.lineStyle(1, 0x111111, 1, false,
"normal", null, null, 3);
my_sprite.graphics.drawRect(0, 0, 400, 500);
my_sprite.graphics.endFill();

stage.addChild(my_sprite);

my_sprite.addChild(my_bitmap); // Some valid bitmap

my_bitmap.alpha = 1; // This will work now

Thursday, September 17, 2009

AS3: Getting/Keeping keyboard focus

If your application is losing keyboard focus, you can regain it by adding:

myobject.stage.focus = this;

More details in this discussion.

Sunday, September 13, 2009

AS3: Applying filter on display objects for eg. a Bitmap

Filters can be used to apply simple effects to your display object. As a example we will apply a glow filter to a Bitmap object.

The process for applying a filter to a display object is:
1. Create a new Filter object.
2. Create a temp array.
3. Copy the filters property from your display object to the temp array.
4. Add your new Filter object to the temp array.
5. Copy the temp array to the original display object filters property.

The reason for the copying out of the filters property is because AS3 does not support adding our new filter object directly to the display objects filter property.

The code will look like so:

// Parameters and default values for the GlowFilter() class are:
// GlowFilter(color:uint = 0xFF0000, alpha:Number = 1.0,
// blurX:Number = 6.0,
// blurY:Number = 6.0, strength:Number = 2,
// quality:int = 1, inner:Boolean = false,
// knockout:Boolean = false)


var _glow_filter:GlowFilter = new GlowFilter();
var _tmp_filter:Array = new Array();
var _demo_bitmap:Bitmap = new Bitmap(); // Empty, create your own bmp.

_tmp_filter = _demo_bitmap.filters;
_tmp_filter.push(_glow_filter);
_demo_bitmap.filters = _tmp_filter;

*UPDATE* (31Jan2010) : I like the tutorial here:
http://www.republicofcode.com/tutorials/flash/as3filters/

Tuesday, September 8, 2009

AS3: "include" directive to include code

The "include" directive is useful for organising code. This directive does not work like c/c++ directive, in flash it is used to inline code. So any code in the file included will be inlined where ever you specify it.

For example if you have a file constants.as residing in the same directory as your code, then the code would look like this:

Some as3 code;
include "constants.as" // Code from constants.as is inlined here.
Some more as3 code;

Monday, August 31, 2009

AS3: Creating a timer event for delay/sleep

There is no sleep() method in AS3, so one cannot do:

some code;
sleep(); // Delay for x amount of time
some more code;

So one needs to use the timer class to achieve delays and such, which means that if you come from a multi thread background and are accustomed to putting in a sleep(), you would have to rethink your approach.

The timer usage is simple enough:

import flash.events.TimerEvent;
import flash.utils.Timer;

// Create the timer, generate a tick every 500 millisec, Timer ends after 2 seconds.
var new_timer:Timer = new Timer(500, 2);

// function on_tick() will be called every 500 millisec
new_timer.addEventListener(TimerEvent.TIMER, on_tick);
// function timer_complete will be called once the timer expires, 2 secs in this case
new_timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_complete);
// Start the timer
new_timer.start();

function on_tick(event:TimerEvent):void {
// some code
}

function timer_complete(event:TimerEvent):void {
// some code
}

Note the parameters, event:TimerEvent in the functions on_tick and timer_complete, those are needed else the implementation will not work.

Tuesday, August 25, 2009

AS3: Extending Bitmap class

To extend the Bitmap class and get it to display stuff properly, we need to call the constructor of the Bitmap class in our custom class using super(). When we call Super we need to pass a BitmapData var that contains the image we want to display. This has to happen before you do addChild(your_extended_bitmap_class)

So the custom class which would need to reside in a custom_class.as file would look like this:

package {
import flash.display.Bitmap;
import flash.display.BitmapData;

public class custom_class extends Bitmap {
public var our_new_property:String = "Blah";

// We will call super in our constructor.
public function custom_class(image:BitmapData = null) {
super(image);
}
}
}

To use this class, the code would look like this:

// Embed a image to use
[Embed(source="my_image.png")]
[Bindable]
private var image:Class;

private var tmp_image:Bitmap = new image();

private var custom_image:custom_class =
new custom_class(tmp_image.bitmapData);

// Now our custom properties and all
// the Bitmap properties are accessible

custom_image.our_new_property = "some string value";
custom_image.x = some x value;
stage.addChild(custom_image); // Will now get displayed on the stage.

Multidimensional arrays in AS3

To declare a multidimensional array in AS3, we would have to create a loop, so to create for example a 2 dimensional bitmap array the code would be like so:
private var my_array:Array = new Array();

var i:int;
for (i=0; i < 20; i++) {
my_array[i] = new Array();

var j:int;
for (j=0; j < 20; j++) {
my_array[i][j] = new Bitmap();

}
}



Now we have a 20 x 20 bitmap array initialised and you can access the properties and methods of the bitmap like so:
my_array[some value][some value].x = some x value;