Java Midp 2.0 Touch Screen Games ⭐ Exclusive Deal
(e.g., tower defense, puzzle) Store last tap point and move character toward it.
public void startApp() canvas = new GameCanvas(); display = Display.getDisplay(this); display.setCurrent(canvas); canvas.start();
protected void pointerPressed(int x, int y) playerX = Math.min(Math.max(x, 10), getWidth() - 10); shootRequested = true;
protected void pointerReleased(int x, int y) touching = false; onTouchUp(x, y); java midp 2.0 touch screen games
Record touch down/up positions to detect direction.
Most vendors ignored these until later JTWI/Java Verified phones. Use vendor-specific APIs. Nokia Touch API (S60 5th Ed / S40) // Need: com.nokia.mid.ui.TouchEvent import com.nokia.mid.ui.TouchEvent; import com.nokia.mid.ui.TouchDevice; // In FullCanvas subclass public void pointerPressed(int x, int y) // still works, but better:
protected void pointerDragged(int x, int y) playerX = Math.min(Math.max(x, 10), getWidth() - 10); Use vendor-specific APIs
protected void pointerPressed(int x, int y) touching = true; touchX = x; touchY = y; onTouchDown(x, y);
public void start() running = true; new Thread(this).start(); public void stop() running = false;
protected void pointerDragged(int x, int y) touchX = x; touchY = y; onTouchDrag(x, y); Graphics & Double Buffering for Touch Response Touch
protected void paint(Graphics g) Graphics.LEFT);
Use timestamps: record press time, check in update loop. 5. Graphics & Double Buffering for Touch Response Touch games must feel instant – input to visual feedback < 100ms. Enable double buffering: public class GameCanvas extends Canvas private Image offscreen; private Graphics offGfx; protected void sizeChanged(int w, int h) offscreen = Image.createImage(w, h); offGfx = offscreen.getGraphics();
public void run() { while (running) { updateGame(); repaint(); try Thread.sleep(30); catch (InterruptedException e) {} } }
private void updateGame() // Use touchX, touchY, touching for game logic
int dpadCenterX = 40, dpadCenterY = screenHeight - 40; if (Math.hypot(touchX - dpadCenterX, touchY - dpadCenterY) < 35) int dx = touchX - dpadCenterX; int dy = touchY - dpadCenterY; if (Math.abs(dx) > Math.abs(dy)) moveHorizontal(dx); else moveVertical(dy);