topical media & game development

talk show tell print

actionscript-wii-Cube.ax

actionscript-wii-Cube.ax [swf] flex


  package {
          import flash.display.Sprite;
          import flash.display.StageAlign;
          import flash.display.StageScaleMode;
          import flash.events.Event;
          
          import org.wiiflash.Wiimote;
          import org.wiiflash.events.ButtonEvent;
          import org.wiiflash.events.WiimoteEvent;
          
          [SWF(backgroundColor=0xffffff)]
          public class @ax-actionscript-wii-Cube extends Sprite
          {
                  private var points:Array;
                  private var triangles:Array;
                  private var fl:Number;
                  private var vpX:Number;
                  private var vpY:Number;
                  
                  private var cursorX:Number;
                  private var cursorY:Number;
                  private var myWiimote:Wiimote;
                  
                  public function @ax-actionscript-wii-Cube()
                  {
                          init();
                  }
                  
                  private function init():void
                  {
                          stage.displayState = "fullScreen";
                          
                          myWiimote = new Wiimote();
                          myWiimote.connect();
                          
                          stage.align = StageAlign.TOP_LEFT;
                          stage.scaleMode = StageScaleMode.NO_SCALE;
                          
                          var cubesize:Number = stage.stageWidth;
                          cursorX = vpX = stage.stageWidth / 2;
                          cursorY = vpY = stage.stageHeight / 2;
                          
                          fl = 0.5 * stage.stageWidth / 2;
                          
                          points = new Array();
                          // front four corners
                          points[0] = new actionscript_wii_Point3D(-cubesize, -cubesize, -cubesize);
                          points[1] = new actionscript_wii_Point3D( cubesize, -cubesize, -cubesize);
                          points[2] = new actionscript_wii_Point3D( cubesize,  cubesize, -cubesize);
                          points[3] = new actionscript_wii_Point3D(-cubesize,  cubesize, -cubesize);
                          // back four corners
                          points[4] = new actionscript_wii_Point3D(-cubesize, -cubesize,  cubesize);
                          points[5] = new actionscript_wii_Point3D( cubesize, -cubesize,  cubesize);
                          points[6] = new actionscript_wii_Point3D( cubesize,  cubesize,  cubesize);
                          points[7] = new actionscript_wii_Point3D(-cubesize,  cubesize,  cubesize);
                          for(var i:uint = 0; i < points.length; i++)
                          {
                                  points[i].setVanishingPoint(vpX, vpY);
                                  points[i].setCenter(0, 0, 2*cubesize);
                          }
                          
                          /*points = new Array();
                          // front four corners
                          points[0] = new actionscript_wii_Point3D(-100, -100, -100);
                          points[1] = new actionscript_wii_Point3D( 100, -100, -100);
                          points[2] = new actionscript_wii_Point3D( 100,  100, -100);
                          points[3] = new actionscript_wii_Point3D(-100,  100, -100);
                          // back four corners
                          points[4] = new actionscript_wii_Point3D(-100, -100,  100);
                          points[5] = new actionscript_wii_Point3D( 100, -100,  100);
                          points[6] = new actionscript_wii_Point3D( 100,  100,  100);
                          points[7] = new actionscript_wii_Point3D(-100,  100,  100);
                          for(var i:uint = 0; i < points.length; i++)
                          {
                                  points[i].setVanishingPoint(vpX, vpY);
                                  points[i].setCenter(0, 0, 200);
                          }*/
                          
                          triangles = new Array();
                          // front
                          //triangles[0] = new actionscript_wii_Triangle(points[0], points[1], points[2], 0x6666cc);
                          //triangles[1] = new actionscript_wii_Triangle(points[0], points[2], points[3], 0x6666cc);
                          triangles[0] = new actionscript_wii_Triangle(points[0], points[1], points[2], 0xffffff);
                          triangles[1] = new actionscript_wii_Triangle(points[0], points[2], points[3], 0xffffff);
                          // top
                          triangles[2] = new actionscript_wii_Triangle(points[0], points[5], points[1], 0x66cc66);
                          triangles[3] = new actionscript_wii_Triangle(points[0], points[4], points[5], 0x66cc66);
                          //back
                          triangles[4] = new actionscript_wii_Triangle(points[4], points[6], points[5], 0xcc6666);
                          triangles[5] = new actionscript_wii_Triangle(points[4], points[7], points[6], 0xcc6666);
                          // bottom
                          triangles[6] = new actionscript_wii_Triangle(points[3], points[2], points[6], 0xcc66cc);
                          triangles[7] = new actionscript_wii_Triangle(points[3], points[6], points[7], 0xcc66cc);
                          // right
                          triangles[8] = new actionscript_wii_Triangle(points[1], points[5], points[6], 0x66cccc);
                          triangles[9] = new actionscript_wii_Triangle(points[1], points[6], points[2], 0x66cccc);
                          // left
                          triangles[10] = new actionscript_wii_Triangle(points[4], points[0], points[3], 0xcccc66);
                          triangles[11] = new actionscript_wii_Triangle(points[4], points[3], points[7], 0xcccc66);
                          addEventListener(Event.ENTER_FRAME, onEnterFrame);
                          myWiimote.addEventListener(WiimoteEvent.UPDATE, onUpdated);
                          myWiimote.addEventListener(ButtonEvent.A_PRESS, onAPressed)
                          myWiimote.addEventListener(ButtonEvent.A_RELEASE, onAReleased);;
                          myWiimote.addEventListener(ButtonEvent.B_PRESS, onBPressed);
                          myWiimote.addEventListener(ButtonEvent.B_RELEASE, onBReleased);
                  }
                  
                  private function onEnterFrame(event:Event):void
                  {
                          var angleX:Number = (cursorY - vpY) * .0001;
                          var angleY:Number = (cursorX - vpX) * .0001;
                          for(var i:uint = 0; i < points.length; i++)
                          {
                                  var point:actionscript_wii_Point3D = points[i];
                                  point.rotateX(angleX);
                                  point.rotateY(angleY);
                          }
                          
                          graphics.clear();
                          for(i = 0; i < triangles.length; i++)
                          {
                                  triangles[i].draw(graphics);
                          }
                  }
                  
                  private function onUpdated ( pEvt:WiimoteEvent ):void 
                  {
                          /*if (myWiimote.ir.p1==true) {// && myWiimote.ir.p2==true) {
                                   //statusText.text = "Status:" + ((1-myWiimote.ir.x1) * stage.stageWidth) + (myWiimote.ir.y1 * stage.stageHeight);
                                   cursorX = (1-myWiimote.ir.x1) * stage.stageWidth;
                                  cursorY = myWiimote.ir.y1 * stage.stageHeight;
                                  ax = (2*myWiimote.ir.x1) - 1;
                                  ay = 1-myWiimote.ir.y1;
                                  
                                  //force feedback
                                  if (cursorX<10 || cursorX>stage.stageWidth-10) {
                                          myWiimote.rumbleTimeout = 300;        
                                  }
                                  if (cursorY<10 || cursorY>stage.stageHeight-10) {
                                          myWiimote.rumbleTimeout = 300;        
                                  }
                          }*/
                          
                          //motion sensor
                          if (myWiimote.a) {
                                  cursorX = (myWiimote.sensorX/2 + 0.5) * stage.stageWidth;
                          }
                          if (myWiimote.b) {
                                  cursorY = (myWiimote.sensorY/2 + 0.5) * stage.stageHeight;
                          }
                  }
                  
                  private function onAPressed ( pEvt:ButtonEvent ):void
                  {        
                          //az = 1;
                  }
                  
                  private function onAReleased ( pEvt:ButtonEvent ):void
                  {        
                          //az = 0;
                          cursorX = stage.stageWidth / 2;
                  }
                  
                  private function onBPressed ( pEvt:ButtonEvent ):void
                  {        
                          //az = -1;
                  }
                  
                  private function onBReleased ( pEvt:ButtonEvent ):void
                  {        
                          //az = 0;
                          cursorY = stage.stageHeight / 2;
                  }
          }
  }
  


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