-2
package com.Multiply.com;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class Core extends JPanel {
public static void main(String[] args){
    Core game = new Core();

    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setTitle("Multiply");
    game.frame.setIconImage(testIcon);
    game.frame.add(game);
    game.frame.pack();
    game.frame.setLocationRelativeTo(null);
    game.frame.setResizable(false);
    game.frame.setVisible(true);
    t1.start();

    running = true;
    Collision.charCollision();

}

//start of variables
private static final long serialVersionUID = 1L;
public static int width = 1280; //width and height of the screen
public static int height = 720;

public static boolean running = false;

public static int charX = width /2 - 50;
public static int charY = height /2 - 50;
public static int charSizeX = 50;
public static int charSizeY = 50;
public static double charSpeed = 10;

public static Image testIcon = Toolkit.getDefaultToolkit().getImage("Multiply_Icon.png"); //Icon variables, locates the icon in the folder

JFrame frame = new JFrame();    //the frame variable

static Thread t1 = new Thread();

public Core(){
    KeyListener listener = new MyKeyListener();
    addKeyListener(listener);
    setFocusable(true);

    Dimension size = new Dimension(width, height);  //variable that sets the size of the window to 1280x720p
    setPreferredSize(size);                         //Sets the size of the window to "size" 

    frame = new JFrame();                           //makes frame into the JFrame window
}

public class MyKeyListener implements KeyListener {
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        //System.out.println("keyPressed="+KeyEvent.getKeyText(e.getKeyCode()));
        if(keyCode == KeyEvent.VK_W){
            charY-= charSpeed;
            repaint();
        }
        else if(keyCode == KeyEvent.VK_A){
            charX-= charSpeed;;
            repaint();
        }
        else if(keyCode == KeyEvent.VK_S){
            charY+= charSpeed;
            repaint();
        }
        else if(keyCode == KeyEvent.VK_D){
            charX+= charSpeed;
            repaint();
        }   
    }
    @Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        //System.out.println("keyReleased="+KeyEvent.getKeyText(e.getKeyCode()));
        if(keyCode == KeyEvent.VK_ESCAPE){
            ConfigFileWriter cfw = new ConfigFileWriter();
            if(cfw.wroteFile == false){
                cfw.openFile();
                cfw.writeFile();
                cfw.closeFile();
                cfw.wroteFile = true;
                System.out.println("wroteFile = true");
                }
            running = false;
            System.out.println("Escape");
            try {
                t1.join();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            System.exit(0);
        }
        else if(keyCode == KeyEvent.VK_OPEN_BRACKET){   //To be removed, testing purposes
            charSpeed-= 1;
        }
        else if(keyCode == KeyEvent.VK_CLOSE_BRACKET){
            charSpeed+= 1;
        }
        else if(keyCode == KeyEvent.VK_L){
            ConfigFileReader cfr = new ConfigFileReader();

            cfr.openFile();
            cfr.readFile();
            cfr.closeFile();
            charSpeed = Double.parseDouble(cfr.speed);
        }
    }
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }
}

public void paint(Graphics g) {     // Draws top first to bottom last 
    Graphics2D g2 = (Graphics2D) g;

    Image background = Toolkit.getDefaultToolkit().getImage("C:/Users/Kanin/Desktop/Multiply_Game_Engine/Images/Black_Background.png");
    Image character = Toolkit.getDefaultToolkit().getImage("C:/Users/Kanin/Desktop/Multiply_Game_Engine/Images/Character.png"); // C:/Users/Kanin/Desktop/Multiply_Game_Engine/ 
    g2.drawImage(background, 0, 0, this); //draws the black background
    g2.drawImage(character, charX, charY, charSizeX, charSizeY, this);
}

}

package com.Multiply.com;

public class Collision extends Core {
private static final long serialVersionUID = 1L;
public static void charCollision(){
    while(running){
        System.out.println("Running");  //Won't detect unless this is here for some reason.
        if(charX < -1){         //Character can't go past the left side of the screen.
            charX = 0;
        }
        else if(charX > 1240){  //Character can't go past the right side of the screen.
            charX = 1240;
        }
        else if(charY < -1){    //Character can't go above the top side of the screen.
            charY = 0;
        }
        else if(charY > 680){   //Character can't go below the bottom side of the screen.
            charY = 680;
        }
    }
}
}

Near the very top it says System.out.println("Running"); if i don't have that line it doesn't register that my block is going out of the boundaries and it just keeps going. When i run the game, if i remove the system.out block of code it doesn't even run anything in the if statements

CrazyKane
  • 3
  • 3
  • Ummm... won't detect _what_? _What_ just keeps going? Where do you declare those variables you're working with? There seems to be lots of information missing. – ajb Apr 04 '15 at 06:05
  • 2
    Can you share the entire code. The snippet does not depict what is the value for "running " in while(running) – Saurabh Jhunjhunwala Apr 04 '15 at 06:06

1 Answers1

1

My guess would be that the KeyListeners you registered are executed in a different thread than the main thread (one of the threads spawned by the GUI framework), but the Collision.charCollision method is run in the main thread. When leaving out the System.out.println statement, the variables in the method charCollision are assumed to be self-contained by the VM, but as soon as you insert the interaction with the console (and thus interaction with other threads) the variable running is kept in sync with the GUI threads. You can try to declare all variables used in the GUI as well as in your main thread as volatile and see if that already fixes your problem. See Loop doesn't see changed value without a print statement for further information about multi-threading.

Community
  • 1
  • 1
muued
  • 1,666
  • 13
  • 25