ksxd flash 05 - เมื่อ WiiMote มาเจอกับ Flash..
posted on 11 Jul 2008 04:40 by korstudio in Flash
Wii คือเครื่องเล่นเกมตัวนึงที่กำลังมาแรงในตลาดครับ
ในเวลานี้ มันสามารถติดต่อกับ Flash ผ่านทาง WiiFlash Server ได้แล้ว
WiiFlash คือชุดคำสั่งที่เอาไว้สำหรับให้ Wiimote (จอยวี) ส่งค่าไปยัง Flash ได้ โดยจะมี WiiFlash Server
เป็นตัวแปลงสัญญาณของตัว วีโมท และรับคำสั่งจาก Flash ครับ
ตอนนี้ผมลองนิดหน่อยอยู่ครับ แต่อยากเอามาแบ่งปันให้ดู :D
จริงๆ ผมได้เห็น Library ที่ชื่อ WiiFlash มานานแล้ว แต่ไม่มีโอกาสได้ลองเล่นจริงๆ ซะที
เพราะยังไม่มีตังค์ซื้อ Bluetooth USB กับ Wiimote นั่นเอง (ฮ่าๆๆๆ)
วันนี้เป็นโอกาสดีครับ ผมเลยลองเขียนโปรแกรมง่ายๆ ขึ้นมาตัวนึงเพื่อทดสอบ
เครื่องมือและไลบรารี่ ที่ผมใช้
- FlashDevelop 3.0.0 beta 7
- Flex 3 SDK Nightly Build
- WiiFlash 0.4.1
- Tweener
- Papervision3D 1.5
ถาม: ทำไมไม่ใช้ Flash CS3?
ตอบ: เพราะขี้เกียจเปิดครับ 555+
ในเวลานี้ มันสามารถติดต่อกับ Flash ผ่านทาง WiiFlash Server ได้แล้ว
WiiFlash คือชุดคำสั่งที่เอาไว้สำหรับให้ Wiimote (จอยวี) ส่งค่าไปยัง Flash ได้ โดยจะมี WiiFlash Server
เป็นตัวแปลงสัญญาณของตัว วีโมท และรับคำสั่งจาก Flash ครับ
ตอนนี้ผมลองนิดหน่อยอยู่ครับ แต่อยากเอามาแบ่งปันให้ดู :D
จริงๆ ผมได้เห็น Library ที่ชื่อ WiiFlash มานานแล้ว แต่ไม่มีโอกาสได้ลองเล่นจริงๆ ซะที
เพราะยังไม่มีตังค์ซื้อ Bluetooth USB กับ Wiimote นั่นเอง (ฮ่าๆๆๆ)
วันนี้เป็นโอกาสดีครับ ผมเลยลองเขียนโปรแกรมง่ายๆ ขึ้นมาตัวนึงเพื่อทดสอบ
เครื่องมือและไลบรารี่ ที่ผมใช้
- FlashDevelop 3.0.0 beta 7
- Flex 3 SDK Nightly Build
- WiiFlash 0.4.1
- Tweener
- Papervision3D 1.5
ถาม: ทำไมไม่ใช้ Flash CS3?
ตอบ: เพราะขี้เกียจเปิดครับ 555+
วีดีโอทดลอง WiiFlash ตัวแรกอยู่นี่ครับ:
โค้ดครับ
package {
import caurina.transitions.Tweener;
//My library, for my laziness' sake..
//Contains object creation and return that created object
import com.korstudio.utils.NewObjects;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.errors.IOError;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import org.papervision3d.cameras.Camera3D;
import org.papervision3d.materials.MovieMaterial;
import org.papervision3d.objects.Plane;
import org.papervision3d.scenes.Scene3D;
import org.wiiflash.Wiimote;
import org.wiiflash.events.ButtonEvent;
import org.papervision3d.Papervision3D;
//SWF Metadata
[SWF(width="800", height="600", backgroundColor="#FFFFFF", framerate="30")]
public class TestWiiFlash extends Sprite {
private var scene3D:Scene3D;
private var camera3D:Camera3D;
private var container:Sprite;
private var wiimote:Wiimote;
private var txtField:TextField;
private var status_txt:TextField;
private var effectEnable:Boolean = false;
private var zoomAdjust:int = 0;
private var oldRotX:Number;
private var oldRotY:Number;
private var oldRotZ:Number;
public function TestWiiFlash() {
init();
init3D();
initWiiMote();
}
public function init():void {
txtField = new TextField();
txtField.text = "Initialized";
txtField.autoSize = TextFieldAutoSize.LEFT;
addChild(txtField);
status_txt = NewObjects.NewTextField("left", 0, 50);
addChild(status_txt);
}
public function init3D():void {
container = new Sprite();
container.name = "container";
container.x = stage.stageWidth / 2;
container.y = stage.stageHeight / 2;
addChild(container);
scene3D = new Scene3D(container);
camera3D = new Camera3D();
camera3D.zoom = 10;
//New sprite and draw rectangular with 300 pixels width and height
var testSprite:Sprite = NewObjects.NewSpriteDrawRect(0x000000, 1.0, 300, 300);
var mat:MovieMaterial = new MovieMaterial(testSprite);
mat.smooth = true;
mat.doubleSided = true;
var plane:Plane = new Plane(mat, testSprite.width, testSprite.height, 1, 1);
plane.name = "testPlane";
scene3D.addChild(plane);
addEventListener(Event.ENTER_FRAME, onSceneRender);
}
public function initWiiMote():void {
wiimote = new Wiimote();
wiimote.connect();
wiimote.addEventListener( ButtonEvent.B_PRESS, onWiimoteBPressed);
wiimote.addEventListener( ButtonEvent.B_RELEASE, onWiimoteBReleased);
wiimote.addEventListener( ButtonEvent.UP_PRESS, onWiimoteUpPressed);
wiimote.addEventListener( ButtonEvent.UP_RELEASE, onWiimoteUpReleased);
wiimote.addEventListener( ButtonEvent.DOWN_PRESS, onWiimoteDnPressed);
wiimote.addEventListener( ButtonEvent.DOWN_RELEASE, onWiimoteDnReleased);
wiimote.addEventListener( Event.CONNECT, onWiimoteConnect );
wiimote.addEventListener( IOErrorEvent.IO_ERROR, onWiimoteConnectError );
wiimote.addEventListener( Event.CLOSE, onCloseConnection );
}
private function onWiimoteBPressed(event:ButtonEvent):void {
effectEnable = true;
}
private function onWiimoteBReleased(event:ButtonEvent):void {
effectEnable = false;
Tweener.removeAllTweens();
}
private function onWiimoteUpPressed(event:ButtonEvent):void {
zoomAdjust = 1;
}
private function onWiimoteUpReleased(event:ButtonEvent):void {
zoomAdjust = 0;
}
private function onWiimoteDnPressed(event:ButtonEvent):void {
zoomAdjust = -1;
}
private function onWiimoteDnReleased(event:ButtonEvent):void {
zoomAdjust = 0;
}
private function onWiimoteConnect(event:Event):void {
txtField.appendText("\n Wiimote is connected");
}
private function onWiimoteConnectError(event:IOErrorEvent):void {
txtField.appendText("\n Wiimote is not connected");
}
private function onCloseConnection(event:Event):void {
txtField.appendText("\n Wiimote is disconnected");
}
private function onSceneRender(event:Event):void {
scene3D.renderCamera(camera3D);
if(effectEnable){
var p:Plane = scene3D.getChildByName("testPlane") as Plane;
var rotY:Number = -Math.floor(wiimote.yaw * 100) * 0.5;
var rotX:Number = Math.floor(wiimote.pitch * 100) * 0.5;
var rotZ:Number = -Math.floor(wiimote.roll * 100) * 0.5;
var zoom:Number = camera3D.zoom + zoomAdjust;
if (Math.abs(oldRotX - rotX) <= 5) rotX = oldRotX;
if (Math.abs(oldRotY - rotY) <= 5) rotY = oldRotY;
if (Math.abs(oldRotZ - rotZ) <= 5) rotZ = oldRotZ;
Tweener.addTween(p, { rotationX: rotX, rotationY: rotY, rotationZ: rotZ,
time: 1, transition: "easeOutExpo" } );
Tweener.addTween(camera3D, { zoom: zoom, time: 0.8 } );
status_txt.text = "Pitch: " + rotX;
status_txt.appendText("\nYaw: " + rotY);
status_txt.text = "Roll: " + rotZ;
oldRotX = rotX;
oldRotY = rotY;
oldRotZ = rotZ;
}else {
p = scene3D.getChildByName("testPlane") as Plane;
if (!Tweener.isTweening(p)) {
Tweener.addTween(p, { rotationX:0, rotationY:0, rotationZ:0, time:1 } );
Tweener.addTween(camera3D, { zoom: 10, time: 0.8 } );
}
}
}
}
}
^^
Tags: actionscript 3, flash, flex, wii, wiiflash5 Comments
น่าลองทำเกมบ้างครับ
#1 By tumy (118.172.27.170) on 2008-07-11 17:58