I've decided to try to learn the fundamentals of making a game. So, I started off by making a background and a character that moves. But then I wanted it so that when the character tries to go out of the frame, it's not allowed to. For the movement code, I used a tutorial that I modified to my needs because I didn't even know what classes from the Java library to use. But then I decided to just make my collision detection from scratch. But it's a little bit buggy. It won't detect and stop the character for a couple of milliseconds. Here's my code:

I don't know if this is enough of my code to find the source of the problem but that's why I offered the source code.

08-03-2011, 10:43 PM

Norm

Quote:

It won't detect and stop the character for a couple of milliseconds

What stops the character?
Do you mean the x,y position continues to change and the paint method continues to draw the character after the collision was detected. What is the disconnect between the detection and the stopping?
Is there any relationship between when the user presses a key and when the collision is detected? Can the character continue to move if the key is not pressed?
Without seeing the code, we can only guess why the motion doesn't stop IMMEDIATELY when the flag is set.

08-03-2011, 10:52 PM

iamsothecoolest

The easiest way to see what I'm talking about would be to compile the code in the attachment

08-03-2011, 10:54 PM

Norm

Have you looked at the points I asked about?

08-03-2011, 11:15 PM

Norm

Add some printlns to show the values of x and y as the character moves around. Look at what the values are when it goes out of the frame. Compare those with where the edges of the frame are.

08-03-2011, 11:27 PM

iamsothecoolest

Oh, I actually already had the showing coordinates idea implemented :(giggle):. But the coordinates where it goes out of the frame varies each time with the furthest being around 100 pixels out of the frame.
EDIT: Like I said, the easiest way to see would be to compile the program in the .zip file I attached.

08-03-2011, 11:32 PM

Norm

You need to do some more debugging:
Add some printlns to your code to show the x,y values of the character each time it is drawn. Add printlns where the x,y position of the character changes. Add printlns where the collision is detected. See if it continues making changes to the x,y after the collision is detected.

08-03-2011, 11:45 PM

Iron Lion

Unrelated to the question, but this is redundant:

Quote:

Code:

if(getCharY() != 0 && getCharY() > 0) {

If the second condition is true, the first is going to be true as well, so there's no need to test for both.

08-03-2011, 11:47 PM

iamsothecoolest

Quote:

Originally Posted by Iron Lion

Unrelated to the question, but this is redundant:

If the second condition is true, the first is going to be true as well, so there's no need to test for both.

That was just me being a stupid experimenter. :(blush):

EDIT: Got rid of the first condition of the if statement. That was the one that was unecessary.