Connecting a Wii Remote™
to your Flash Games

Learn how to connect a WiiMote to your Windows 7 PC and control your Flash games. You can even have your Flash game control your WiiMote!

Description:
This tutorial will be broken down into three sections. Equipment List, Hardware Installation, Software installation, and Flash ActionScript 3.

Level:
Beginner to intermediate


Section I: Equipment

In order to connect your WiiMote to your Computer you will need the following items including a Windows 7 PC:

I’ve had the best results using Windows 7 on a PC. I tested Mac OSX 10.5.3 as well as Windows XP and by far the best performance was with Windows 7.





Section 2: Hardware Installation (Windows 7)
1. insert USB Bluetooth Adapter
2. Go to Control Panel –> Hardware and Sound –> Devices and Printers
3. Confirm that the USB Bluetooth is registering with your PC. (FIG 1)

4. Right Click on the Bluetooth Adapter and go to Settings.
5. Check “Allow Bluetooth Devices to find this computer.” (FIG 2)

6. Click Add A Device
7. Press both the 1 & 2 Button of your Wii Remote at the same time until it shows up in your list of Bluetooth devices.(FIG3)

8. Click Next
9. Click “Pair without using Code” and Click “Next“.
10. Click Finished after seeing that the drivers for the Wii Remote™ were installed by Windows 7 and that the remote is available.

Section 3: Software Installation (instructions for using WiiFlash Server)
1. Download and decompress files.
2. Go inside the WiiFlash 0.4.5/Servers/ folder.
3. Launch WiiFlashServerJ.exe

The WiiFlashServer will show you how many Wii Remotes are available and each WiiMote should vibrate once.

4. From here you can go to WiiFlash 0.4.5/Samples/ folder and launch a sample that is compatible with your remote or balance board.

Tip: The samples are a good way to see if your Wii Remote™ is connected before you start coding.

Section 4:ActionScript 3 Code

Important Before you start coding!
You will need to set up the API for WiiFlash. You can do this either one of two ways.
I Accessing the source code directly
1. Go back to the WiiFlash 0.4.5 folder and drag out the org folder to put in with your other class files. The org folder is located inside Core/api/source-classes/.

II Accessing a swc
1. You can simply just point your .FLA library to the WiiFlash.swc which is located in the Core/api/swc/ folder. In Flash Publish Settings –> Flash –> ActionScript 3.0 Settings –> Library Path.
2. Click the folder icon.
3. Browse to the Core/api/swc/WiiFlash.swc.
4. Click OK.

Now you can start coding. Lets just go over an example class that you can get the Wii Remote data streaming into Flash. Use it as is, or you can break up into pieces and use as you see fit. There are enough methods here to get you started.

I’m not going to get into much detail, but rather lightly touch on them. There are some comments in the code that will also help you figure out what does what.

