ok, im on my fone, so when i get back ill post the app
So, i got it to move up and down, but when i press down, it moves, up and vise versa. Also when i try to switch directions, but don't release my finger from the screen it keeps going the same way, can you help?
package pong;
import java.io.IOException;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class PongCanvas extends GameCanvas implements Runnable {
private int sleepTime = 30;
private boolean dragging = false;
private Image ballImg;
private Image padImg;
private Sprite ballSprite;
private Sprite padSprite;
private int pointPressY = 0;
private int ballX = getWidth() / 2;
private int ballY = getHeight() / 2;
private int padX = 10;
private int padY = getHeight() / 2;
private int ballDirection = 1;
private final static int ballXVel = 3;
private final static int ballYVel = 1;
private final static int padYVel = 2;
public PongCanvas() {
super(false);
setFullScreenMode(true);
}
public void run() {
while(true) {
updateScreen(getGraphics());
try {
Thread.sleep(sleepTime);
} catch (Exception e) {
}
}
}
public void start() {
try {
ballImg = Image.createImage("/ball.png");
padImg = Image.createImage("/pad.png");
} catch (IOException ioex) {
System.out.println(ioex);
}
ballSprite = new Sprite(ballImg, 3, 3);
ballSprite.defineReferencePixel(2, 2);
ballSprite.setRefPixelPosition(ballX, ballY);
padSprite = new Sprite(padImg, 3, 20);
padSprite.defineReferencePixel(1, 10);
padSprite.setRefPixelPosition(padX, padY);
Thread runner = new Thread(this);
runner.start();
}
private void createBackground(Graphics g) {
g.setColor(0x000000);
g.fillRect(0, 0, getWidth(), getHeight());
}
private void updateScreen(Graphics g) {
createBackground(g);
ballSprite.setRefPixelPosition(ballX, ballY);
ballSprite.paint(g);
padSprite.setRefPixelPosition(padX, padY);
padSprite.paint(g);
moveBall();
movePad();
flushGraphics();
}
private void moveBall() {
if (ballDirection == 0) {
ballX -= ballXVel;
ballY -= ballYVel;
} else if (ballDirection == 1) {
ballX += ballXVel;
ballY -= ballYVel;
} else if (ballDirection == 2) {
ballX += ballXVel;
ballY += ballYVel;
} else if (ballDirection == 3) {
ballX -= ballXVel;
ballY += ballYVel;
}
if (ballDirection == 0 && ballX < 0) {
ballDirection = 1;
} else if (ballDirection == 0 && ballY < 0) {
ballDirection = 3;
} else if (ballDirection == 1 && ballY < 0) {
ballDirection = 2;
} else if (ballDirection == 1 && ballX > getWidth()) {
ballDirection = 0;
if (sleepTime > 5) sleepTime--;
} else if (ballDirection == 2 && ballY > getHeight()) {
ballDirection = 1;
} else if (ballDirection == 2 && ballX > getWidth()) {
ballDirection = 3;
if (sleepTime > 5) sleepTime--;
} else if (ballDirection == 3 && ballY > getHeight()) {
ballDirection = 0;
} else if (ballDirection == 3 && ballX < 0) {
ballDirection = 2;
}
if (ballDirection == 0 && ballSprite.collidesWith(padSprite, false)) {
ballDirection = 1;
} else if (ballDirection == 3 && ballSprite.collidesWith(padSprite, false)) {
ballDirection = 2;
}
}
private void movePad() {
}
public void pointerPressed(int paramInt1, int paramInt2) {
pointPressY = paramInt2;
repaint();
}
public void pointerReleased(int paramInt1, int paramInt2) {
pointPressY = paramInt2;
repaint();
}
public void pointerDragged(int paramInt1, int paramInt2) {
if (pointPressY > padY) {
padY += padYVel;
} else
if (pointPressY < padY) {
padY -= padYVel;
}
repaint();
}
}