Looking+at+the+source+code+of+Wiimote+Demo+fla

=Looking at the source code of Wiimote Demo.fla=

As yet we have not moved the files from the unzipped folders or saved another version of the Wiimote demo.fla. We will do this later, as it is important to understand that moving the Flash file from its current location will make your application fail. We will discuss the location of files later.

The Wiimote Demo.fla contains all of the code required to get some action to take place on any button press of the WiiMote. As well the code contains necessary functions that will measure the pitch, yaw, roll, etc. You can even find out how much battery is left. Mine is down to 0.435 so I had better type faster....

If you haven't opened the Wiimote Demo.fla file do this now. You will need to make sure that you can see the line numbers in the code window. If you can't see the line numbers refer to the Flash help files for guidance.

The first part of the code (to line **17**) sets up the Wiimote connection and imports all associated ActionScript classes, as there are files to import. It is important that we maintain the paths to all of the imported ActionScript files. This is why we can't save or move the .fla to a different folder yet.

Next a synchronize object is created (lines **19 to 21**), this appears to be a text box that appears to tell you whether the server has connected to Flash successfully or not. It will also indicate if the server closes.

Directly after that is the first event listener (line **23**) that is used which isn't a Wiimote event but just a plain old Flash stage event. Though it triggers a call to a function called onClick (line **26**) that makes the Wiimote rumble stop after one second.

Lines **34** and **35** position the synchronize object on the stage.

Lines **38** to **63** register all of the events that might occur on a Wiimote. Such as A button pressed and A button released.

The functions, for the most part, are self explanatory. When the A button on the Wiimote is pressed it calls the function **onAReleased.** This then shows the Wiimote in the Flash application when it is running, with the A button pressed. The code used to do this is:

a_btn.gotoAndStop( Boolean ( pEvt.state )+ 1 );


 * a_btn is the name of the movie clip that holds the a button picture.
 * gotoAndStop sends the playhead to the frame required of the a_btn movie clip.
 * Boolean - means true or false
 * pEvt is the object on which the event took place
 * state indicates, I believe that the button is on (1 or true) or off (0 or false)
 * adding 1 to pEvt.state creates a number that is 1 or 2

Therefore the code above is a fairly convoluted way of saying a_btn.gotoAndStop(2). Was it necessary?? Well it looks pretty snappy doesn't it?

The only other thing worth mentioning is the final function that appears at line **271** onUpdated. This appears to run like an EnterFrame event. Looking through this function you will see how the pitch, yaw, etc are accessed by the Flash ActionScript code. You will be able to use the values produced by these properties to produce particular effects in your own code.

In the case of this function pEvt.target is the Wiimote itself. Therefore pEvt.target.pitch is the current actual pitch of the Wiimote in your hand.

Next we will explore how to create an application using only these events found in this current code.