Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In the current game I'm building, I'm having trouble moving from one game state to another. I'm using enum to control my states. As of now, this is my enum:

public static enum State
{
    M_MENU, LEVEL_01;
}

public static State state = State.M_MENU;

The corresponding state loads as it should based on which state I change it to manually within the code. The problem I'm having is changing from M_MENU to LEVEL_01 from within the game. I thought that maybe I could set the state from within a mouseListener when the mouse clicks within a specific bound of an image, but the state didn't change. Keep in mind, I only have two states that I'm trying to flip through. Anyone ran into a similar problem as this? I'm just trying to move from the menu to the first level by clicking on the "START" image.

share|improve this question
This may sound silly but have you added a mouseListener to your main class? if so, have you tried a "System.out.println("blah blah"); inside your mouseClicked method? if that doesn't work then try removing everything from the mouseCLicked method and place it in the mouseReleased method or mousePressed... I've found that sometimes the mouseCLicked method doesn't work for me on some things – Savlon Mar 24 at 10:31
@Savlon I've already tried that and checked if I was clicking within boundaries. The mouse registers fine, but dont know why it's not registering the state has changed. Even the enum shows that its value changed, but I'm suspecting it has to do with my thread. – ChocoMan Mar 24 at 10:40
3  
yeh If the enum state is changing then I'm guessing it has something to do with your update method... Can you please post it in your original post – Savlon Mar 24 at 11:16

1 Answer

If your game is based on AWT You probably meet a problem the multithreading. There is a good article about AWT\Multithreding pitfalls in Java: http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/doc-files/AWTThreadIssues.html

Synchronization allows us to ensure that threads see consistent views of memory.Processors can use caches to speed up access to memory (or compilers may store values in registers for faster access). On some multiprocessor architectures, if a memory location is modified in the cache on one processor, it is not necessarily visible to other processors until the writer's cache is flushed and the reader's cache is invalidated. This means that on such systems, it is possible for two threads executing on two different processors to see two different values for the same variable.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.