Wii Getting Started and Starter Code

We are dedicating a chapter of your book to creating Papervision games and programming the Wii. Getting started with the Wii is really easy and super fun. And if you have nine kids like I do, you’ll be an immediate success at home. Take cover as your kids scramble in front of your computer frantically fighting over the Wii controller to try out you newest and greatest Wii creation. With the Wii, you add a new dimension to your 3D experience. Here are the steps to get you started (along with helpful starter code as well):

  • Buy a Wii controller (you need only the controller). I got a new one off of Amazon for $35 (I suggest you buy new)
  • Check out the great WiiFlash video tutorial by Thibault Imberton
  • Download the WiiFlash classes at http://wiiflash.bytearray.org/?page_id=50
  • Insert the classes into your Flex or Flash project (you can use classes or the SWC provided)
  • Try out the WiiFlash examples given in the WiiFlash Download.
  • If you do not have bluetooth on your machine purchase an external bluetooth USB connector. I got the AZiO BTD603-132 USB 2.0 Bluetooth Adaptermine from NewEgg.com for $14.99. Note: You must install the driver that comes with your external bluetooth for it to detect your Wii properly.
  • Place a Wii server shortcut on your desktop – you’ll be clicking on it a bunch.
  • Download my starter code and run it, examine the trace statements as you press the different buttons on your Wii.
  • Add the Papervision classes to your project files and you are ready to make your first Papervision Wii game.

With the Wii starter code and the Papervision classes you are now ready to start programming your own Wii games. More on that later;

Wii Starter Code Creation

The Wii starter code is amazingly simple. It’s adapted from the Wiimote Demo provided in the WiiFlash examples folder. And it is designed to make it easy for you to put anywhere. Here is how it works.

  • After importing the appropriate classes, you must instantiate the Wiimote and then connect to your Wii server as follows;

MyWiiIs = new Wiimote();
MyWiiIs.connect();

  • Next declare a listener for a button on your Wii controller (both press and release are detectable)

MyWiiIs.addEventListener( ButtonEvent.A_PRESS, onAPressed );
MyWiiIs.addEventListener( ButtonEvent.A_RELEASE, onAReleased);

  • Finally, create functions for your listeners that are executed when you click on the appropriate button;

private function onAPressed ( pEvt:ButtonEvent ):void
{
trace(“pressed A”);
}

private function onAReleased ( pEvt:ButtonEvent ):void
{
trace(“released A”);
}

The only other thing to watch out for is reacting to pitch, yaw, roll, sensorX, sensorY, sensorZ, and batteryLevel. I found that this was best handled in Papervision’s render loop using your instantiated name and dot syntax such as

MyWiiIs.pitch

Also, depending on the application you might need a smoothing function since the Wii numbers are very accurate and a little jumpy, and also you must take the integer value of the Wii sensor values (shown below) or you may get a lookAt error. In the example below, you’re controlling the rotation of a sphere using Wii pitch, yaw, and roll. But you take the interger value. To get two decimal places, multiply by 100 and then divide by 100 after taking the integer value as shown below.

sphere.rotationX+=-4*(int(MyWiiIs.pitch*100)/100);
sphere.rotationY+=4*(int(MyWiiIs.yaw*100)/100);
sphere.rotationZ+=4*(int(MyWiiIs.roll*400)/100);

That’s all there is to it. As powerful as WiiFlash is, it’s amazing that it is so simple to use.

Click more to see the complete web code.

