Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default Move and pause method

    I've just been messing around with what I've been learning and I'm a little stuck. Basically, I just wanted to create a little "ship" which shoots "bullets" when the mouse is clicked. The idea is that the y coordinate of the bullet will decrease by 1 until the bullet goes to the top of the canvas. Sorry about the bad variable names and lack of comments, it was only meant to be a quick experiment. I wonder whether it's to do with the pause method or the move method not doing what I think it's meant to do; perhaps someone who knows a bit about the ACM library can help? Thanks!

    Java Code:
    import acm.program.*;
    import acm.graphics.*;
    import java.awt.event.*;
    
    public class Game extends GraphicsProgram{
    
    	private GRect ship;
    	private GRect bullet;
    	private int shipSize = 25;
    
    
    	public void run() {
    
    		setup();
    		addMouseListeners();
    
    	}
    
    	public void setup() {
    
    		ship = new GRect( getWidth() / 2 - shipSize / 2, getHeight() - shipSize, shipSize, shipSize);
    		add(ship);
    
    	}
    
    	public void mouseClicked (MouseEvent e) {
    
    		shoot();
    
    	}
    
    	public void shoot() {
    
    		double x = ship.getX() + shipSize / 2 - 1.5;
    		double y = ship.getY() - 3;
    
    		bullet = new GRect (x, y, 3, 3);
    
    		bullet.setFilled(true);
    
    		add(bullet);
    
    		while (bullet.getY() > 0) {
    
    			bullet.move(0, -1);
    			pause(50);
    
    		}
    
    	}
    	
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Learning Java View Post
    I wonder whether it's to do with the pause method or the move method not doing what I think it's meant to do

    use Thread.sleep(long millis). Look at this example. it runs for 10 seconds and prints out the var i to the console and then sleeps for 1 second.

    Java Code:
    public class SleepExample {
    	public static void main(String[] args) {
    		int i = 0;
    		while (true) {
    			if (i++ < 10) {
    				System.out.println("Print i = " + i
    						+ " and then sleep for 1 second or 1000 millis");
    				try {
    					Thread.sleep(1000);
    				} catch (InterruptedException e) {
    				}
    			} else {
    				break;
    			}
    		}
    	}
    }

  3. #3
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Thanks for your reply. I have already tried what you have mentioned but it hasn't worked either -- unless I'm doing something wrong that is.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    I have already tried what you have mentioned but it hasn't worked either
    You should show what you've done. Without your code, we can only imagine.

  5. #5
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    (Off topic)

    Quote Originally Posted by j2me64 View Post

    Java Code:
    public class SleepExample {
    	public static void main(String[] args) {
    		int i = 0;
    		while (true) {
    			if (i++ < 10) {
    				System.out.println("Print i = " + i
    						+ " and then sleep for 1 second or 1000 millis");
    				try {
    					Thread.sleep(1000);
    				} catch (InterruptedException e) {
    				}
    			} else {
    				break;
    			}
    		}
    	}
    }
    Is there any particular reason the example uses an infinite loop to do something 10 times? Or is it just sloppy code? Surely it would be better to do:

    Java Code:
    public class SleepExample {
    	public static void main(String[] args) {
    		int i = 0;
    		while (i++ < 10) {
    			System.out.println("Print i = " + i
    			+ " and then sleep for 1 second or 1000 millis");
    			try {
    				Thread.sleep(1000);
    			} catch (InterruptedException e) {
    			}
    		}
    	}
    }
    Please correct me if im wrong :)

    berkeleybross

  6. #6
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    On topic:

    Have you tried extending the pause time to something longer than 50milliseconds? Perhaps the bullet is moving to fast for you to see.

    Where is bullet.move defined?

    And can you tell us a little about what you can see when the program is running?
    e.g. is the ship being drawn correctly?

    You may want to try to centre the ship vertically as well - the bullet may be travelling down and immediatly go off screen.

    Berkeleybross

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Learning Java View Post
    but it hasn't worked either -- unless I'm doing something wrong that is.

    strange, try to replace your while (bullet.getY() > 0) block of code with this one

    Java Code:
    		while (bullet.getY() > 0) {
    
    			bullet.move(0, -1);
    			// pause(50);
    			try {
    				Thread.sleep(50);
    				} catch (InterruptedException e) {
    				}
    		}

    a sleeping thread doesn' t consume any cpu time.

  8. #8
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    @Norm I've just tried what j2me64 suggested in post #7.

    @berkeleybross At 50 milliseconds I don't see anything happen at all, the program just freezes. If I remove the pause or decrease the pause delay to 1 millisecond I do actually see the bullet end up at the top of the canvas; so I can only assume it is doing what it's supposed to do. I've tried all sorts with the pause delay and nothing has worked. The move method is defined in the ACM library.

    @j2me64
    That still doesn't work bro.

    You know what, I wonder whether it's something to do with the mouse listener. Maybe the mouse listener and the pause method in the while loop interfere with each other or something?

    Thanks

  9. #9
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Learning Java View Post
    Maybe the mouse listener and the pause method in the while loop interfere with each other or something?

    maybe. post the code you use inside the pause method.

  10. #10
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Quote Originally Posted by j2me64 View Post
    maybe. post the code you use inside the pause method.
    I didn't write the pause method, it was provided in one of the imports. I'm new to java so not sure how I would get the code for it :confused:

  11. #11
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I've been following a tutorial called Killer game programming in Java, it introduced a very nice concept, called the update draw paint animation cycle. The good thing about this is, pausing is implemented very easily. All you need is a boolean representing the pause setting, if it's true, don't update the game state, just repaint:
    Java Code:
    while(true) {
      if(!paused)
        gameUpdate(); //updates the positions of all objects on screen
      gameRender(); //prepare an image of the current game state
      gamePaint(); //draw the prepared image on screen
    }
    This way, when the pause is triggered, your program will stil run (you can implement an options screen this way), but the state of the game will remain the same. Another good point of this cycle is, it introduces you to double buffering.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  12. #12
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Thanks for all the replies.

    I guess the reason why no one can help is because not many people have experience using those libraries. There's supposed to be another listener I can use like the mouse one for key entries but that doesn't work either, and it doesn't look like they teach it at the CS106a course now... maybe that's why. Oh well, thanks anyways!

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    listener I can use like the mouse one for key entries but that doesn't work either
    What doesn't work? What listener are you using? Have you tried to Use a KeyListener?

  14. #14
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Yes I tried using a KeyListener but it didn't work for me. The mouse listener works though... although like I said when I pause in the program things seem to go wrong -- but I don't know if that's related to the mouse listener; the only reason why I suspect it might be is because I've used the pause method before without any problems.

  15. #15
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Learning Java View Post
    I'm new to java so not sure how I would get the code for it :confused:

    sometimes the source-files are delivered with the jar-file, so that you can attach the source to the library in your ide. this way, when you for example debug your code you can even "step into" the library source code.

  16. #16
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    I don't know how I would do that. For the ACM library I have a single acm.jar file.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    tried using a KeyListener but it didn't work for me
    Can you post the code for us to take a look?

    Did the component have the focus?

  18. #18
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    I can't even add the key listener, I get an error: The method addKeyListeners() is undefined for the type Test. Whereas the mouse listener has no errors...

    Java Code:
    import acm.graphics.*;
    import acm.program.*;
    import java.awt.event.*;
    
    public class Test extends GraphicsProgram {
    
    	public void run() {
    
    		addKeyListeners();
    		addMouseListeners();
    
    	}
    
    }

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    The method addKeyListeners() is undefined for the type Test.
    I don't know anything about the GraphicsProgram class.
    I was talking about the Component class. Read the API doc for the Component class to get the method to call for adding a KeyListener to a component.

  20. #20
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    I don't know anything about the GraphicsProgram class.
    I was talking about the Component class. Read the API doc for the Component class to get the method to call for adding a KeyListener to a component.


    Strange, the KeyListener method works on my PC fine, but not on my mac. No worries, I'll try figure it out. Thanks for the help!

Page 1 of 2 12 LastLast

Similar Threads

  1. Make animation pause
    By mneskovic in forum New To Java
    Replies: 11
    Last Post: 06-08-2010, 05:16 PM
  2. is there a way to pause java?
    By zhangster in forum New To Java
    Replies: 4
    Last Post: 04-24-2010, 07:38 PM
  3. How to pause
    By ravjot28 in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-31-2010, 06:09 AM
  4. Make Java Pause
    By FlyNn in forum New To Java
    Replies: 2
    Last Post: 02-10-2010, 10:18 AM
  5. Help with Pause
    By trill in forum Java Applets
    Replies: 2
    Last Post: 09-28-2007, 08:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •