Jump to content

Help with controlling multiple Objects

Tech N Gamer
Go to solution Solved by Tech N Gamer,

I figured it out, it's completely skipping the loop for some reason. I found this out after I took out the while and edited the else if to be more proper.

I need help, I am using the Keyboard Class from Lightweight Java Game Library and it won't change anything. It goes in a while loop but nothing happens, even when a key is pressed. Any help will be appreciated.

Brah, do you even Java?

Link to comment
Share on other sites

Link to post
Share on other sites

Include the part of the code that isn't working and I'll give it a look

Link to comment
Share on other sites

Link to post
Share on other sites

private void keyChange() {
	        while(Keyboard.next()) {
	            System.out.println("in loop");
	            if(Keyboard.getEventKey() == Keyboard.KEY_Q && Keyboard.getEventKeyState()){
	                System.out.println("Q Key");
	                Shadow.changeDir(true);
	                break;
	            } else if(Keyboard.getEventKey() == Keyboard.KEY_W && Keyboard.getEventKeyState()){
	                System.out.println("W Key");
	                Shadow.changeDir(false);
	                break;
	            } else if(Keyboard.getEventKey() == Keyboard.KEY_X && Keyboard.getEventKeyState()){
	                System.out.println("up");
	                Speedy.changeDir(true);
	            } else if(Keyboard.getEventKey() == Keyboard.KEY_C && Keyboard.getEventKeyState()){
	                System.out.println("down");
	                Speedy.changeDir(false);
	            } else if(Keyboard.getEventKey() == Keyboard.KEY_T && Keyboard.getEventKeyState()){
	                System.out.println("down");
	                Inky.changeDir(true);
	            } else if(Keyboard.getEventKey() == Keyboard.KEY_Y && Keyboard.getEventKeyState()){
	                System.out.println("down");
	                Inky.changeDir(false);
	            } else if(Keyboard.getEventKey() == Keyboard.KEY_M && Keyboard.getEventKeyState()){
	                System.out.println("down");
	                Pokey.changeDir(true);
	            } else if(Keyboard.getEventKey() == Keyboard.KEY_COMMA && Keyboard.getEventKeyState()){
	                System.out.println("down");
	                Pokey.changeDir(false);
	            } else if(Keyboard.getEventKey() == Keyboard.KEY_RIGHT && Keyboard.getEventKeyState()){
	                System.out.println("right");
	                PacMan.changeDir(0);
	                break;
	            } else if(Keyboard.getEventKey() == Keyboard.KEY_LEFT && Keyboard.getEventKeyState()){
	                System.out.println("left");
	                PacMan.changeDir(2);
	                break;
	            } else if(Keyboard.getEventKey() == Keyboard.KEY_UP && Keyboard.getEventKeyState()){
	                System.out.println("up");
	                PacMan.changeDir(1);
	                break;
	            } else if(Keyboard.getEventKey() == Keyboard.KEY_DOWN && Keyboard.getEventKeyState()){
	                System.out.println("down");
	                PacMan.changeDir(3);
	                break;
	            }
	        }
	    }

Brah, do you even Java?

Link to comment
Share on other sites

Link to post
Share on other sites

There are a lot of classes at play here but this method is what detects the keys and pushes them to the proper class.

Brah, do you even Java?

Link to comment
Share on other sites

Link to post
Share on other sites

When you say it nothing happens, does that mean if you press the "Q" key it prints

in loop

or 

in loop
Q Key

but has no effect

or does it print nothing?

 

If it prints nothing are you calling keyChange inside your game loop?

 

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, fizzlesticks said:

When you say it does nothing, does that mean if you press the "Q" key it prints


in loop

or 


in loop
Q Key

but has no effect

or does it print nothing?

 

If it prints nothing are you calling keyChange inside your game loop?

 

What it does is it will print nothing and do nothing. Also, KeyChange is not called at all.

Brah, do you even Java?

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Tech N Gamer said:

What it does is it will print nothing and do nothing. Also, KeyChange is not called at all.

Add a call to keyChange inside your game loop, it's not going to call itself.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Tech N Gamer said:

What it does is it will print nothing and do nothing. Also, KeyChange is not called at all.

Well you're going to need to call keyChange somewhere in your program

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, fizzlesticks said:

Add a call to keyChange inside your game loop, it's not going to call itself.

 

Just now, prolemur said:

Well you're going to need to call keyChange somewhere in your program

Sorry, I forgot it's called KeyChange() and it's in the main loop.

Brah, do you even Java?

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Tech N Gamer said:

 

Sorry, I forgot it's called KeyChange() and it's in the main loop.

So you have a call to the function you posted inside the main loop but it's never being called? Is your code even getting to the main loop?

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Yea, the main loop posts to the Eclipse Console every 15fps.

Brah, do you even Java?

Link to comment
Share on other sites

Link to post
Share on other sites

I figured it out, it's completely skipping the loop for some reason. I found this out after I took out the while and edited the else if to be more proper.

Brah, do you even Java?

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Tech N Gamer said:

Yea, the main loop posts to the Eclipse Console every 15fps.

Could you post the code for your main loop?

Did you accidentally make 2 functions called KeyChange, 1 with a capital 'K' and 1 with a lowercase and you're looking at the wrong one? If everything you've said so far was true it would mean there's a bug in Java for it to not be calling your function.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×