package {
import flash.display.Sprite;
import org.wiiflash.Wiimote;
import org.wiiflash.events.ButtonEvent;
import org.wiiflash.events.WiimoteEvent;
public class MyWiiBase extends Sprite
{ private var MyWiiIs:Wiimote;

public function MyWiiBase()
{

MyWiiIs = new Wiimote();
MyWiiIs.connect();

WiiListeners();

}

private function WiiListeners():void{

//Pitch, Yaw, Roll
MyWiiIs.addEventListener( WiimoteEvent.UPDATE, onUpdated );
//Bottons
MyWiiIs.addEventListener( ButtonEvent.A_PRESS, onAPressed );
MyWiiIs.addEventListener( ButtonEvent.A_RELEASE, onAReleased);
MyWiiIs.addEventListener( ButtonEvent.LEFT_PRESS, onLeftPressed );
MyWiiIs.addEventListener( ButtonEvent.LEFT_RELEASE, onLeftReleased);
MyWiiIs.addEventListener( ButtonEvent.RIGHT_PRESS, onRightPressed );
MyWiiIs.addEventListener( ButtonEvent.RIGHT_RELEASE, onRightReleased);
MyWiiIs.addEventListener( ButtonEvent.UP_PRESS, onUpPressed );
MyWiiIs.addEventListener( ButtonEvent.UP_RELEASE, onUpReleased);
MyWiiIs.addEventListener( ButtonEvent.DOWN_PRESS, onDownPressed );
MyWiiIs.addEventListener( ButtonEvent.DOWN_RELEASE, onDownReleased);
MyWiiIs.addEventListener( ButtonEvent.B_PRESS, onBPressed );
MyWiiIs.addEventListener( ButtonEvent.B_RELEASE, onBReleased);
MyWiiIs.addEventListener( ButtonEvent.MINUS_PRESS, onMinusPressed);
MyWiiIs.addEventListener( ButtonEvent.MINUS_RELEASE, onMinusReleased);
MyWiiIs.addEventListener( ButtonEvent.PLUS_PRESS, onPlusPressed);
MyWiiIs.addEventListener( ButtonEvent.PLUS_RELEASE, onPlusReleased);
MyWiiIs.addEventListener( ButtonEvent.HOME_PRESS, onHomePressed);
MyWiiIs.addEventListener( ButtonEvent.HOME_RELEASE, onHomeReleased);
MyWiiIs.addEventListener( ButtonEvent.ONE_PRESS, onOnePressed);
MyWiiIs.addEventListener( ButtonEvent.ONE_RELEASE, onOneReleased);
MyWiiIs.addEventListener( ButtonEvent.TWO_PRESS, onTwoPressed);
MyWiiIs.addEventListener( ButtonEvent.TWO_RELEASE, onTwoReleased);

}

private function onUpdated ( pEvt:WiimoteEvent ):void

{

//trace(pEvt.target.pitch +” pitch”);
//trace(pEvt.target.roll +” roll”);
//trace( pEvt.target.yaw +” yaw”);

//trace(pEvt.target.sensorX +” x”);
//trace(pEvt.target.sensorY +” y”);
//trace(pEvt.target.batteryLevel +” battery level”);

}

private function onAPressed ( pEvt:ButtonEvent ):void

{

trace(“pressed A”);

}

private function onAReleased ( pEvt:ButtonEvent ):void
{

trace(“released A”);

}

private function onMinusPressed ( pEvt:ButtonEvent ):void
{
trace(“onMinusPressed”);

}

private function onMinusReleased ( pEvt:ButtonEvent ):void
{

trace(“onMinusReleased”);

}

private function onPlusPressed ( pEvt:ButtonEvent ):void
{
trace(“onPlusPressed”);

}

private function onPlusReleased ( pEvt:ButtonEvent ):void
{

trace(“onPlusReleased”);

}

private function onHomePressed ( pEvt:ButtonEvent ):void
{

trace(“Home Pressed”);

}

private function onHomeReleased ( pEvt:ButtonEvent ):void
{
trace(“Home Released”);

}

private function onOnePressed ( pEvt:ButtonEvent ):void
{

trace(“onOnePressed”);

}

private function onOneReleased ( pEvt:ButtonEvent ):void
{
trace(“onOneReleased”);

}

private function onTwoPressed ( pEvt:ButtonEvent ):void
{

trace(“onTwoPressed”);

}

private function onTwoReleased ( pEvt:ButtonEvent ):void
{
trace(“onTwoReleased”);

}

private function onBPressed ( pEvt:ButtonEvent ):void
{

trace(“onBPressed”);

}

private function onBReleased ( pEvt:ButtonEvent ):void

{

trace(“onBReleased”);
}

private function onUpPressed ( pEvt:ButtonEvent ):void
{

trace(“onUpPressed”);

}

private function onUpReleased ( pEvt:ButtonEvent ):void
{

trace(“onUpReleased”);

}

private function onLeftPressed ( pEvt:ButtonEvent ):void

{

trace(“onLeftPressed”);

}

private function onLeftReleased ( pEvt:ButtonEvent ):void
{

trace(“onLeftReleased”);

}

private function onRightPressed ( pEvt:ButtonEvent ):void
{

trace(“onRightPressed”);
}

private function onRightReleased ( pEvt:ButtonEvent ):void

{

trace(“onRightReleased”);

}

private function onDownPressed ( pEvt:ButtonEvent ):void
{

trace(“onDownPressed”);

}

private function onDownReleased ( pEvt:ButtonEvent ):void
{

trace(“onDownReleased”);

}

}}


About these ads

2 Responses to Wii Getting Started and Starter Code

  1. […] the Wii controlled silly string program is the line3D class and the Wii starter code provided in a previous post. Drawing a 3D line is really simple in Papervision. Just create a new line as shown […]

  2. […] and I left the Wii code inside the project just in case I needed to reactivate it. Check out my Wii Post to learn how to create a Wii controlled […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: