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:
Second class: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(); } }
Third class: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");} } }
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.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");} } }