J2ME 2D Game Getting Started (2) Perfect Peripheral Tools

xiaoxiao2021-03-06  123

Second, improve the peripheral tool (image, GameObject, font)

Although we have the support of MIDP 2.0, sometimes some accessories are needed for easy us. This is afraid that it is most interesting before the real game design.

1. First is an ImageTools tool class, provide a method to help call imagepublic class imagetools {protected imagetools () {}

Public static image getimage (string str) {image img = null; try {img = image.createImage (STR);} catch (exception ex) {system.out.println (ex);} finally {return;}}}

2. GameObject provides a universal game object.

With the Sprite class, why do you have to GameObject? In fact, we generally see Sprite as a advanced image, often calling multiple game objects, GameObject is actually the Sprite's status class. GameObject provides simple lifecycle concept, animation update speed;

Public class gameObject {

Public Sprite Sprite; // Built-in Sprite Public Boolean Alive; // Save Mark Private INT LifeCount = 0; // Lifecycle Counter Public Int Lifetime = 0; // Life Cycle, with 单 单 PUBLIC INT SPEED = 0; / / Animation update speed, (0 to endless, 0 represents each with new picture) private int animcount = 0; // / Animation Update Counter

Public GameObject (Image Img, Int Width, Int Height) {sprite = new sprite (IMG, Width, Height); reset ();

Public void move (int dx, int dy) {// relative mobile sprite.move (dx, dy);}

Public void MoveTo (int X, int y) {// absolutely mobile sprite.setPosition (x, y);

Public void update () {// Update status, animation update, life cycle update

If (! alive) return; if ( animcount> speed) {animcount = 0; sprite.nextframe (); if (Lifetime! = 0 && LifeCount> Lifetime) Alive = false;}}

Public void Paint (Graphics G) {// Paint

IF (! alive) return; sprite.paint (g);}

Public void reset () {// Reset alive = true; lifecount = 0; Animcount = 0; sprite.setframe (0);}}

3. Package font class, do you need a beautiful font?

We often need to use images to output text, a convenient font class is required. We hope only one picture, an array of characters described in a picture to initialize a font class. The font class provides a method similar to Textout to facilitate information in one location. Package a simple version first, only support English and numbers, and output cannot be automatically wrapped. You may have a simple idea, just a simple saving character array, traversing the array when printing, to find Index in Frameseq in the sprite, but when we print a string, there is too much The traversal operation delayed valuable time, where we use a small skill to use capacity to exchange speed, we know Character. Hashcode () can return the ASCII code of the character, the common character will return 1-127; use this, we open up a 128 Array Charhash stores the image of the input character C in Charhash [c. Hashcode ()]. This mapping method is also used to read characters. The element of Charhash is -1, and the value is greater than 0 in the future. Public class font {

Sprite sprite; // sprite int width, height; // Each char size int [] charhash; // stores 1-127 common characters in the sprite's Frameseq location graphics g;

Public Font (Graphics G, Image Img, Int Width, Int Height, CHAR [] Chars) {

THIS.G = g; sprite = new sprite (img, width, height); this.width = width; this.height = height; charhash = new int [128]; for (int i = 0; i

Public void Drawchar (CHAR CH, INT X, INT Y) {Character C = New Character (CH); Int Hashcode = C.hashcode (); Sprite.SetPosition (x, y); if (Hashcode> = 0) {Sprite .SetFrame (Charhash [hashcode]); sprite.paint (g);}}

Public void DrawString (String Str, INT X, INT Y) {INT Length = Str.Length (); for (INT I = 0; I

This will call font.drawstring ("Hello, 0, 0) as long as there is an instance font.

A beautiful picture string is output at 0,0 position. How is it good?

转载请注明原文地址:https://www.9cbs.com/read-99722.html

New Post(0)