Showing posts with label Tutorials. Show all posts
Showing posts with label Tutorials. Show all posts
Friday, June 18, 2010
AS3: Textfield alpha
If create a dynamic textfield and use non-embedded fonts, the alpha property will not do anything. The solution is to put the textfield in a container and set the blendMode property of that container to Layer.
Tuesday, March 30, 2010
AS3: Runtime class instantiating
This is very useful if you do custom actions based on XML or any other method. This post explains thing quite nicely:
http://thillerson.wordpress.com/2007/03/01/runtime-class-instantiation-in-actionscript-30/
http://thillerson.wordpress.com/2007/03/01/runtime-class-instantiation-in-actionscript-30/
Friday, March 5, 2010
AS3: Does my xml doc contain a attribute
This is a simple check which increases the ease of using xml.
if you have a xml file like so:
<root_node some_attribute="">
</root_node>
in AS3 you can decide if a attribute exist by using the lenght() method. So code using the xml would be:
Note it is easy to make a mistake and forget to use length as a method i.e. "length()" and use it as variable i.e. just "length" which will not work.
if you have a xml file like so:
<root_node some_attribute="">
</root_node>
in AS3 you can decide if a attribute exist by using the lenght() method. So code using the xml would be:
var my_xml:XML = some_function_to_get_xml();
if( my_xml.@some_attribute.length() != 0 ) {
trace("Yup, some_attribute exists");
}
if( my_xml.@some_non_existant_attribute.length() != 0) {
trace("This will not be printed since attribute does not exist");
}
Note it is easy to make a mistake and forget to use length as a method i.e. "length()" and use it as variable i.e. just "length" which will not work.
Wednesday, February 17, 2010
AS3: Preventing a parent from getting a Child's mouse clicks
If you have code that is like this:
Now one would expect that on clicking on the parent_sprite, the child_sprite would appear. On click on the child_sprite, some stuff would get done and then the child_sprite is removed and the parent_sprite would be the only one on screen.
What I experienced is, that the child_sprite reappeared on the screen. This was because the parent was registering a click as soon as the event for the parent was registered again in handle_button_click() and was doing a addChild(child_sprite). The reason for this is that AS3 has a concept of "bubbling" where a event is passed down to the parent once the child is done with it and it will continue down the chain to the stage.
The solution to this is to use stopPropagation() or stopImmediatePropagation(). stopPropagation() will only prevent parent from receiving the events. While stopImmediatePropagation will prevent everyone listening for the event in the chain from receiving it.
So the code would be:
var parent_sprite:Sprite = new Sprite();
parent_sprite.addEventListener(MouseEvent.CLICK,
handle_mouse_click);
addChild(parent_sprite);
function handle_mouse_click(event:MouseEvent):void {
parent_sprite.removeEventListener(MouseEvent.CLICK,
handle_mouse_click);
var child_sprite:Sprite = new Sprite();
draw_button_using_child_sprite(); // Some func to
// draw a button
child_sprite.addEventListener(MouseEvent.CLICK,
handle_button_click);
parent_sprite.addChild(child_sprite);
}
function handle_button_click(event:MouseEvent):void {
do_my_stuff();
parent_sprite.removeChild(child_sprite);
parent_sprite.addEventListener(MouseEvent.CLICK,
handle_mouse_click);
}
Now one would expect that on clicking on the parent_sprite, the child_sprite would appear. On click on the child_sprite, some stuff would get done and then the child_sprite is removed and the parent_sprite would be the only one on screen.
What I experienced is, that the child_sprite reappeared on the screen. This was because the parent was registering a click as soon as the event for the parent was registered again in handle_button_click() and was doing a addChild(child_sprite). The reason for this is that AS3 has a concept of "bubbling" where a event is passed down to the parent once the child is done with it and it will continue down the chain to the stage.
The solution to this is to use stopPropagation() or stopImmediatePropagation(). stopPropagation() will only prevent parent from receiving the events. While stopImmediatePropagation will prevent everyone listening for the event in the chain from receiving it.
So the code would be:
var parent_sprite:Sprite = new Sprite();
addChild(parent_sprite);
parent_sprite.addEventListener(MouseEvent.CLICK,
handle_mouse_click);
function handle_mouse_click(event:MouseEvent):void {
parent_sprite.removeEventListener(MouseEvent.CLICK,
handle_mouse_click);
var child_sprite:Sprite = new Sprite();
draw_button_using_child_sprite(); // Some func
// to draw a button
child_sprite.addEventListener(MouseEvent.CLICK,
handle_button_click);
parent_sprite.addChild(child_sprite);
}
function handle_button_click(event:MouseEvent):void {
do_my_stuff();
parent_sprite.removeChild(child_sprite);
parent_sprite.addEventListener(MouseEvent.CLICK,
handle_mouse_click);
event.stopPropagation(); // Prevent parent from
// receiving event
}
Got the information for this from:
http://www.kirupa.com/forum/showpost.php?p=1948149&postcount=202
Saturday, January 30, 2010
AS3: Using a String to access Object / Class properties and getDefinitionByName
Consider for example you have a wonderful asset library and have a great scheme of describing maps and such in XML. So you get a asset name from XML and now want to add it to stage.
Remember you can access a Object property like so:
wonderful_img_class in this case could refer to a image you have included using the [Embed(source=)] tag.
I initially spent time trying to use getDefinitionByName and got hit by the "Error #1065: Variable is not defined" before realising I could just do the above.
From my initial googling getDefinitionByName seems pretty unflexible and hence useless.
However if you want to investigate getDefinitionByName this post seems most useful:
http://www.rozengain.com/blog/2009/08/21/getdefinitionbyname-referenceerror-and-the-frame-metadata-tag/
Remember you can access a Object property like so:
import my_assets.graphics;
var grap:graphics = new graphics();
var my_prop:String = "wonderful_img_class"; /* Got from XML :p */
stage.addChild(new graph[my_prop]); /* Now you asset is displayed */
wonderful_img_class in this case could refer to a image you have included using the [Embed(source=)] tag.
I initially spent time trying to use getDefinitionByName and got hit by the "Error #1065: Variable is not defined" before realising I could just do the above.
From my initial googling getDefinitionByName seems pretty unflexible and hence useless.
However if you want to investigate getDefinitionByName this post seems most useful:
http://www.rozengain.com/blog/2009/08/21/getdefinitionbyname-referenceerror-and-the-frame-metadata-tag/
Wednesday, December 23, 2009
Tuesday, December 22, 2009
AS3: Embedding XML files
If you have a XML file called myfile.xml. Code to include this would be:
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/
[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:
once your stage has been initialized.
stage.scaleMode = StageScaleMode.SHOW_ALL;
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:
Adobe documentation:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/URLRequest.html
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
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
Tuesday, October 6, 2009
AS3/FLEX : Tutorials
Sites that have tutorials in no particular order:
http://www.senocular.com/flash/tutorials.php
http://www.foundation-flash.com/index.php
http://www.kirupa.com/forum/showthread.php?t=223798
http://www.senocular.com/flash/tutorials.php
http://www.foundation-flash.com/index.php
http://www.kirupa.com/forum/showthread.php?t=223798
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:
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;
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:
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:
Note the parameters, event:TimerEvent in the functions on_tick and timer_complete, those are needed else the implementation will not work.
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.
Subscribe to:
Posts (Atom)