Implement simple typing function in the game

xiaoxiao2021-03-06  92

This article tells how to implement similar to typing in game development, in fact, and the text scrolls in the previously described are not large, mainly using the functions provided by Timer and Canvas.

Our goal is to print a few words in order and certain time intervals on the screen, which requires us to calculate the width of the text and the location of the specific print. For example, such an array private final string [] content = {"Hello", "World", "WHO", "Are", "you", "?"}; I used the way to calculate their relative distance, so I Use an Int type to store these values. Private int [] slen = new int [content.length]; Slen [0] Store the word start printing, here I set it to 10, Slen [1] stores the length of the start position and the length of the word of Hello as the next The start position of the word World is pushed. Private vidin () {

INT TEMP = 10;

For (int i = 0; i

}} Note that the factors of the screen width are not considered. If you want to display multiple lines, you should consider the width of the screen, adjust your algorithm.

Inside the Paint () method we follow the obtained coordinate position Painted Protected Void Paint (Graphics Arg0) {if (first) {Arg0.SetColor (128, 0, 128); arg0.fillRect (0, 0, getWidth () Height; first = false;

Arg0.Setcolor (255, 255, 255);

Arg0.SetFont (font); arg0.drawstring (Content [i], Slen [i], height / 2, graphics.left | graphics.top);} The program is not complex, and the legend and source code are given below.

Import javax.microedition.lcdui.display; import javax.microedition.midlet.midlet; import javax.microedition.midlet.midletStateChangeException;

Public class typemidlet extends MIDlet {

Private Display Display; Private Typecanvas Typecanvas;

Protected void startapp () throws midletStateChangeException {

Display = display.getdisplay (this); typecanvas = new typecanvas (); Display.SetCurrent (Typecanvas);

}

protected void pauseapp () {

}

Protected Void DestroyApp (Boolean Arg0) throws midletStateChangeException {

}

} Import java.util.Timer; import java.util.TimerTask; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Font Import javax.microedition.lcdui.graphics;

public class TypeCanvas extends Canvas {private Timer timer = new Timer (); private int i = 0; private boolean first = true; private int height = getHeight (); Font font = Font.getFont (Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.size_medium); private final string [] content = {"Hello", "World", "WHO", "Are", "you", "?"}; Private int [] Slen = new int}; content.length ];

Public typecanvas () {super (); initslen (); typetask tt = new typesk (); timer.schedule (TT, 300, 300);

}

Protected Void Paint (Graphics Arg0) {if (first) {arg0.setcolor (128, 0, 128); arg0.fillRect (0, 0, getWidth (), height); first = false;

Arg0.Setcolor (255, 255, 255);

Arg0.SetFont (font); arg0.drawstring (Content [i], Slen [I], Height / 2, Graphics.Left | graphics.top);

private vidin () {

INT TEMP = 10;

For (int i = 0; i

}

Private class typetask extends Timertask {public void run () {

IF (i

}}}

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

New Post(0)