inicio mail me! sindicaci;ón

t-roy.net*

blog by designer troy ginbey

Archive for May, 2008

scale9Grid in AS3.0

Wow… every so often you stumble upon something that you know will change the way you design forever. I was flicking through the ACP (Adobe Certified Professional) Exam Guide for Flash CS3 (as you do on a Sunday), and came across something I didn’t recognise - scale9Grid.

scale9Grid divides a display object into 9 regions, allowing different rules to be applied to it when the object is scaled.

How is this useful?? Well.. think about designing a simple content area with rounded corners. You want the content area scale vertically as the amount of text it holds increases. With any normal object with rounded corners, as you scale it’s height, the corners would scale and distort the more it is verticaly scaled. Enter scale9Grid. With one line of code, you can define the regions of the rounded rectangle to allow for distortion free scaling… Here’s an example:

myRoundedRectangle.scale9Grid = new Rectangle(10,10,300,600);

The above code adds a scale9Grid to a rounded rectangle object - meaning when it is scaled, the rounded corners will stay the same…. Brilliant!

Array Methods in AS3.0

I’ve been using Array for a while now - but don’t frequently use all of it’s methods… so I’m going to go through the ones I don’t use a lot….

Array.sort();

var beers:Array = ["Fosters", "Alpha", "VB", "Asahi"];
beers.sort(); // default sort
trace(beers); // output: Angelou,Blake,Dante,cummings

beers.sort(Array.CASEINSENSITIVE);
trace(beers); // output: Alpha,asahi,Fosters,VB

beers.sort(Array.DESCENDING);
trace(beers); // output: asahi,VB,Fosters,Alpha

beers.sort(Array.DESCENDING | Array.CASEINSENSITIVE);
trace(beers); // output: VB,Fosters,asahi,Alpha

Adding & removing display objects from the display list in AS3.0

package {
import flash.display.*;
import flash.text.TextField;

public class AS3Tester extends Sprite {
public function AS3Tester() {
var myTextContainer:TextField = new TextField();
myTextContainer.text = "I like beer";
this.addChild(myTextContainer);
}
}
}

In the above code, I create a variable to reference a new TextField object, and set it’s text to “I like beer”. This alone wont add the TextField to the stage. To do this I then need to use the addChild method to add it to the display list:

addChild(child:DisplayObject):DisplayObject

addChild is a method of DisplayObjectContainer. When a DisplayObjectContainer (a Sprite, a Loader or the Stage) calls the addChild method, it adds a child DisplayObject instance to that DisplayObjectContainer instance.

To remove an object from the display list is the reverse:
removeChild(child:DisplayObject):DisplayObject

So the code to remove the TexField above would be:
this.removeChild(myTextContainer);

If I wanted to add a display object instance to a specific DisplayObjectContainer instance, I use addChildAt:
addChildAt(child:DisplayObject, index:int):DisplayObject

Here’s an example of the code in use:

var myDisplayContainer:Sprite = new Sprite();

var myFirstSprite:Sprite = new Sprite();
var mySecondSprite:Sprite = new Sprite();

myDisplayContainer.addChild(myFirstSprite);
myDisplayContainer.addChildAt(mySecondSprite, 0);

this.addChild(myDisplayContainer);

The above code first creates a DisplayObjectContainer called myDisplayContainer, then adds a Sprite (myFirstSprite) to it. Then using addChildAt, a second sprite is added to myDisplayContainer at index 0, which is the back of the list - meaning it will be displayed underneath myFirstSprite.

The final line adds the myDisplayContainer to the stage.

Display List Programming in AS3.0

Display List programming is a big part of ActionScript 3.0… Basically anything that is visually added to the stage must be added to Flash’s Display List hierarchy.

In the next few posts I’m going to go through a few core display programming tasks…

  • Adding display objects to the display list
  • Removing objects from the display list
  • Moving objects among display containers
  • Moving objects in front of or behind other objects

Taking out the trash in AS3.0

Garbage collection is a BIG deal in AS3 - Adobe have a couple of good articles articles which explains the theory…
http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html
http://www.adobe.com/devnet/flashplayer/articles/resource_management.html

Uses for XMLList

An XMLList is a great object for working with one or more XML elements. You can call methods on the elements as a group or on the individual elements in the collection. In the example below, I’ve created an XML List to store all beers from the beers XML object that have the brand “fosters”:

beers:XML = XML;
showBeersByBrand("alpha");
private function showBeersByBrand(name:String):void {
var results:XMLList = beers.beer.(@brand == name);
showList(results);
}
private function showList(list:XMLList):void {
var item:XML;
for each(item in list) {
trace("item: " + item.toXMLString());
}
}

for.. each.. in

For each in is a handy statement to iterate over items in a collection (or object), and execute statements on those items - for example, an Object, Array, or XML. I’ll give an example of each of these…

Object
var myObject:Object = {name:"Shofferhoffer", type:"Wheat Beer", alcoholPercentage:4.5, origin:"Germany"}
for each (var item in myObject) {
trace(item);
}

outputs:
4.5
Germany
Shofferhoffer
Wheat Beer

Array
var myArray:Array = new Array("Corona", "Shofferhoffer", "Fosters");
for each (var item in myArray) {
trace(item);
}

outputs:
Corona
Shofferhoffer
Fosters

XML
var myXML:XML = merlotshirazwheat beeralelager;
for each (var item in myXML.beer) {
trace(item);
}

outputs:
wheat beer
ale
lager

The for each..in statement iterates only through the dynamic properties of an object, not the fixed properties. And unlike the for..in statement, the for each..in statement iterates over the values of an object’s properties, rather than the property names.

So, there’s a few good usage examples for for.. each.. in. Worth keeping in mind when working with objects!

AS3.0 Fundamentals

I’ve decided I’m going to start writing on a few AS3.0 coding basics - from simple for statements, to the XML Class, XML List, Array Class etc.

I’ll be posting these in a new “AS3.0 Fundamentals” category, but you’ll also find them under the Adobe Flash section.

Adobe Flash Player 10: Astro

Flash Player 10 is in beta - going by the name “Astro”. New features of the player include 3D Effects which allow you to use APIs to animate objects through 3d space, lightweight runtime Custom Filters & Effects (which use the same technology as some After Effects CS3 filters), a new advenced Text Layout system which co-exists with TextField, an enhanced Drawing API with 3D APIs, shape drawing systems and re-stylable properties. Visual processing is also shifted more to the video card, which should allow smoother video.