Having problems with this code.

Hi. I just started programming games a while ago and I'm having problems with this code. What I'm trying to do is move a ball across the screen. I got it so it does that. But, I wanted the ball to reset itself when it reaches the edge of the applet. The main problem is that when the ball reaches the edge of the applet the ball doesn't reset itself it just freezes where it's at. Also, I tried to use a random number generator to determine whether the ball moves to the left or right, but the ball only moves to the right. This would mean that it's a positive number and the ball never moves to the left. It should move to both the left and the right. Anyway here's the source below for the main class and the ball class.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class Animation extends Applet implements Runnable, MouseListener {

int x;

int y;

Ball ball;

Thread main;

private Image db;

private Graphics dbg;

Cursor c;

public void init() {

setBackground(Color.black);

ball = new Ball(150,150,10,Color.red,1,3);

c = new Cursor(Cursor.CROSSHAIR_CURSOR);

this.setCursor(c);

addMouseListener(this);

}

public void start() {

main = new Thread(this);

main.start();

}

public void stop() {

main.stop();

}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mousePressed(MouseEvent e) {

x = e.getX();

y = e.getY();

if(ball.userHit(x,y)) {

ball.ballWasHit();

}

}

public void mouseReleased(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

public void run() {

Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

while(true) {

try { main.sleep(40); }

catch(InterruptedException e) {}

ball.move();

repaint();

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

}

}

public void paint(Graphics g) {

ball.DrawBall(g);

}

public void update(Graphics g) {

if(db==null) {

db = createImage(this.getSize().width,this.getSize().height);

dbg = db.getGraphics();

}

dbg.setColor(getBackground());

dbg.fillRect(0,0,this.getSize().width,this.getSize().height);

dbg.setColor(getForeground());

paint(dbg);

g.drawImage(db,0,0,this);

}

}

import java.awt.*;

import java.awt.event.*;

import java.util.*;

public class Ball {

private int xpos;

private int ypos;

private int xspeed;

private int radius;

private int maxspeed;

private int first_x = 150;

private int first_y = 150;

private final int x_leftout = 10;

private final int x_rightout = 290;

Color color;

Random rnd = new Random();

public Ball(int x,int y,int radius, Color color,int xspeed,int maxspeed) {

xpos = x;

ypos = y;

this.radius = radius;

this.color = color;

this.xspeed = xspeed;

maxspeed = this.maxspeed;

}

public void move() {

xpos += xspeed;

isOut();

}

public void ballWasHit() {

xpos = first_x;

ypos = first_y;

xspeed = (rnd.nextInt())%maxspeed;

}

public boolean userHit(int maus_x,int maus_y) {

double x = maus_x - xpos;

double y = maus_y - ypos;

double distance = Math.sqrt((x*x) + (y*y));

if(distance < 15) {

return true;

}

else return false;

}

public boolean isOut() {

if(xpos < x_leftout) {

xpos = first_x;

ypos = first_y;

xspeed = (rnd.nextInt())%maxspeed;

return true;

}

if(xpos > x_rightout) {

xpos = first_x;

ypos = first_y;

xspeed = (rnd.nextInt())%maxspeed;

return true;

}

else return false;

}

public void DrawBall(Graphics g) {

g.setColor(color);

g.fillOval(xpos - radius, ypos - radius,2 * radius,2 * radius);

}

}

[4105 byte] By [drtbker2001a] at [2007-9-28 1:31:02]
# 1

Hi,

I didnt try the code itself, just a quick go though....

I guess you need to stick your (BallIsOut?) function inside the run loop..so loop is checking if the ball is out of bound (which is what your function doing but cancel few repeated code) in each cycle.

public boolean isOut() {

if(xpos < x_leftout || xpos > x_rightout) {

return true;

else return false;

}

public void run() {

Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

while(true) {

try { main.sleep(40); }

catch(InterruptedException e) {}

ball.move();

if(ball.isOut())

ball.ballWasHit();

repaint();

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

}

Hope this work!

Regards

John

jGizmoa at 2007-7-7 21:05:01 > top of Java-index,Other Topics,Java Game Development...
# 2

public Ball(int x,int y,int radius, Color color,int xspeed,int maxspeed) {

xpos = x;

ypos = y;

this.radius = radius;

this.color = color;

this.xspeed = xspeed;

maxspeed = this.maxspeed; // here is an error

}

myavuzselima at 2007-7-7 21:05:01 > top of Java-index,Other Topics,Java Game Development...