topical media & game development

talk show tell print

actionscript-wii-Fountain.ax

actionscript-wii-Fountain.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-Fountain extends Sprite
          {
                  private var count:int = 1600;
                  private var balls:Array;
                  private var gravity:Number = 0.5;
                  
                  private var cursorX:Number = stage.stageWidth / 2;
                  private var cursorY:Number = stage.stageHeight / 2;
                  private var myWiimote:Wiimote;
                  
                  public function @ax-actionscript-wii-Fountain()
                  {
                          init();
                  }
                  
                  private function init():void
                  {
                          stage.displayState = "fullScreen";
                          
                          myWiimote = new Wiimote();
                          myWiimote.connect();
                          
                          stage.scaleMode = StageScaleMode.NO_SCALE;
                          stage.align=StageAlign.TOP_LEFT;
                          balls = new Array();
                          for(var i:int = 0; i < count; i++)
                          {
                                  var ball:actionscript_wii_Ball = new actionscript_wii_Ball(2, Math.random() * 0xffffff);
                                  ball.x = stage.stageWidth / 2;
                                  ball.y = stage.stageHeight;
                                  ball.vx = (Math.random() * 20 - 10) * (cursorX / stage.stageWidth);
                                  ball.vy = (Math.random() * -10 - 10) * (cursorY / stage.stageHeight);
                                  addChild(ball);
                                  balls.push(ball);
                          }
                          
                          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:Number = 0; i < balls.length; i++)
                          {
                                  var ball:actionscript_wii_Ball = actionscript_wii_Ball(balls[i]);
                                  ball.vy += gravity;
                                  ball.x += ball.vx;
                                  ball.y += ball.vy;
                                  if(ball.x - ball.radius > stage.stageWidth ||
                                     ball.x + ball.radius < 0 ||
                                     ball.y - ball.radius > stage.stageHeight ||
                                     ball.y + ball.radius < 5)
                                  {
                                          ball.x = stage.stageWidth / 2;
                                          ball.y = stage.stageHeight;
                                          ball.vx = (Math.random() * 20 - 10) * (cursorX / stage.stageWidth);
                                          ball.vy = (Math.random() * -10 - 10) * (0.7 + 0.3*(stage.stageHeight / cursorY));
                                  }
  
                          }
                  }
                  
                  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;
                                  
                                  //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
                  {        
                          //A Pressed Stub
                  }
                  
                  private function onBPressed ( pEvt:ButtonEvent ):void
                  {        
                          //B Pressed Stub
                  }
                  
                  private function onAReleased ( pEvt:ButtonEvent ):void
                  {        
                          //A Released Stub
                  }
          }
  }
  


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