Game Development Reference
In-Depth Information
There are four callbacks in the controller listener above.
onButtonEvent
: It fires when a button gets pressed in the controller. Its
arguments are a boolean value (pressed) indicating the status of the
button—true if pressed, false if released—plus two integers representing
ASCII and scan code values for the pressed button.
connected
: It fires when the connection to the controller succeeds.
onMessage
: It fires when there is message sent by the
WiiC
native library
(useful for logging purposes).
onJoystickEvent
: It fires when the joystick is moved on the Nunchuck
extension. This event receives the angle in degrees (ranging from 0-360
starting from the top of the XY coordinate system), plus the distance the
joystick is moved (ranging from 0 to 1).
Furthermore, when an instance of the
Wiimote
class gets created in the previous fragment, it
triggers the loading of the native
WiiC
library by invoking
static {
System.loadLibrary("wiimote");
}
By implementing the
IController
interface, the
Wiimote
class can encapsulate messages
between the native side and the Android activity. To do so,
Wiimote
implements the following
set of native methods (which will require C counterparts, as you'll see later):
native void Initialize()
: This method invokes initialization logic on
the native side of the
WiiC
library.
native void DiscoverAndConnect()
: This method kick-starts the
Bluetooth discovery and connection process of the
WiiC
library.
native void DisconnectAll()
: This method disconnect all controllers.
WiiC
supports up to four simultaneous Wii controllers.
native void ToggleRumble()
: This method toggles the rumble feature of
the device.
native void SetLed(int led)
: This method sets the lead (or player)
number of the device.
native void Connect(String address)
: This is a utility method to
connect to a device given its Bluetooth address.
To receive messages from the native side, the
Wiimote
class implements a simple callback
system (this essentially means that the C code will invoke Java methods on Wiimote). This
callback system consists of two methods.