Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Some Help!
#2
Hey WitherSlayer,

So there are a few problems with your code.. Let's individually analyze each one.

First off, the code snippet
[code2=java]if (rect.intersects(floor))
{
y--;
repaint();
}else{
y++;
repaint();
}[/code2]

has a few things wrong with it. First of all, updating the y variable does not actually alter the rect variable's location. So, instead of calling y++ and y--, you need to call
[code2=java]rect.setLocation(rect.x, rect.y + 1);[/code2]
or
[code2=java]rect.setLocation(rect.x, rect.y - 1);[/code2]

Also, you seem to be confused on how positioning in Applets work. As you move down the screen, y values increase. Alternatively, as you move up the screen, y values decrease. Therefore, if you want the object to move down you much increase the y value instead of decreasing it.

Also, in your code snippet you have the object move back (up?) if the object is touching the ground. Essentially, this creates a bouncing object that I do not think you want. So, a fixed code snippet would look like this:
[code2=java]if (!rect.intersects(floor))
{
rect.setLocation(rect.x, rect.y + 1);
repaint();
}[/code2]


Also, in order to actually get your application to work (ie have it run in realtime) you must implement a loop instead of calling everything within the paint method. Generally, a game loop looks a little something like this:
-Clear Screen
-Do Logic
-Render
-Draw
And it constantly repeats itself. Therefore, you should create a game loop that looks something like this:
[code2=java]public void loop()
{
while (true)
{
if (!rect.intersects(floor))
{
rect.setLocation(rect.x, rect.y + 1);
repaint();
}




}
}[/code2]

Then, you can just call the loop method when you start your application (either in the init() or paint()). Of course, now you should have problems with timing. In order to address this issue, you're going to have to use multithreading which is a rather advanced topic. But for now this should address your issues!
My Blog | My Setup | My Videos | Have a wonderful day.


Messages In This Thread
Need Some Help! - by WitherSlayer - 01-24-2013, 08:43 PM
Re: Need Some Help! - by brandonio21_phpbb3_import2 - 01-27-2013, 11:59 AM
Re: Need Some Help! - by WitherSlayer - 01-27-2013, 03:53 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)