Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: My player moves but I would like it to be less jagged.

  1. #1
    Join Date
    Jun 2013
    Location
    Sheffield
    Posts
    4
    Rep Power
    0

    Question My player moves but I would like it to be less jagged.

    Java Code:
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import javax.swing.ImageIcon;
    
    public class Player extends Entity{
    	int speed = 3;
    
    	public Player(int x, int y) {
    		super(x, y);
    	}
    	public void update(){
    
    	}
    	public void draw(Graphics2D g2d){
    		g2d.drawImage(getPlayerImage(), x, y, null);
    	}
    	public Image getPlayerImage(){
    		ImageIcon ic = new ImageIcon("C:/*********/img/walk1.png");
    		return ic.getImage();
    	}
    	public void keyPressed(KeyEvent e){
    		if(e.getKeyCode() == 37){
    			x-= speed;
    		}
    		if(e.getKeyCode() == 38){
    			y-= speed;
    		}
    		if(e.getKeyCode() == 39){
    			x+= speed;
    		}
    		if(e.getKeyCode() == 40){
    			y+= speed;
    		}
    	}
    	public void keyReleased(KeyEvent e){}
    }
    This is my code for my player, but I really want a better way to make the player move.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: My player moves but I would like it to be less jagged.

    One possibility that I have not tried is to increase the speed to say 5 (or some value of your choice). Then when the user types in a key, you simply repaint the image 5 times, incrementing x and y to the reach the speed, perhaps with a timer of some amount of milliseconds between repaints. Variables to consider and test.

    1. The overall speed.
    2. The x and y increments when doing multiple paints.
    3. The timer value between paints.

    You may also want to only paint the part of the panel that has changed. Check out using clip regions in the tutorials in my signature under painting.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Jun 2013
    Location
    Sheffield
    Posts
    4
    Rep Power
    0

    Default Re: My player moves but I would like it to be less jagged.

    Thanks, I found another way of making my character move and thanks for the help!

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: My player moves but I would like it to be less jagged.

    Instead of moving the player each time the key is pressed, instead keep track of which keys are pressed (using a boolean value for each key). Then in your game loop, check those booleans and move your character there instead.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Join Date
    Jun 2013
    Location
    Sheffield
    Posts
    4
    Rep Power
    0

    Lightbulb Re: My player moves but I would like it to be less jagged.

    Fixed:
    Java Code:
        
        int speed = 2;
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
             
            if (key == KeyEvent.VK_W) {
              velY = -speed;
            } else if (key == KeyEvent.VK_S) {
              velY = speed;
            } else if (key == KeyEvent.VK_A) {
              velX = -speed;
            } else if (key == KeyEvent.VK_D) {
              velX = speed;
         }}

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: My player moves but I would like it to be less jagged.

    Think about this: what if you want something to move slower than 1 pixel per "tick". You can't with your current code.

    Position and speed should be floats for the best results and most flexibility, not hard integers.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: My player moves but I would like it to be less jagged.

    That might make it move slower but I don't think it will make it smoother. The smallest granularity of movement is a pixel at a time. So if you have to move the right arrow 5 times to move one pixel, the movement will still be one pixel.

    Regards,
    Jim
    Last edited by jim829; 06-15-2013 at 02:55 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: My player moves but I would like it to be less jagged.

    Quote Originally Posted by jim829 View Post
    The smallest granularity of movement is a pixel at a time.
    You can simulate subpixel locations with Graphics2D and antialiasing.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: My player moves but I would like it to be less jagged.

    Quote Originally Posted by CadeLegallow View Post
    Fixed:
    Java Code:
        
        int speed = 2;
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
             
            if (key == KeyEvent.VK_W) {
              velY = -speed;
            } else if (key == KeyEvent.VK_S) {
              velY = speed;
            } else if (key == KeyEvent.VK_A) {
              velX = -speed;
            } else if (key == KeyEvent.VK_D) {
              velX = speed;
         }}
    I believe all you've done is cover up the problem by making the whole thing faster. This approach is still going to be problematic, for several reasons- different systems repeat keystrokes at different speeds, and different keyboards support fewer multiple key presses at one time. I highly suggest using booleans to keep track of which keys are pressed instead of relying on putting business logic inside the key handler itself.
    DarrylBurke likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: My player moves but I would like it to be less jagged.

    Quote Originally Posted by jim829 View Post
    That might make it move slower but I don't think it will make it smoother.
    If you don't have a steady game loop, it certainly won't matter.

    The smallest granularity of movement is a pixel at a time.
    On the screen, certainly. But not in your game's model. You can easily be at position 30.0 and have it translate to X=30 on screen. Now say that the player is pushing the right-key and your speed is 0.5 pixels per tick, the update will:

    TICK 0: POSX = 30.0, SCREENX= 30
    TICK 1: POSX = 30.5, SCREENX = 30
    TICK 2: POSX = 31.0, SCREENX = 31

    See how that works? But regardless of you using floats or not, if the time between ticks is random, say the time between tick 0 and tick 1 is 6ms and the time between tick 1 and tick 2 is 15ms, it will appear jerky. For fluent movement the updating rate needs be relatively stable.

Similar Threads

  1. Passing Jagged Array to JSONoBJECT and StringEntity
    By josiane.ferice in forum New To Java
    Replies: 4
    Last Post: 04-30-2013, 05:09 PM
  2. Need Help with this question. Its about java and moves
    By JoseManuel in forum New To Java
    Replies: 10
    Last Post: 04-02-2013, 03:32 PM
  3. Jagged array loop question
    By Dankaru in forum New To Java
    Replies: 3
    Last Post: 11-14-2012, 04:21 PM
  4. Bishops moves
    By michail in forum New To Java
    Replies: 5
    Last Post: 01-28-2010, 10:33 PM

Tags for this Thread

Posting Permissions

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