Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 37 38 [39] 40 41 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100895 times)

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #570 on: March 11, 2012, 12:10:22 am »

Okay, I'm running into another bug: the arrow keys aren't working. When the window is just sitting there, on the menu screen, they work fine. But when I click play and the run() method is called, the arrow keys don't work.

Thank you for being so helpful. I must have made about 50 posts in this thread.

EDIT: this is in Java, in case you're wondering.
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games

eerr

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #571 on: March 11, 2012, 07:06:05 am »

Okay, I'm running into another bug: the arrow keys aren't working. When the window is just sitting there, on the menu screen, they work fine. But when I click play and the run() method is called, the arrow keys don't work.

Thank you for being so helpful. I must have made about 50 posts in this thread.

EDIT: this is in Java, in case you're wondering.

I could think of a few things.

I think, perhaps, you lost focus on the interface object?

Perpetual loops blocking i/o,
but they wouldn't be outright blocking with java usually, unless the keylistener is both not in it's own thread and in the same thread as the run() method. And probably other stuff too.
Logged

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #572 on: March 11, 2012, 01:41:54 pm »

Hmmm... It might be in the same thread as run(). The relevant part of the code is:

I'm sorry I didn't condense it more for you, but it already is pretty condensed from over a 1000 lines of code.

Code: [Select]
public class GameWindow extends JPanel implements Runnable{
       
        JFrame window;
        KeyHandler keyHandler;
       
        public GameWindow(){
                window = new JFrame();
                keyHandler = new KeyHandler();
window.addKeyListener(keyHandler);
window.setFocusable(true);
window.requestFocus();
                //Then I make and add all the buttons, including the play button
        }

        public void newRun(){
                // does some stuff you don't care about like getting rid of buttons
                run()
        }

public void run(){

int period = 20;
long beforeTime, timeDiff, sleepTime;

beforeTime = System.currentTimeMillis();

running = true;


while (running){

gameUpdate();
gameRender();
paintScreen();

if (leftPressed){
leftArrowKeyPressed();
}

if (rightPressed){
rightArrowKeyPressed();
}

if (readyToClose){
gameOver();
}

timeDiff = System.currentTimeMillis() - beforeTime;
sleepTime = period - timeDiff;

if (sleepTime <= 0){
sleepTime = 5;
}

try{
Thread.sleep(sleepTime);
}catch (InterruptedException ex){}

beforeTime = System.currentTimeMillis();


if (gameIsOver){
gameOver();
}

}

} //End of run()


public class KeyHandler extends KeyAdapter{

public void keyTyped(KeyEvent e){
int keyCode = e.getKeyCode();
if((keyCode == KeyEvent.VK_ESCAPE) || (keyCode == KeyEvent.VK_Q) ||
(keyCode == KeyEvent.VK_END) ){

readyToClose = true;
}
}

public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();

if (keyCode == KeyEvent.VK_LEFT){
leftPressed = true;
System.out.println("left");
}
else if (keyCode == KeyEvent.VK_RIGHT){
rightPressed = true;
System.out.println("Right");
}
}

public void keyReleased(KeyEvent e){
int keyCode = e.getKeyCode();

if (keyCode == KeyEvent.VK_LEFT){
leftPressed = false;
//System.out.println("left");
}
else if (keyCode == KeyEvent.VK_RIGHT){
rightPressed = false;
//System.out.println("Right");
}
}


}

public class PlayListener implements ActionListener{ //This just starts the game after you click play
public void actionPerformed(ActionEvent event){
//notPaused = true;
if (!clicked){
//System.out.println(" New game ");
newRun();
clicked = true;
} else {
clicked = false;
}
}
}

       
}
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games

eerr

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #573 on: March 11, 2012, 09:08:55 pm »

The Button has focus, not the window.

Stuff like JTextbox Needs focus to receive keypresses.

So like, when it has the focus, the keypresses only go to that textbox, and stuff.
And therefore to none of the other Textboxes.

Focus is only held by a single Java Object, and it's not always the Window.

I can think of a couple ways to fix this, but most thorough is to do something with each new interface object. Either also pass it the Keylistener, make all of the buttons non-focusable, or have some sort of object that sits on top intercepting everything.
Logged

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #574 on: March 11, 2012, 10:00:13 pm »

I can think of a couple ways to fix this, but most thorough is to do something with each new interface object. Either also pass it the Keylistener, make all of the buttons non-focusable, or have some sort of object that sits on top intercepting everything.

Well, after screwing around for a while, I have found that I have absolutely no idea what you mean. Could you explain please?
« Last Edit: March 11, 2012, 10:05:00 pm by Kofthefens »
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games

eerr

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #575 on: March 11, 2012, 11:17:46 pm »

I can think of a couple ways to fix this, but most thorough is to do something with each new interface object. Either also pass it the Keylistener, make all of the buttons non-focusable, or have some sort of object that sits on top intercepting everything.

