- Home
- Articles
- Best Practices
- Working With Display Objects in AS 3.0
Working With Display Objects in AS 3.0

putting it all together
Patrik Vormittag
I am an interactive digital artist at Crispin Porter + Bugosky. Currently creating RIA's and pushing the powers of flash/flex. To view my work or to contact me, please visit my portfolio - http://www.previsualsdesign.com.
View all articles by Patrik Vormittagimport flash.display.Sprite;
import flash.events.*;
import flash.display.MovieClip;
import flash.filters.DropShadowFilter;
import flash.text.TextFormatAlign;
import flash.text.TextField;
import flash.text.TextFormat;
stage.align = "TL";
stage.scaleMode = "noScale";
var boxContainer:Sprite = new Sprite();
var columns:int = 5;
var rows:int = 5;
var boxSpacing:int = 2;
var boxWidth:uint = stage.stageWidth / rows;
var boxHeight:uint = stage.stageHeight / columns;
var startX:Number = boxSpacing;
var startY:Number = boxSpacing;
var boxIndex:int = 0;
var dropShadow:DropShadowFilter = new DropShadowFilter();
dropShadow.distance = 3;
dropShadow.angle = 45;
dropShadow.alpha = 20;
dropShadow.blurX = dropShadow.blurY = 6;
for(var j:int = 0; j < columns; ++j){
startX = boxSpacing;
for(var i:int = 0; i < rows; ++i){
var box:Sprite = new Sprite();
box.name = "box"+ ++boxIndex;
box.x = startX;
box.y = startY;
box.buttonMode = true;
box.graphics.lineStyle(1, 0x999999, 1, true);
box.graphics.beginFill(0x333333, 1);
box.graphics.drawRect(0, 0, boxWidth - boxSpacing, boxHeight - boxSpacing);
box.filters = [dropShadow];
box.addEventListener(MouseEvent.MOUSE_OVER, boxOnMouseOver);
box.addEventListener(MouseEvent.MOUSE_OUT, boxOnMouseOut);
box.addEventListener(MouseEvent.MOUSE_DOWN, boxOnPress);
box.addEventListener(MouseEvent.MOUSE_UP, boxOnRelease);
startX += box.width + boxSpacing;
var boxName:TextField = new TextField();
boxName.text = box.name;
boxName.width = boxWidth;
boxName.height = 25;
boxName.y = (box.height / 2) - (boxName.height / 2);
boxName.mouseEnabled = false;
boxName.wordWrap = true;
var boxNameFormat:TextFormat = new TextFormat("verdana", 10, 0x666666, null, null, null, null, null, TextFormatAlign.CENTER);
boxName.setTextFormat(boxNameFormat);
box.addChild(boxName);
boxContainer.addChild(box);
}
startY += box.height + boxSpacing;
}
addChild(boxContainer);
function boxOnMouseOut(evt:MouseEvent){
if(evt.eventPhase == EventPhase.AT_TARGET){
evt.target.graphics.clear();
evt.target.graphics.lineStyle(1, 0x999999, 1, true);
evt.target.graphics.beginFill(0x333333, 1);
evt.target.graphics.drawRect(0, 0, boxWidth - boxSpacing, boxHeight - boxSpacing);
}
}
function boxOnMouseOver(evt:MouseEvent){
if(evt.eventPhase == EventPhase.AT_TARGET){
evt.target.graphics.clear();
evt.target.graphics.lineStyle(1, 0x333333, 1, true);
evt.target.graphics.beginFill(0x999999, 1);
evt.target.graphics.drawRect(0, 0, boxWidth - boxSpacing, boxHeight - boxSpacing);
}
}
function boxOnPress(evt:MouseEvent){
if(evt.eventPhase == EventPhase.AT_TARGET){
evt.target.parent.setChildIndex(evt.target, (evt.target.parent.numChildren - 1));
evt.target.startDrag();
}
}
function boxOnRelease(evt:MouseEvent){
if(evt.eventPhase == EventPhase.AT_TARGET){
evt.target.stopDrag();
}
}
Which outputs:
This example uses a lot more then just manipulation of DisplayObjects, such as EventHandlers and working with the Flash Drawing API (The handling of events is another main difference in AS 3.0, but that will need to wait for another tutorial). I hope this helped! Enjoy!
Spread The Word
Related Articles
7 Responses to "Working With Display Objects in AS 3.0" 
|
said this on 10 Jul 2007 9:54:37 AM CDT
smart, precise, straightforward.... great!
|
|
said this on 27 Jul 2007 5:28:04 AM CDT
Very Good
I learn lot frm it. Bt its incomplete for me being a beginner of AS3. If there is any solution of how to open a new image window when click on the thumb, I can also leanr next lot. Thank u |
|
said this on 05 Aug 2007 4:10:12 PM CDT
Just a minor correction:
When he did the example with the bitmaps pic1, pic2 and pic3 he did not really swap them because he put pic3 to the position 2 wich is actually the third position because its an array. I guess he meant: pictureHolder.addChildAt(picture3, 1); |
|
said this on 12 Aug 2007 9:00:20 AM CDT
nice work, everything clear. But there's a small problem. when you drag a box its movement is not perfectly smooth. I mean when you drag a box fast, it starts to behave strange. the effect of dragging is not as expected. changing position is "lagged". How to get rid of the problem?
|
|
said this on 15 Aug 2007 4:57:48 PM CDT
hi, just wanted to say that the final example really solved a problem I had been working on for a day.
Thanks! |
|
said this on 07 Sep 2007 4:38:35 PM CDT
Very Very Good. Excellent!
I am a beginner of Flash, and I think the boxSpacing and shodow should be consider when determine the boxWidth and boxHiehgt. var boxWidth:uint = (stage.stageWidth - boxSpacing * 2 - 3) / rows; var boxHeight:uint = (stage.stageHeight - boxSpacing * 2 - 3) / columns; |
|
said this on 11 Oct 2007 9:58:07 PM CDT
Great tutorial on creating dynamic buttons in AS3 with text! Its hard to find a straight-forward tutorial that shows AS3-only buttons with dynamic textfields. Thanks!
|


Author/Admin)