topical media & game development
#graphic-flex-image-effects-06-Flex-AxisTranslationTest.ax
#graphic-flex-image-effects-06-Flex-AxisTranslationTest.ax
[swf]
[flash]
flex
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
[SWF(width=550, height=400, backgroundColor=0x111111)]
Demonstrates translating display objects in 3D space by creating a number of
of sprites around a central axis but at different x, y and z positions.
Clicking on each sprite translates parent sprite to center clicked sprite on screen.
public class @ax-graphic-flex-image-effects-06-Flex-AxisTranslationTest extends Sprite {
private static const NUM_ITEMS:uint = 30;
private static const ITEM_RADIUS:uint = 40;
private static const Z_DIST_BETWEEN_ITEMS:uint = 150;
private static const ITEM_DIST_FROM_CENTER:uint = 300;
private static const ANGLE_BETWEEN_ITEMS:Number = 30*(Math.PI/180);
private var _menu:Sprite;
private var _menuItems:Vector.<Sprite>;
Constructor. Creates and positions sprites and sets up listener for stage click events.
public function @ax-graphic-flex-image-effects-06-Flex-AxisTranslationTest() {
// will hold all sprites (representing menu items)
_menu = new Sprite();
_menuItems = new Vector.<Sprite>();
for (var i:uint = 0; i < NUM_ITEMS; i++) {
createMenuItem(i);
}
addChild(_menu);
// centers initial menu item
navigateToItem(_menuItems[0]);
stage.addEventListener(MouseEvent.CLICK, onStageClick);
}
Creates a single sprite "menu item" at the specified index,
which determines its position on the stage.
parameter: index The index position of the sprite menu item.
private function createMenuItem(index:uint):void {
var menuItem:Sprite = new Sprite();
menuItem.graphics.beginFill(Math.random()*0xFFFFFF);
menuItem.graphics.drawCircle(0, 0, ITEM_RADIUS);
menuItem.graphics.endFill();
// place at radius around cetral axis with angle determined by index position
menuItem.x = Math.cos(ANGLE_BETWEEN_ITEMS*index)*ITEM_DIST_FROM_CENTER;
menuItem.y = Math.sin(ANGLE_BETWEEN_ITEMS*index)*ITEM_DIST_FROM_CENTER;
// each item is at higher z position
menuItem.z = index*Z_DIST_BETWEEN_ITEMS;
menuItem.addEventListener(MouseEvent.CLICK, onMenuItemClick);
_menuItems.push(menuItem);
// placed at bottom of display list to render below previous items
_menu.addChildAt(menuItem, 0);
}
Repositions parent sprite so that specified "menu item" is centered on stage.
parameter: menuItem The menu item sprite to navigate to.
private function navigateToItem(menuItem:Sprite):void {
// offset by center stage so that item is centered on x/y
_menu.x = stage.stageWidth/2-menuItem.x;
_menu.y = stage.stageHeight/2-menuItem.y;
_menu.z = -menuItem.z;
}
Handler for when a menu item is clicked. This navigates to the item.
parameter: event Event dispatched by menu item sprite.
private function onMenuItemClick(event:MouseEvent):void {
navigateToItem(event.target as Sprite);
// prevent stage click handler from being invoked
event.stopPropagation();
}
Handler for when the stage is clicked. This navigates to the top level item.
parameter: event Event dispatched by stage.
private function onStageClick(event:MouseEvent):void {
navigateToItem(_menuItems[0]);
}
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.