In J2ME mobile game development, the MIDP2.0 version provides a class that helps with game creation, called Game Wizard Sprite, which uses Game Wizard Sprite in J2ME to simply manage the frame images used by role animations.
Here's a summary of some of the core elements of the game Wizard Sprite.
First, image segmentation
In the use of Sprite class, only need to prepare an image can be constructed a vivid animation effect, will be produced by the animation effect of the image used in order to create a picture, in the sprite construction method to set the picture source and picture of the split frame number, Sprite can automatically segment the picture according to the number of frame.
Sprite Common construction methods are: public Sprite (Image i, int framewidth, int frameheight).
It's important to note that the frames that make up the picture must be the same width, and that the number of pixels should correspond to the picture, or an IO exception will occur.
Second, picture frame settings
After sprite segmentation, the picture will be divided into separate small pictures, such as a picture composed of 4 frames, then the image index will be from 0-3 different frame objects. Using the Setframe (int index) method of the Sprite class, you can specify which frame to display at any game.
The corresponding other methods are NextFrame () and Prevframe () to obtain the frame image before and after.
Iii. the orientation and depiction of the elves
The Sprite class uses the SetPosition method to specify the coordinates that the sprite depicts (the fixed point is the upper-left vertex coordinate of the picture), thus locating the sprite's position on the screen; when depicting an elf, You only need to pass the graphics object to the parameters in the Paint method in sprite to display the wizard.
For example: Sprite object plane
Set Position: Plane.setposition (GetWidth ()/2, GetHeight ()/2);
Display Wizard: Plane.paint (g);
Four, the elves turn and flip
In the game often need to make the game object has the function of turning and flipping to make the game more vivid and realistic, in the Sprite class provides the method void SetTransform (int transform) to set the wizard's rotation and rollover effect, use it we can easily let the picture move up.
Parameter transform eight constants defined in advance, and these eight constants correspond to the following effects:
1, Trans_none: not to rotate and flip;
2, trans_rot90:90 degree rotation;
3, trans_rot180:180 degree rotation;
4, trans_rot270:270 degree rotation;
5, Trans_mirror: Flip around;
6, Trans_mirror_rot90: Turn around after turning 90 degrees;
7, trans_mirror_rot180: Turn around after turning 180 degrees;
8, trans_mirror_rot270: Turn around after turning 270 degrees.
If set elf plane rotate 90 degrees as follows:
Plane.settransform (SPRITE.TRANS_ROT90);
V. ELF reference points and target positioning
There are also two important methods in Sprite, definereferencepixel (int x,int y) and setrefpixelposition (int y,int y), which are used to set the sprite's reference point and the target location for the sprite reference point.
For example, set the reference point of the wizard plane and the anchor point as follows:
Plane.definereferencepixel (0,12);
Plane.setrefpixelposition (GetWidth ()/2,getheight ()/2);
VI. Sprite Collision Detection
In MIDP1.0, the collision is judged by the size and coordinates of the object, and the sprite is provided with a special method of collision judgment, which makes it easy to judge the character's contact.
The common method of collision judgment is Collideswith (Sprite sprite,boolean Pixellebel);
The first parameter is a Sprite object that may collide with itself;
The second parameter is pixel-level collision detection, or false, which occurs when the border of the sprite picture is touched, and if true, the collision occurs when the real pixel content in the picture is contacted, and is usually set to true to make the game more realistic.