Documente Academic
Documente Profesional
Documente Cultură
_____________________________________________________________________________________________________
1. Topics
The real work in the SSCanvas class takes place in the paint () method, which draws
the current slide image and caption on the display.
import javax.microedition.lcdui.*;
import java.io.*;
public SSCanvas(Display d) {
super();
display = d;
__________________________________________________________________________________
File: MCOMP:\CHAP11.DOC Page: 1
IVE(TY) Mobile Computing Chapter 11 – J2ME Game Animation
_____________________________________________________________________________________________________
slides[0] = Image.createImage("/LoveCircle.jpg");
slides[1] = Image.createImage("/DoubleWide.jpg");
slides[2] = Image.createImage("/FlumeZoom.jpg");
slides[3] = Image.createImage("/KulpDeSac.jpg");
slides[4] = Image.createImage("/LouiesLedge.jpg");
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
}
void start() {
display.setCurrent(this);
repaint();
}
case RIGHT:
if (++curSlide >= slides.length)
curSlide = 0;
repaint();
break;
}
}
__________________________________________________________________________________
File: MCOMP:\CHAP11.DOC Page: 2
IVE(TY) Mobile Computing Chapter 11 – J2ME Game Animation
_____________________________________________________________________________________________________
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
3. Sprite Animation
In the case of television, the illusion of movement is created when a rapid succes-
sion of images is displayed with slight changes in their appearance. More
specifically, the human eye can be tricked into perceiving animated movement
with as low as 12 frames of movement per second (12fps). Most professional
animations use a higher frame rate. Television, for example, uses 30fps. When
you go to the movies, you see motion pictures at about 24fps. It's pretty apparent
that these frame rates are more than enough to captivate your attention and
successfully create the illusion of movement. The fact that film and television
frame rates are different means that films have to be converted to be shown on
television.
__________________________________________________________________________________
File: MCOMP:\CHAP11.DOC Page: 3
IVE(TY) Mobile Computing Chapter 11 – J2ME Game Animation
_____________________________________________________________________________________________________
The good news is that you can still do some pretty powerful things with 2D
animation.
__________________________________________________________________________________
File: MCOMP:\CHAP11.DOC Page: 4
IVE(TY) Mobile Computing Chapter 11 – J2ME Game Animation
_____________________________________________________________________________________________________
Z-order is the relative depth of sprites on the screen. If a plane sprite happens to fly
over a tank sprite, you obviously want the plane to appear above the tank and,
therefore, hide the tank as it passes over. You handle this problem by assigning each
sprite a screen depth, which is also referred to as Z-order. The depth of sprites is called
Z-order because it works sort of like another dimension-like a z axis.
You can think of sprites moving around on the screen in the XY axis. Similarly, the z
axis can be thought of as another axis projected into the screen that determines how
the sprites overlap each other. To put it another way, Z-order determines a sprite's
depth within the screen. Because they make use of a z axis, you might think that Z-
ordered sprites are 3D. The truth is that Z-ordered sprites can't be considered 3D
because the z axis is a hypothetical axis only used to determine how sprite objects
hide each other.
The easiest way to control Z-order in a game is to pay close attention to the order in
which you draw the game graphics. Fortunately, the MIDP API provides a class
called LayerManager that simplifies the task of managing multiple graphics
objects (layers) and their respective Z-orders.
Cel animation is an early version of sprite animation. Each cel sheet corresponds
to a unique Z-order value, determined by where in the pile of sheets the sheet is
located. If a sprite near the top of the pile happens to be in the same location on
the cel sheet as any lower sprites, it conceals them. The location of each sprite in
the stack of cel sheets is its Z-order, which determines its visibility precedence.
The same thing applies to sprites in cast-based animations, except that the Z-order
is determined by the order in which the sprites are drawn, rather than the cel sheet
location.
__________________________________________________________________________________
File: MCOMP:\CHAP11.DOC Page: 5
IVE(TY) Mobile Computing Chapter 11 – J2ME Game Animation
_____________________________________________________________________________________________________
Collision detection is used to determine when sprites physically interact with each
other.
Collision detection using rectangle collision as below simply involves checking to see
whether the bounding rectangles of two objects overlap.
Collision detection that uses image data collision as below involves checking the
specific pixels of the images for two objects to see whether they overlap.
__________________________________________________________________________________
File: MCOMP:\CHAP11.DOC Page: 6
IVE(TY) Mobile Computing Chapter 11 – J2ME Game Animation
_____________________________________________________________________________________________________
The Layer class models a general graphical object known as a layer, which serves as
the foundation for sprites and other graphical game objects. You can think of any
discrete visual element in a game as being a distinct layer. From a programming
perspective, the Layer class keeps track of information such as the position, width,
height, and visibility of a visual element.
** It's important to note that the Layer class is an abstract class, which means you can
never directly create an instance of a Layer object. Following are the methods
defined in the Layer class, all of which are important when it comes to working with
layers and sprites:
__________________________________________________________________________________
File: MCOMP:\CHAP11.DOC Page: 7
IVE(TY) Mobile Computing Chapter 11 – J2ME Game Animation
_____________________________________________________________________________________________________
To create a sprite based on a single image, just pass a newly created Image object
into the Sprite constructor, like this:
try{
monsterSprite = new Sprite(Image.createlmage("/Monster.png");
monsterSprite.setPosition(0, 0);
}
catch (IOException e) {
System.err.println("Failed loading image!");
}
This code uses the canvas width and height in conjunction with the sprite's width and
height to center the sprite on the screen.
2. Moving a sprite works a little differently in that you simply provide the amount
you'd like to move the sprite in each direction:
monsterSprite.move(-5,10);
In this example, the sprite is moved 5 pixels to the left and 10 pixels down. Negative
offset values indicate left (x-axis) or up (y-axis) directions, whereas positive values
indicate right (x-axis) or down (y-axis).
3. Because every Sprite object has an image associated with it, a paint()
method is provided that paints the sprite at its current position. All you have to do is
pass the paint() method a Graphics object, like this:
monsterSprite.paint(g);
This code assumes that you have a Graphics object named g, but you always have
a Graphics object handy in game painting code.
__________________________________________________________________________________
File: MCOMP:\CHAP11.DOC Page: 8
IVE(TY) Mobile Computing Chapter 11 – J2ME Game Animation
_____________________________________________________________________________________________________
If you were to use everything you've learned thus far to build an animated MIDlet
using the regular MIDP Canvas class, you would find that the resulting animation has
an annoying flicker. This flicker occurs because the game screen is cleared before the
animated graphics are painted. In other words, animated graphics objects are erased
and repainted each time they are moved. Because the erase and repaint process takes
place directly on the game screen, the animation appears to flicker.
You can solve the flicker problem associated with sprite animation by using a
technique known as double buffering. In double buffering, you perform all your
erasing and drawing on an offscreen drawing surface that isn't visible to the user.
After all the drawing is finished, the end result is painted straight to the game screen
in one pass. Because no visible erasing is taking place, the end result is flicker-free
animation.
Figure below reveals how an offscreen memory buffer is used to perform all the
incremental animation drawing, with only the finished image being drawn to the
game screen.
The GameCanvas class includes a few other interesting features that come in handy
for mobile games, such as efficient key input processing.
__________________________________________________________________________________
File: MCOMP:\CHAP11.DOC Page: 9