Sample ActionScript Class

  1.          package {
  2.  
  3.  // WiiFlash Stuff
  4.  import org.wiiflash.Wiimote;
  5.  import org.wiiflash.events.ButtonEvent;
  6.  import org.wiiflash.events.WiimoteEvent;
  7.  
  8.  public class WiiRemote extends MovieClip {
  9.   //create a new Wiimote
  10.   private var _wiimote:Wiimote;
  11.   // a private way for your app to know a Wii Remote is connected.
  12.   private var _isConnected:Boolean=false;
  13.  
  14.   public function WiiRemote() {
  15.    init();
  16.   }
  17.  
  18.   private function init():void {
  19.    setUpWiiRemote();
  20.   }
  21.  
  22.   private function setUpWiiRemote():void {
  23.    _wiimote=new Wiimote  ;
  24.    _wiimote.addEventListener(Event.CONNECT,onWiimoteConnect,false,0,true);
  25.    _wiimote.addEventListener(WiimoteEvent.CONTROLLER_DISCONNECT,onWiimoteDisco,false,0,true);
  26.    //Wii Remote look for and listen for an IR connect
  27. _wiimote.addEventListener(WiimoteEvent.IR1_FOUND,onIR1Found,false,0,true);
  28.    _wiimote.addEventListener(WiimoteEvent.IR1_LOST,onIR1Lost,false,0,true);
  29.  
  30.    _wiimote.connect();
  31.    //Anything in your app that Accesses the Mouse will be controlled by the Wii
  32.    _wiimote.mouseControl=true;
  33.  
  34.   }
  35.  
  36.   private function onWiimoteConnect(wEvent:Event):void {
  37.    // for our own info
  38.    _isConnected=true;
  39.    // listen for a press of the A Button on the WiiRemote
  40.    _wiimote.addEventListener(ButtonEvent.A_PRESS,onAPress,false,0,true);
  41.    trace("wii connected");
  42.    // get a constant update on the status of the wiiMote
  43. _wiimote.addEventListener(WiimoteEvent.UPDATE,updateWiimote);
  44.  
  45.   }
  46.  
  47.   private function onWiimoteDisco(wEvent:Event):void {
  48.    _isConnected=false;
  49.    _wiimote.removeEventListener(ButtonEvent.A_PRESS,onAPress);
  50.    
  51.    _wiimote.removeEventListener(WiimoteEvent.UPDATE,updateWiimote);
  52.    trace("connection interrupted");
  53.   }
  54.  
  55.   private function onIR1Lost(wEvent:Event):void {
  56.    trace("IR1 Lost");
  57.    // reserved for something you want to happen
  58.    // when the IR isn't picking up the WiiMote
  59.   }
  60.  
  61.   private function onIR1Found(wEvent:Event):void {
  62.    trace("IR1 Found");
  63.    // reserved for something you want to happen
  64.    // when the IR is being tracked.
  65.   }
  66.  
  67.   private function onAPress(e:ButtonEvent):void {
  68.    trace("A Button Pressed");
  69.   }
  70.  
  71.   private function updateWiimote(wEvent:WiimoteEvent):void {
  72.  
  73.    trace('a: '+_wiimote.a+'\n'+'b: '+_wiimote.b+'\n\n'+'up: '+_wiimote.up+'\n'+'down: '+_wiimote.down+'\n'+'left: '+_wiimote.left+'\n'+'right: '+_wiimote.right+'\n\n'+'minus: '+_wiimote.minus+'\n'+'home: '+_wiimote.home+'\n'+'plus: '+_wiimote.plus+'\n\n'+'1: '+_wiimote.one+'\n'+'2: '+_wiimote.two+'\n\n'+'x: '+_wiimote.sensorX+'\n'+'y: '+_wiimote.sensorY+'\n'+'z: '+_wiimote.sensorZ+'\n\n'+'pitch: '+Math.round(_wiimote.pitch*180/Math.PI)+'\n'+'roll: '+Math.round(_wiimote.roll*180/Math.PI)+'\n\n'+'nunchuk: '+_wiimote.hasNunchuk+'\n\n'+'ir1: '+_wiimote.ir.p1+'\n'+'ir2: '+_wiimote.ir.p2);
  74.  
  75.   }
  76.  
  77. /*  Here are some public methods you can call internally or from
  78.                 another class to make the Wii Remote Vibrate*/
  79.  
  80.   public function vibrateWiiMote():void {
  81.    if (_isConnected) {
  82.     // rumbleTimeout is a built in Method of WiiFlash
  83.     _wiimote.rumbleTimeout=1000;
  84.     trace("create vibration on");
  85.    } else {
  86.     // do nothing
  87.    }
  88.   }
  89.  
  90.   public function stopVibration():void {
  91.    if (_isConnected) {
  92.     _wiimote.rumble=false;
  93.    } else {
  94.     // do nothing
  95.    }
  96.   }
  97.  
  98.  }
  99. }

Summary
The WiiFlashServer is how the information from the WiiMote speaks to your SWF file. Once this data is being tracked by your SWF you can control what your SWF does internally. For example: you can make the WiiMote a pen and draw on the screen using Flash’s Graphics API.

The above code will get your Wii Remote controlling your mouse in Flash and listen for button press and even has a public method to to make the Wii Remote vibrate. You can call that function locally or from another class.

Tip: Create a boolean that is true when the WiiMote has connected to the WiiFlashServer. Mine is called _isConnected. This way you can test the app with a mouse instead of a wiiMote. This prevents errors should another class be calling the wiiMote to vibrate when its not connected.

Hopefully this is enough to get you started controlling your Flash games or interactive content with a Nintendo Wii Remote™. Good Luck!

Posted on April 22, 2011 at 10:23 pm by Runtime · Permalink
In: ActionScript 3, Flash, Games

Leave a Reply

You must be logged in to post a comment.