Results 1 to 7 of 7
  1. #1
    Jossos is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default Ball Move to mouse not animating.

    I'm trying to create a program that makes a ball object move to the location where the user clicks. It actually works fine (just doing the x location for now) but it doesn't play the frames between its starting location and the end location.

    Java Code:
    import acm.graphics.*;
    import acm.program.*;
    import acm.util.*;
    
    import java.awt.event.*;
    
    public class RT extends GraphicsProgram implements RTConstants {
    
    	public void run() {
    		
    		unit = new GOval (CIRCLE_DIAM, CIRCLE_DIAM);
    		unit.setFilled(true);
    		add(unit, 100, 100);
    		
    		addMouseListeners();
    		
    	}
    	
    	public void mouseClicked (MouseEvent e) {
    		
    		double x = e.getX();
    		double y = e.getY();
    		
    		if (x > unit.getX()) {
    			while (x > unit.getX()) {
    				moveToMouse(unit, x, y);
    				pause(10);
    			}
    		} else if (x < unit.getX()) {
    			while (x < unit.getX()) {
    				moveToMouse(unit, x, y);
    				pause(10);
    			}
    		}
    	}
    	
    	public void moveToMouse(GObject unit, double x, double y) {
    		if (x > unit.getX())
    			unit.move(SPEED, 0);
    		else if (x < unit.getX())
    			unit.move(-SPEED, 0);
    	}
    	
    	private GOval unit;
    }
    Please help! My other graphic programs involving animated GObjects work just fine, I don't understand why this one isn't working.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Ball Move to mouse not animating.

    it doesn't play the frames
    Can you explain what you mean here?

    Because you are using third party classes, not many of us can test your code. If you'd convert to using Java SE classes there would be more people that could help.

  3. #3
    Jossos is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default Re: Ball Move to mouse not animating.

    Quote Originally Posted by Norm View Post
    Can you explain what you mean here?

    Because you are using third party classes, not many of us can test your code. If you'd convert to using Java SE classes there would be more people that could help.
    Basically, what it does is move "unit" SPEED pixels (set to 3) 100 times a second (separated by a pause(10)) until the unit reaches the x location the user clicked at.

    This all works fine, however, I should be seeing unit "slide" across the screen.
    What I get is a freeze of the frame for the amount of time it would take to get there, and then it just appears there, without the "slide" in between, which is what I'm wanting.
    Last edited by Jossos; 01-05-2012 at 01:56 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Ball Move to mouse not animating.

    As I said: you are not using Java SE classes so I have no idea what the classes and methods you are using do.

  5. #5
    Jossos is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default Re: Ball Move to mouse not animating.

    nevermind then

  6. #6
    Jossos is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default Re: Ball Move to mouse not animating.

    Actually, I'll just ask another problem in this thread: why does eclipse crash every time I try to make a new class? I can't create classes now. How annoying.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Ball Move to mouse not animating.

    Perhaps if you explain what each of the statements in your code is supposed to do, someone could see a problem in your logic.

Similar Threads

  1. RNG Used To Move Mouse
    By alexburkee in forum New To Java
    Replies: 5
    Last Post: 12-12-2011, 12:36 AM
  2. Problem with click to move ball algorithm
    By Laythe in forum Java Applets
    Replies: 1
    Last Post: 12-19-2009, 01:00 AM
  3. How to move a ball across the applet window?
    By getkiran in forum Java Applets
    Replies: 3
    Last Post: 03-05-2009, 09:45 AM
  4. How do I make My ball to move randomly?
    By whdbstjr90 in forum New To Java
    Replies: 4
    Last Post: 12-31-2007, 06:32 PM
  5. Replies: 2
    Last Post: 12-01-2007, 08:48 AM

Posting Permissions

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