Need Some Help! - Printable Version +- BP Forums (https://bpforums.info) +-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55) +--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56) +---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19) +---- Thread: Need Some Help! (/showthread.php?tid=739) |
Need Some Help! - WitherSlayer - 01-24-2013 I would like to create a basic game that is kind of like the mario flash games. I'm still testing on some of the concepts, so I created this testing class. I'm trying to get a gravity affect by making the y coodinate lower until it hits the floor using an ".intersects" for the collision. But the box(the box is my test charater) thing just floats in the air and the repaint() command just makes it flash ut it does not move down. I also tried to make the box move using a key listener, but I don't think it even makes it up to that line of code. Here's the code so far: [code2=java]import java.applet.Applet; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class rectPlayer extends Applet implements KeyListener{ private static final long serialVersionUID = 1L; int x = 10; int y = 10; Rectangle rect; Rectangle floor; public void init() { this.addKeyListener(this); rect = new Rectangle (x,y,50,50); floor = new Rectangle(0,200,500,10); } public void paint(Graphics g) { setSize(new Dimension(500,300)); Graphics2D g2 = (Graphics2D)g; g2.draw(rect); g2.draw(floor); if (rect.intersects(floor)) { y--; repaint(); }else{ y++; repaint(); } } @SuppressWarnings("unused") @Override public void keyPressed(KeyEvent e) { if (KeyEvent.KEY_PRESSED == KeyEvent.VK_RIGHT) { x++; x++; } else if (KeyEvent.KEY_PRESSED == KeyEvent.VK_LEFT){ x--; x--; } else if (KeyEvent.KEY_PRESSED == KeyEvent.VK_UP){ y--; y--; } else if (KeyEvent.KEY_PRESSED == KeyEvent.VK_DOWN){ y++; y++; } } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } }[/code2] Re: Need Some Help! - brandonio21 - 01-27-2013 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! Re: Need Some Help! - WitherSlayer - 01-27-2013 Thanks a bunch! When i wrote that code I did actually want it to be boucing but then I posted it before changing it back to just falling. <!-- s:o --><img src="{SMILIES_PATH}/icon_e_surprised.gif" alt=":o" title="Surprised" /><!-- s:o --> Can you just put the key listener into a loop? Anyways thanks You're awesome! <!-- s --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="" title="Very Happy" /><!-- s --> Re: Need Some Help! - brandonio21 - 01-27-2013 Well, putting a KeyListener into a game with a loop may prove to be a bit difficult. However, I think you can figure it out through experimentation. It's most fun that way, anyway. And no problem! I'm glad that I could help! |