package com.dafishinsea.wii { import flash.display.Sprite; import flash.events.Event; import org.wiiflash.Wiimote; import org.wiiflash.events.ButtonEvent; import com.dafishinsea.sea.FishSwim; import flash.geom.*; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.text.TextField; import flash.text.TextFormat; [SWF(backgroundColor="0x000000", width="800", height="600", frameRate="24")] public class Fiish extends Sprite { private var wiimote:Wiimote; private var fish:FishSwim; private var yaw_txt:TextField; private var pitch_txt:TextField; private var roll_txt:TextField; private var last_roll:int = 0; private var last_pitch:int = 0; private var fishSpeed:Vector3D = new Vector3D(1,0,0); public function Fiish() { init(); } private function init():void { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; //add fish fish = new FishSwim(0xFF0000); fish.x= stage.stageWidth/2; fish.y= stage.stageHeight/2; fish.z= 50; fish.velocity.x = 1; addChild(fish); //setup wiimote wiimote = new Wiimote(); wiimote.connect(); wiimote.addEventListener(ButtonEvent.A_PRESS, onAPressed); wiimote.addEventListener(ButtonEvent.B_PRESS, onBPressed); addEventListener(Event.ENTER_FRAME, onEnterFrame); //some textfields to monitor values var tf:TextFormat = new TextFormat(); tf.color = 0xFF0000; tf.font = "Verdana"; yaw_txt = new TextField(); yaw_txt.x = 10; yaw_txt.y = 10; yaw_txt.defaultTextFormat = tf; addChild(yaw_txt); pitch_txt = new TextField(); pitch_txt.x = 10; pitch_txt.y = 20; pitch_txt.defaultTextFormat =tf; addChild(pitch_txt); roll_txt = new TextField(); roll_txt.x = 10; roll_txt.y = 40; roll_txt.defaultTextFormat = tf; addChild(roll_txt); } private function onEnterFrame(event:Event):void { graphics.clear(); var roll:Number = int(wiimote.roll*180/Math.PI); var pitch:Number = int(wiimote.pitch*180/Math.PI); //get average of this and last measurments roll = int((roll+last_roll)/2); pitch = int((pitch+last_pitch)/2); roll_txt.text = "roll: " + roll; pitch_txt.text = "pitch: " + pitch; //fish.rotationY = roll*2; //fish.rotationZ = pitch*2; fish.rotationY += roll/45; fish.rotationZ += pitch/45; //fishspeed = new Vector3D(1,0,0); var fishMatrix:Matrix3D = fish.transform.matrix3D; var fishVelocity:Vector3D = fishMatrix.deltaTransformVector(fishSpeed); fish.x += fishVelocity.x; fish.y += fishVelocity.y; fish.z += fishVelocity.z; fish.render(); last_pitch = pitch; last_roll = roll; } private function onAPressed(event:ButtonEvent):void { trace("A Pressed!"); fishSpeed.x += 1; } private function onBPressed(event:ButtonEvent):void { trace("B Pressed!"); fishSpeed.x -= 1; if(fishSpeed.x < 0) fishSpeed.x = 0; } } }