topical media & game development

talk show tell print

flex-sandy-box-item.ax

flex-sandy-box-item.ax [swf] flex


  package
  {
          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;
          import sandy.primitive.Box;
  
          
Boxes.as 26 September 2007
author: Dennis Ippel - www.rozengain.com

  
           
          [Event( name="clicked", type="sandydemo.flex_sandy_box_event" )]
          
          public class @ax-flex-sandy-box-item extends Sprite
          {
                  private var scene:Scene3D;
                  private var selectedMenuItem:Shape3D;
                  private var box1:Box;
                  private var box2:Box;
                  private var box3:Box;        
                  
                  public function @ax-flex-sandy-box-item()
                  {
                          init();
                  }
                  
                  private function init():void
                  {
                          // -- configure the camera, move it upwards a little
                          var camera:Camera3D = new Camera3D( 400, 200 );
                          camera.y = 25;
                          camera.z = -150;
                          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 );
                          
                          // -- box 1
                          box1 = new Box( "red box", 40, 40, 40 );
                          configureBox( box1, 0xff0000, -50, -25 );
  
                          // -- box 2
                          box2 = new Box( "yellow box", 40, 40, 40 );
                          configureBox( box2, 0xffff00, 0, -25 );
                          
                          // -- box 3
                          box3 = new Box( "green box", 40, 40, 40 );
                          configureBox( box3, 0x00ff00, 50, -25 );                
  
                          scene.render();
                  }
  
                  
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 )
                          ];
                  }
                  
                  
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 = [];
                  }
                  
                  
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 );
                          
                          var boxEvent:flex_sandy_box_event = new flex_sandy_box_event( flex_sandy_box_event.CLICKED );
                          boxEvent.which = currentShape.name;
                          
                          dispatchEvent( boxEvent );
                  }
                  
                  
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 configureBox( 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 );
                  }
          }
  }
  


(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.