Java problem. Thread wont stop(FIXED)
Hello again guys!(I wrote another post yesterday)
I have encountered another problem. It is probably another one of these things were i forget to put some code in and it doesn´t work. Well the problem is that the threads im using wont stop when i want them to. I have 2 threads wich each contains a while loop that makes the squares in my little game move. I have made 2 booleans and put them to true. So the while loop looks like this: while(isGoing){}, so i have made a method called collision and have the intersects method in an if statement for the 2 squares. So if they intersect i basically want to switch the booleans to false and stop the whole thing from moving. But i doesn´t work. This post probably looks a bit messy. But i will post the code and go to bed because im pretty tired. The code might be a bit messy/bad because i dont have much experience with games. But anyways here is the code:
First class:
Code Java:
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame{
Image dbImage;
Graphics dbg;
Player hero;
AI bot;
public Main(){
bot = new AI();
hero = new Player();
addKeyListener(hero);
setVisible(true);
setSize(500,500);
setResizable(false);
setDefaultCloseOperation(3);
setLocation(500,250);
}
public void draw(Graphics g){
g.setColor(Color.GRAY);
g.fillRect(0,0,500,500);
bot.draw(g);
hero.draw(g);
}
public void paint(Graphics g){
dbImage = createImage(getWidth(),getHeight());
dbg = dbImage.getGraphics();
draw(dbg);
g.drawImage(dbImage,0,0,this);
repaint();
}
public static void main(String[] args) {
new Main();
}
}
Second class:
Code Java:
import java.awt.*;
import java.util.Random;
public class AI implements Runnable{
Player player;
Rectangle enemy;
int x = 250,y = 250;
int randXDir;
int randYDir;
boolean isGoing = true;
Random r;
Thread t1;
public AI(){
r = new Random();
t1 = new Thread(this);
player = new Player();
enemy = new Rectangle(x,y,15,15);
x = enemy.x;
y = enemy.y;
setRandomXDir();
setRandomYDir();
t1.start();
}
public void setRandomXDir(){
randXDir = 0;
randXDir = r.nextInt(485);
}
public void setRandomYDir(){
randYDir = 0;
randYDir = r.nextInt(485);
}
public void collision(){
if(enemy.intersects(player.hero)){
isGoing = false;
player.isGoing = false;
}
}
public void draw(Graphics g){
g.setColor(Color.GREEN);
g.fillRect(x, y, enemy.width, enemy.height);
g.drawString("X: " + x + "Y: " + y, 25,50);
}
public void setDirection(){
if(x > randXDir){
x--;
}
if(x < randXDir){
x++;
}
if(y > randYDir){
y--;
}
if(y < randYDir){
y++;
}
if(x == randXDir && y == randYDir){
setRandomXDir();
setRandomYDir();
}
}
public void move(){
collision();
setDirection();
}
public void run(){
try{
while(isGoing){
move();
Thread.sleep(5);
}
}catch(Exception e){System.out.println("error");}
}
}
Third class:
Code Java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Player implements KeyListener, Runnable{
Rectangle hero;
int heroX = 100,heroY = 100;
int xDir, yDir;
boolean isGoing = true;
Thread t1;
public Player(){
hero = new Rectangle(heroX,heroY,30,30);
t1 = new Thread(this);
t1.start();
}
public void draw(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(heroX, heroY,hero.width ,hero.height);
collision();
}
public void collision(){
if(heroX >= 470)
heroX = 470;
if(heroX <= 0)
heroX = 0;
if(heroY >= 470)
heroY = 470;
if(heroY <= 20)
heroY = 20;
}
public void setXDirection(int xDir){
this.xDir = xDir;
}
public void setYDirection(int yDir){
this.yDir = yDir;
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_UP){
setYDirection(-1);
}
if(keyCode == KeyEvent.VK_DOWN){
setYDirection(1);
}
if(keyCode == KeyEvent.VK_RIGHT){
setXDirection(1);
}
if(keyCode == KeyEvent.VK_LEFT){
setXDirection(-1);
}
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_UP){
setYDirection(0);
}
if(keyCode == KeyEvent.VK_DOWN){
setYDirection(0);
}
if(keyCode == KeyEvent.VK_RIGHT){
setXDirection(0);
}
if(keyCode == KeyEvent.VK_LEFT){
setXDirection(0);
}
}
public void keyTyped(KeyEvent arg0) {}
public void move(){
heroX += xDir;
heroY += yDir;
}
public void run() {
try{
while(isGoing){
move();
Thread.sleep(3);
}
}catch(Exception e){System.out.println("Error");}
}
}
I have also tried switching things around so some things might be even more weird but i dont know im pretty tired so i cant reply until tomorrow.
Re: Java problem. Thread wont stop
Try debugging the code by adding some println statements to print out the values of variables used to control the logic. For example in collision() print out the two rectangles that are being tested for intersection.
Re: Java problem. Thread wont stop
Well i haven´t fixed the problem. But it seems if(enemy.intersects(player.hero)) is never true...
--- Update ---
This is so annoying. I cant find the problem! Why doesn´t the intersecs work!?
--- Update ---
Can someone look at my intersecs method in collision? The if statement doesn´t get executed and i dont know why
--- Update ---
Sigh
Re: Java problem. Thread wont stop
Did you do what I suggested in my post? What was printed? The output will help you find what the problem is.
Re: Java problem. Thread wont stop
Well i put a println in the following way:
Code Java:
public void collision(){
System.out.println("COLLISION");
if(enemy.intersects(player.hero)){
System.out.println("FALSE");
isGoing = false;
player.isGoing = false;
}
}
And when i did that it only printed "COLLISION", so i know that the if statement is never executed. But i dont know how to fix it...
Re: Java problem. Thread wont stop
Quote:
know how to fix it...
Reread post #2 about what to print out. What you are printing does not show any variable's value. You need to see the values to know what the code is doing so you can work on fixing it.
Re: Java problem. Thread wont stop
I dont know what the problem is and i will probably just give up.
Re: Java problem. Thread wont stop
Quote:
I dont know what the problem is
You will never find the problem if you don't debug the code.
What was printed out when you added the println statements I suggested? Did you look at the printout to see if it made sense?
Re: Java problem. Thread wont stop
Im not even sure what im supposed to put and where. I have never debugged with println before and i have no idea what variables i should print and where even after i red your first post.
Re: Java problem. Thread wont stop
Quote:
what variables i should print
Here is what I suggested:
For example in collision() print out the two rectangles that are being tested for intersection
Code :
if(enemy.intersects(player.hero)){
enemy
player.hero
Print them before the if statement
Re: Java problem. Thread wont stop
Do you mean like the x and y coordinates or?
Re: Java problem. Thread wont stop
The Rectangle class has a toString() method that will return the values of all 4 variables. Just use the name of the variable in the println() statement.
Re: Java problem. Thread wont stop
Oh okay i will try that in a while
--- Update ---
When i do that it only displays the same coordinates all the time. Its like the rectangles i drew and created as objects arent the same. Im not experienced with making games and this is just for fun and see if i can make just a little thing like this. So is it like that? They arent the same?
Re: Java problem. Thread wont stop
Quote:
displays the same coordinates all the time
That would mean that the code is not changing the coordinates of those rectangles. Check the code to see why it doesn't change the rectangle's contents.
What values does the code use to draw the objects at changing locations?
Are those values in the rectangle objects?
Re: Java problem. Thread wont stop
This is some code i have for the rectangle:
Code Java:
Rectangle hero;
int heroX,heroY;
int xDir, yDir;
int width,height;
heroX = 100;
heroY = 100;
width = 30;
height = 30;
hero = new Rectangle(heroX,heroY,width,height);
g.fillRect(heroX,heroY,width,height);
public void collision(){
if(heroX >= 470)
heroX = 470;
if(heroX <= 0)
heroX = 0;
if(heroY >= 470)
heroY = 470;
if(heroY <= 20)
heroY = 20;
}
There are some parts of the code that might help. They are from different methods and I only included one full method wich is the collision detection for the sides of the screen wich works fine. This is only for 1 of the 2 rectangles but they are built in the same way.
Re: Java problem. Thread wont stop
Where does the code change the values of the hero retangle?
The separate variable: heroX has nothing to do with the rectangle: hero that is used in the collision() method where you printed out the rectangle's values.
You should be using the x and y variables in the Rectangle class, not separate variables.
Re: Java problem. Thread wont stop
Well i have changed everything with seperate variables to for example hero.x. Here is the code that makes the Player rectangle move:
Code Java:
public void setXDirection(int xDir){
this.xDir = xDir;
}
public void setYDirection(int yDir){
this.yDir = yDir;
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_UP){
setYDirection(-1);
}
if(keyCode == KeyEvent.VK_DOWN){
setYDirection(1);
}
if(keyCode == KeyEvent.VK_RIGHT){
setXDirection(1);
}
if(keyCode == KeyEvent.VK_LEFT){
setXDirection(-1);
}
}
public void move(){
hero.x += xDir;
hero.y += yDir;
}
I also have a keyReleased that just sets xDir and yDir to 0.
Re: Java problem. Thread wont stop
Does the collision test work now?
Re: Java problem. Thread wont stop
Not yet but its getting there i think.
Re: Java problem. Thread wont stop
Re: Java problem. Thread wont stop
Re: Java problem. Thread wont stop
Fixed. Im sorry for the long post and thank you for answering