Java Midp 2.0 Touch Screen Games File

Java Midp 2.0 Touch Screen Games File

Record touch down/up positions to detect direction.

private void updateGame() // Use touchX, touchY, touching for game logic

public void start() running = true; new Thread(this).start(); public void stop() running = false;

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); java midp 2.0 touch screen games

public void run() { while (running) { if (shootRequested) shootRequested = false; addBullet(playerX, playerY); updateBullets(); repaint(); try Thread.sleep(20); catch (Exception e) {} } }

class GameCanvas extends Canvas implements Runnable { private int playerX, playerY; private boolean shootRequested; private boolean running;

protected void pointerDragged(int x, int y) playerX = Math.min(Math.max(x, 10), getWidth() - 10); Record touch down/up positions to detect direction

protected void pointerDragged(int x, int y) touchX = x; touchY = y; onTouchDrag(x, y);

Assume touch exists if screen width > 240px OR model name contains "Touch"/"5800"/"S5230"/etc. But fragile. 3. Game Loop for Touch Games MIDP games use a threaded game loop with repaint() and serviceRepaints() . Basic Canvas Game Loop public class TouchGame extends Canvas implements Runnable { private volatile boolean running; private int touchX, touchY; private boolean touching; public TouchGame() setFullScreenMode(true); touchX = -1;

protected void paint(Graphics g) // Draw game, e.g. draw button if touching if (touching) g.setColor(0xFF0000); g.fillRect(touchX-10, touchY-10, 20, 20); draw button if touching if (touching) g

private int startX, startY; public void pointerPressed(int x, int y) startX = x; startY = y; public void pointerReleased(int x, int y) int dx = x - startX, dy = y - startY; if (Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > 20) if (dx > 0) swipeRight(); else swipeLeft(); else if (Math.abs(dy) > 20) if (dy > 0) swipeDown(); else swipeUp();

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();

protected void pointerReleased(int x, int y) touching = false; onTouchUp(x, y);

public void run() { while (running) { updateGame(); repaint(); try Thread.sleep(30); catch (InterruptedException e) {} } }

java midp 2.0 touch screen games
Do you have a question?

Choose below how you prefer to contact us.