//mobile and embedded /
To keep things simple, in the following sections I will show you the interactions only from Player 1’s perspective.
The server and Player 2 interactions are
easy to code and conceptualize, so I
will leave these as an exercise for you.
First things first: we need to create our
GameCanvas and add the code for the
Mobile Sensor API.
sor, and if it does, we determine whether
we can connect to it and receive data
from it, as shown in Listing 2.
If the sensor is not found, then the
game cannot be played, and the player
should be informed. When it is found,
we make a connection to it, and set the
TiltCanvas class as the listener for receiving the data from it.
The sensor sends the current title of
the device along the three axes: x, y, and
z. Because we only want to know how
the player has tilted the device along the
vertical axis, we use only the y axis data.
So, if the player tilted the device more
than the previous tilt, the ball is rolled to
the right. On the other hand, if the player tilted the device less than the previous
tilt, the ball is rolled to the left. Once we
have reached the end of the screen, either along the left or right side, we notify
the server with the current data.
There are game specifics in this listing
that I haven’t discussed yet. Listing 3
shows the corresponding code for this
class that is responsible for drawing the
ball depending on the location data it
gets from the dataReceived method.
This is a very standard method for
a gaming application. The screen is
cleared, and based on the current game
data, objects on the screen are redrawn.
In our case, the current position of the
ball is drawn on the screen along the x
axis, because the y axis is presumed to
be constant.
// the graphics object for this screen
Graphics g;
public TiltCanvas() {
super(true);
SensorInfo sensors[] =
SensorManager.findSensors(
"acceleration", SensorInfo.CONTEXT_TYPE_DEVICE);
JAVA IN ACTION
The TiltCanvas Class
The TiltCanvas class is responsible for
handling game interactions and implementing the Mobile Sensor API code.
Listing 1 shows the constructor.
The TiltSensor constructor does the
initialization by opening a connection
to the sensor for the accelerometer,
and it also loads the basic ball image.
Note that it makes a call to the super-constructor because TiltCanvas extends
GameCanvas.
if (sensors == null || sensors.length == 0) {
System.err.println("Nothing found!");
} else if (sensors.length > 1) {
System.err.println("Too many sensors found!");
} else {
tiltSensor = sensors[0];
try {
// make a connection to this sensor
connection = (SensorConnection) Connector.open( tiltSensor.getUrl()); connection.
// load the ball image
ballImage = Image.createImage("/ red_ball.jpg");
class TiltCanvas extends
GameCanvas implements
DataListener, Runnable
ABOUT US
We also need to implement the
DataListener interface of the Mobile
Sensor API to receive data when the device is tilted. Besides this, the Runnable
interface is implemented to run the
code in a thread.
setupCorrect = true;
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
See all listings as text
Mobile Sensor API Implementation
In our game, we want a particular type of
sensor. This sensor is universally known
as the acceleration sensor. So, in the
constructor of our TiltCanvas class we
find out whether the device has this sen-
Conclusion
In this article, we discussed the basics of
gaming and the need for the GameCanvas
class. We also saw how to incorporate
the Mobile Sensor API into the specific
game we were building.
In the next article, we will learn how
to incorporate the Location API into our
game. </article>
LEARN MORE
•;“J2ME Tutorial, Part 3: Exploring the
Game API of MIDP 2.0”
•;“Working with the Mobile
Sensor API”
blog