topical media & game development

talk show tell print

actionscript-wii-Fireworks.ax

actionscript-wii-Fireworks.ax [swf] flex


  package {
          import flash.display.Sprite;
          import flash.display.StageAlign;
          import flash.display.StageScaleMode;
          import flash.errors.IOError;
          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-Fireworks extends Sprite
          {
                  private var balls:Array;
                  private var numBalls:uint = 0;
                  private var numBallsDiff:uint = 100;
                  private var fl:Number = 250;
                  private var vpX:Number = stage.stageWidth / 2;
                  private var vpY:Number = stage.stageHeight / 2;
                  private var cursorX:Number;
                  private var cursorY:Number;
                  private var gravity:Number = 0.2;
                  private var floor:Number = 200;
                  private var bounce:Number = -0.6;
                  
                  private var myWiimote:Wiimote;
                  private var cursor:actionscript_wii_Cursor;
                  
                  public function @ax-actionscript-wii-Fireworks()
                  {
                          init();
                  }
                  
                  private function init():void
                  {
                          stage.displayState = "fullScreen";
                          
                          myWiimote = new Wiimote();
                          myWiimote.connect();
                  
                          cursor = new actionscript_wii_Cursor();
                          cursor.x = stage.stageWidth / 2;
                          cursor.y = stage.stageHeight / 2;
                          addChild(cursor);
                          
                          stage.align = StageAlign.TOP_LEFT;
                          stage.scaleMode = StageScaleMode.NO_SCALE;
                          
                          addEventListener(Event.ENTER_FRAME, onEnterFrame);
                                                  
                          myWiimote.addEventListener(WiimoteEvent.UPDATE, onUpdated);
                          myWiimote.addEventListener(ButtonEvent.A_RELEASE, onAReleased);
                          myWiimote.addEventListener(ButtonEvent.A_PRESS, onAPressed);
                          myWiimote.addEventListener(ButtonEvent.B_PRESS, onBPressed);
                  }
                  
                  private function onEnterFrame(event:Event):void
                  {
                          for(var i:uint = 0; i < numBalls; i++)
                          {
                                  var ball:actionscript_wii_Ball3D = balls[i];
                                  move(ball);
                          }
                          sortZ();
                  }
                  
                  private function move(ball:actionscript_wii_Ball3D):void
                  {
                          var radius:Number =  ball.radius;
                          
                          ball.vy += gravity;
                          ball.xpos += ball.vx;
                          ball.ypos += ball.vy;
                          ball.zpos += ball.vz;
                          
                          if(ball.ypos > floor)
                          {
                                  ball.ypos = floor;
                                  ball.vy *= bounce;
                          }
                          
                          if(ball.zpos > -fl)
                          {
                                  var scale:Number = fl / (fl + ball.zpos);
                                  ball.scaleX = ball.scaleY = scale;
                                  ball.x = vpX + ball.xpos * scale;
                                  ball.y = vpY + ball.ypos * scale;
                                  ball.visible = true;
                          }
                          else
                          {
                                  ball.visible = false;
                          }
                  }
                  
                  private function sortZ():void
                  {
                          if (numBalls>0) {
                                  balls.sortOn("zpos", Array.DESCENDING | Array.NUMERIC);
                                  for(var i:uint = 0; i < numBalls; i++)
                                  {
                                          var ball:actionscript_wii_Ball3D = balls[i];
                                          setChildIndex(ball, i);
                                  }
                          }
                  }
                  
                  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);
                                   cursor.x = cursorX = (1-myWiimote.ir.x1) * stage.stageWidth;
                                  cursor.y = cursorY = myWiimote.ir.y1 * stage.stageHeight;
                                  
                                  //force feedback
                                  if (cursorX<10 || cursorX>stage.stageWidth-10) {
                                          myWiimote.rumbleTimeout = 300;        
                                  }
                                  if (cursorY<10 || cursorY>stage.stageHeight-10) {
                                          myWiimote.rumbleTimeout = 300;        
                                  }
                          }
                  }
                  
                  private function onAPressed ( pEvt:ButtonEvent ):void
                  {        
                          removeBalls();
                          vpX = cursorX;
                          vpY = cursorY;
                          balls = new Array();
                          for(var i:uint = 0; i < numBallsDiff; i++)
                          {
                                  var ball:actionscript_wii_Ball3D = new actionscript_wii_Ball3D(3, Math.random() * 0xffffff);
                                  balls.push(ball);
                                  ball.ypos = -100;
                                  ball.vx = Math.random() * 6 - 3;
                                  ball.vy = Math.random() * 6 - 6;
                                  ball.vz = Math.random() * 6 - 3;
                                  addChild(ball);
                          }
                          numBalls += numBallsDiff;
                  }
                  
                  private function onBPressed ( pEvt:ButtonEvent ):void
                  {        
                          removeBalls();
                  }
                  
                  private function onAReleased ( pEvt:ButtonEvent ):void
                  {        
                          //A Released Stub
                  }
                  
                  private function removeBalls():void {
                          if (numBalls>0) {
                                  for(var i:uint = 0; i < numBalls; i++)
                                  {
                                          var ball:actionscript_wii_Ball3D = balls[i];
                                          removeChild(ball);
                                  }
                                  numBalls -= numBallsDiff;
                                  balls = null;
                          }        
                  }
          }
  }
  


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