Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Moving sprite with keyboard slight problem

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    26
    My Mood
    Aggressive
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Moving sprite with keyboard slight problem

    i got an image and i can move it with my keyboard (left/up/down/right)

    when i LONGPRESS an arrow key like how you usually do this -> wwwwwwwwwwwwwwwwwww,

    it does move 1 pixel away, then stops for a second or so, then continues moving.

    what i want is it keeps moving on :/

    please help!

    if my explanation is smoggy, try to type a letter with just a single press and you will see it stops and types again... >.<


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Moving sprite with keyboard slight problem

    I see the problem.
    So you need to translate this wwwwwwwwwwwwwwwwwww (resembling directional input) which is a series of key presses (caused by a long press, yes, but it is what it is), into an on and off.
    What have you tried?
    Some way to make it go when the button is pressed down, and only stop when the button is released.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    26
    My Mood
    Aggressive
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Moving sprite with keyboard slight problem

    in a keylistener i did this

     
    public void KeyPressed(KeyEvent ke){
        if(ke.getKeyCode()==KeyEvent.VK_LEFT){
            direction[3] = 1;
        }
        /*
        same with other 3 keys
        */
    }
     
    public void KeyReleased(KeyEvent ke){
        if(ke.getKeyCode()==KeyEvent.VK_LEFT){
            direction[3] = 0;
        }
        /*
        same with other 3 keys
        */
    }

    in a timer with actionlistener i have this

     
    lbl_map.setLocation(lbl_map.getX()+direction[0]-direction[2], lbl_map.getY()+direction[1]-direction[3]);

    :/

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Moving sprite with keyboard slight problem

    Post a SSCCE, I do not see why that does what you describe.

Similar Threads

  1. Slight problem with fraction calculator code
    By Jampolo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2013, 05:01 PM
  2. Sprite won't animate while moving! Please help
    By sellers04 in forum AWT / Java Swing
    Replies: 3
    Last Post: December 14th, 2011, 06:41 PM
  3. Moving sprite leaves a trail
    By chris2307 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 26th, 2011, 09:01 AM
  4. Problem in rotating and moving image at the same time
    By SlimShady in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 17th, 2010, 03:33 PM
  5. Moving circle problem help
    By nobleisthyname in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 3rd, 2010, 12:43 PM