topical media & game development
flex-sandy-interactive.ax
flex-sandy-interactive.ax
[swf]
flex
package {
import caurina.transitions.Tweener;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.GlowFilter;
import sandy.core.Scene3D;
import sandy.core.scenegraph.Camera3D;
import sandy.core.scenegraph.Group;
import sandy.core.scenegraph.Shape3D;
import sandy.materials.Appearance;
import sandy.materials.ColorMaterial;
import sandy.materials.attributes.LightAttributes;
import sandy.materials.attributes.MaterialAttributes;
[SWF(width="400", height="200", backgroundColor='#000000', frameRate='24', quality='high')]
SandyInteractivity.as
24 September 2007
author: Dennis Ippel - www.rozengain.com
public class @ax-flex-sandy-interactive extends Sprite
{
private var scene:Scene3D;
private var selectedMenuItem:Shape3D;
private var menuItem1:flex_sandy_menu_1;
private var menuItem2:flex_sandy_menu_2;
private var menuItem3:flex_sandy_menu_3;
public function @ax-flex-sandy-interactive()
{
init();
addEventListener( Event.ENTER_FRAME, enterFrameHandler );
}
private function init():void
{
// -- configure the camera, move it upwards a little
var camera:Camera3D = new Camera3D( 400, 200 );
camera.y = 25;
camera.z = -75;
camera.lookAt( 0, 0, 0 );
// -- creat the root group and the scene
var root:Group = new Group( "root" );
scene = new Scene3D( "scene", this, camera, root );
// -- menu item 1
menuItem1 = new flex_sandy_menu_1( "menuItem1" );
configureMenuItem( menuItem1, 0xff0000, -50, -25 );
// -- menu item 2
menuItem2 = new flex_sandy_menu_2( "menuItem2" );
configureMenuItem( menuItem2, 0xffff00, 0, -25 );
// -- menu item 3
menuItem3 = new flex_sandy_menu_3( "menuItem3" );
configureMenuItem( menuItem3, 0x00ff00, 50, -25 );
}
The handler for the mouse over event
parameter: event
private function mouseOverHandler( event:MouseEvent ):void
{
// -- get the target container. The event handler
// was attached to the Shape3D's container. Here
// we use it again to apply a filter
var container:Sprite = event.target as Sprite;
// -- apply a glow filter to the container
container.filters = [
new GlowFilter( 0xffffff, 1, 12, 12 )
];
// -- get the Shape3D this container is in
var currentShape:Shape3D = getShape3DByContainer( container );
// -- create a tween
Tweener.addTween( currentShape, {
rotateX: 90,
time : 1,
transition : "easeOutBounce"
});
}
The handler for the mouse out event
parameter: event
private function mouseOutHandler( event:MouseEvent ):void
{
// -- get the target container. The event handler
// was attached to the Shape3D's container. Here
// we use it again to remove a filter
var container:Sprite = event.target as Sprite;
container.filters = [];
// -- get the Shape3D this container is in
var currentShape:Shape3D = getShape3DByContainer( container );
// -- create a tween
Tweener.addTween( currentShape, {
rotateX: 0,
time : 0.5,
transition : "easeOutBounce"
});
}
The handler for the mouse click event
parameter: event
private function clickHandler( event:MouseEvent ):void
{
// -- get the target container.
var container:Sprite = event.target as Sprite;
// -- get the Shape3D this container is in
var currentShape:Shape3D = getShape3DByContainer( container );
// -- create a tween
Tweener.addTween( currentShape, {
z: 50,
time : 0.5,
transition : "easeOutSine",
onComplete : function():void
{
// -- tween back to the original position
Tweener.addTween( currentShape,
{
z: 0,
time : 1,
transition : "easeOutSine"
});
}
});
}
Compares the given container Sprite to all the container Sprites
in the scene.
parameter: container
returns:
private function getShape3DByContainer( container:Sprite ):Shape3D
{
for each( var shape:Object in scene.root.children )
if( shape is Shape3D && shape.container == container )
return shape as Shape3D;
return null;
}
Adds standard properties to the items
parameter: item
parameter: color
parameter: x
parameter: y
private function configureMenuItem( item:Shape3D, color:uint, x:int, y:int ):void
{
// -- appearance / material properties
var mattAttr:MaterialAttributes = new MaterialAttributes(
new LightAttributes( false, 0.3 )
);
item.appearance = new Appearance(
new ColorMaterial( color, 100, mattAttr )
);
item.appearance.frontMaterial.lightingEnable = true;
// -- position
item.x = x;
item.y = y;
// -- add event listeners to this Shape3D's container. The
// container is a Sprite, native to Flash
item.container.addEventListener( MouseEvent.MOUSE_OVER, mouseOverHandler );
item.container.addEventListener( MouseEvent.MOUSE_OUT, mouseOutHandler );
item.container.addEventListener( MouseEvent.CLICK, clickHandler );
item.container.useHandCursor = true;
item.container.buttonMode = true;
// -- add this Shape3D to the scene
scene.root.addChild( item );
}
private function enterFrameHandler( event:Event ):void
{
scene.render();
}
}
}
(C) Æliens
27/08/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.