Well, after screwing around for a while, I have found that I have absolutely no idea what you mean. Could you explain please?
When you click the button, it takes the focus from the window.

It's also possibly to tab to the button accidentally? maybe?

When it takes focus, it also takes the keypresses.

And Eats them.
« Last Edit: March 11, 2012, 11:27:38 pm by eerr »
Logged

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #576 on: March 11, 2012, 11:27:38 pm »

That does make sense, but I didn't understand how to fix it. Sorry I was unclear.
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games

eerr

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #577 on: March 11, 2012, 11:33:35 pm »

That does make sense, but I didn't understand how to fix it. Sorry I was unclear.

How you do this effects what bugs you get in the future- and weather or not you know how to fix them.

Just Set
button.focusable= False and window.isfocused=true
Logged

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #578 on: March 11, 2012, 11:42:25 pm »

The bug remains. I just set all the buttons I have to setFocusable(false), and the window has

Code: [Select]
keyHandler = new KeyHandler();
window.addKeyListener(keyHandler);
window.setFocusable(true);
window.requestFocus();

but it still doesn't work. Thanks for your help, though.
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games

eerr

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #579 on: March 12, 2012, 09:37:23 pm »

Spoiler (click to show/hide)
Any call to run() should create a new, seperate thread.
If you want to call the function without a new thread make a seperate function, and call that one more than once from the original run()

Doh-
You need to pass game window a new keyhandler.

Other than that- not enough information.
« Last Edit: March 12, 2012, 09:51:00 pm by eerr »
Logged

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #580 on: March 12, 2012, 09:51:27 pm »

Are you sure you meant

Code: [Select]
        public GameWindow(){
                window = new JFrame();
                keyHandler = new KeyHandler();
window.addKeyListener(keyHandler);
window.setFocusable(true);
window.requestFocus();
                //Then I make and add all the buttons, including the play button
        }
And not:
Code: [Select]
        public GameWindow(){
                window = new JFrame();
                keyHandler = new KeyHandler();
window.addKeyListener(keyHandler);
window.setFocusable(true);
window.requestFocus();
window.add(this);
                //Then I make and add all the buttons, including the play button
        }
this? You never actually add a GameWindow to the window frame that you've created.

As an aside, this is not an idiomatic way of using swing. What you should be doing is when the GameWindow (which extends JPanel - so it's not actually a window; windows are Frames, not Panels) is created, assign buttons to the GameWindow (i.e. this.add(myButton)), then in your main method create a JFrame and add the GameWindow to the newly created frame (i.e. GameWindow myGW = new GameWindow(); JFrame myFrame = new JFrame(); myFrame.add(myGW)). This is since each interface object in swing should generally only deal with its child elements (i.e. a panel shouldn't be creating its parent frame).
Logged

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #581 on: March 12, 2012, 10:20:25 pm »

Are you sure you meant
this? You never actually add a GameWindow to the window frame that you've created.

I have condensed a huge amount of the code; the GameWindow is added after you click play. I posted the full amount of the code a few pages back, though I have changed it around a fair bit since then. If you really want the most recent version, let me know and I would be happy to post it...

Here's the old version:
Spoiler (click to show/hide)


Thank you for the help. Sorry, looking at just what you quoted, my code must seem ridiculous.
« Last Edit: March 12, 2012, 10:28:36 pm by Kofthefens »
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #582 on: March 12, 2012, 10:29:50 pm »

Sorry for the double post, but when I tried to add the new most recent version of the GameWindow class, it told me I exceeded the character limit. So:

Here is the most recent version of the gameWindow class. (It won't work with the old other classes, but it is the real brains of the program)

Spoiler (click to show/hide)
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #583 on: March 13, 2012, 01:42:20 pm »

I have a feeling it's because you didn't declare leftPressed and rightPressed as volatile.

You also are using Thread wrong; a thread should either be constructed with a Runnable object (i.e. Thread myThread = new Thread(myRunnable)) or should have an overloaded run() function. Call myThread.start() to start the thread. As a rule, constructors should never be used to run anything but initialization code.
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #584 on: March 13, 2012, 03:34:12 pm »

Sorry for the double post, but when I tried to add the new most recent version of the GameWindow class, it told me I exceeded the character limit. So:

Here is the most recent version of the gameWindow class. (It won't work with the old other classes, but it is the real brains of the program

Do you have every single class in one class? for real?

*meh nvm.

But I did manage to remove stuff untill nothing was left, and then input/output worked.

Or not. Implied work is implied. I can give it to you stripped down so far that the i/o works, but not much is left after my Rampage.
« Last Edit: March 13, 2012, 04:05:59 pm by eerr »
Logged
Pages: 1 ... 37 38 [39] 40 41 ... 91