Game controll problem with keyAdapter

hi folks,
I have build an game in which i want to move the player sprite with the W,A,S,D keys(use them as arrows as in maby games). It works fine, but for example: when i walk up(w) en press then (a) to walk diagonal across the screen the sprite hangs for an moment. before it actualy does go diagonal across the screen. How can i get this movement without any hanging?
I got the following code in my application: (where up, down, left and right are instance variables)
private void activateGameControlsKeys(){
          addKeyListener(new KeyAdapter(){
               public void keyPressed(KeyEvent e){
                    int keyCode = e.getKeyCode();
                    if(!isPaused&&!gameOver){
                         if(keyCode == KeyEvent.VK_W){
                              up = true;
                         if(keyCode == KeyEvent.VK_S){
                              down = true;
                         if(keyCode == KeyEvent.VK_A){
                              left = true;
                         if(keyCode == KeyEvent.VK_D){
                              right = true;
                         if(up){
                              player.movePlayer("W");
                         if(down){
                              player.movePlayer("S");
                         if(left){
                              player.movePlayer("A");
                         if(right){
                              player.movePlayer("D");
               public void keyReleased(KeyEvent e){
                    if(!isPaused&&!gameOver){
                         int keyCode = e.getKeyCode();
                         if(keyCode == KeyEvent.VK_W){
                              up = false;
                         }else if(keyCode == KeyEvent.VK_S){
                              down = false;
                         }else if(keyCode == KeyEvent.VK_A){
                              left = false;
                         }else if(keyCode == KeyEvent.VK_D){
                              right = false;
          });Hope someone can help me out..
Greetz,

hi folks,
I have build an game in which i want to move the
player sprite with the W,A,S,D keys(use them as
arrows as in maby games). It works fine, but for
example: when i walk up(w) en press then (a) to walk
diagonal across the screen the sprite hangs for an
moment. before it actualy does go diagonal across the
screen. How can i get this movement without any
hanging?
I got the following code in my application: (where
up, down, left and right are instance variables)
private void activateGameControlsKeys(){
          addKeyListener(new KeyAdapter(){
               public void keyPressed(KeyEvent e){
                    int keyCode = e.getKeyCode();
                    if(!isPaused&&!gameOver){
                         if(keyCode == KeyEvent.VK_W){
                              up = true;
                         if(keyCode == KeyEvent.VK_S){
                              down = true;
                         if(keyCode == KeyEvent.VK_A){
                              left = true;
                         if(keyCode == KeyEvent.VK_D){
                              right = true;
                         if(up){
                              player.movePlayer("W");
                         if(down){
                              player.movePlayer("S");
                         if(left){
                              player.movePlayer("A");
                         if(right){
                              player.movePlayer("D");
               public void keyReleased(KeyEvent e){
                    if(!isPaused&&!gameOver){
                         int keyCode = e.getKeyCode();
                         if(keyCode == KeyEvent.VK_W){
                              up = false;
                         }else if(keyCode == KeyEvent.VK_S){
                              down = false;
                         }else if(keyCode == KeyEvent.VK_A){
                              left = false;
                         }else if(keyCode == KeyEvent.VK_D){
                              right = false;
          });Hope someone can help me out..
Greetz,Wait, what?
Your making it to complicated. Anyway, it would be easier if you did this:
private void activateGameControlsKeys()
{ addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e)
{ int keyCode = e.getKeyCode();
if(!isPaused&&!gameOver)
{ if(keyCode == e.VK_UP)
{ //Execute code here }
if(keyCode == e.VK_DOWN)
{ //Execute code here }
if(keyCode == e.VK_LEFT)
{ //Execute code here }
if(keyCode == e.VK_RIGHT)
{ //Execute code here }
} } } ); }I think i got the right amount of { things...
Anyway, execute the code within the keyPressed code, it's much easier. Believe me, I know. And the code won't repeat itself if you don't have the keyRelease code. And I have the same problem with the sprite hanging, it's just the way the code executes. I tried to find a way around it, but with no luck. I hope my revision of your code helps some.
Message was edited by:
g@m3r

Similar Messages

  • User Account Control Problems with Premier Elements 7 32 bit in 64 Bit Windows 7

    I found User Account Control Problems in other forums but didn't see much in Premiere Elements, so I hope this helps others who may be having problems with 32 bit programs in a 64 bit computor. When I first got my 64 bit I didn't have problems.
    Back in February I bought a Quad Core Desktop with Windows 7 64 bit. Haven't been on the forum for 7 months since I wrote in regarding importing video with the Pyro AV Link (Premiere Elements Tips and Tricks). I was having problems importing video which started after March 28 sometime, as that was the last time I didn't have a problem importing VHS. Took me a few months off and on to find out what worked.
    I started uninstalling Windows updates and any programs I had installed back to that point in time. Checked after uninstalling each item. The capture screen would come up on a delayed basis but clicking on capture did nothing. Although no driver is required for IE1394 (so they say) I read where some people using the Legacy driver had fixed some problems, so I switched to the Legacy. Seemed to work great (for a few minutes).
    I had even uninstalled and reinstalled PE7 but no difference. Finally I noticed a shield in the PE7 Desktop icon. Thats weird. Never had that before. Just decided to show up after I had done all of the stuff I mentioned. Turns out at some point in time since I've had the computor Windows 7 decided to flag my 32 bit program as misusing the 64 bit system. User Account Control. I never bothered too much with UAC and just clicked on the yes box wen it popped up asking if I wanted this or that to take control of my computor. I tried using the minimum setting for safety reasons, and then finally turned User Account Control off. Bingo! PE7 capture works again and still working.
    While I am not recommending that people arbitrarily turn UAC off, this appeared to be my problem. I would recommed that they try turning it off before uninstallin a bunch of stuff. Having a restore point may or may not have been a fix for me. I think one of the Windows Updates caused the problem but I am not going to uninstall any more stuff at this point, I have not been back on the internet or installed any more updates in my desktop. I am going to use my laptop for internet and used the desktop for video.
    I am guessing this could be a problem for any 64 bit systems trying to use 32 bit programs. IE: XP,Vista,Windows 7 64 bit. In Vista there is also Data Execution Prevention (DEP) which you can turn on or off, supposedly to prevent misuse of memory (as I understand it), but Win7 either doesn't have it or I haven't found where it's located.
    Maybe this has been covered in the forum somewhere and I missed reading it. Hope I am not too confusing. Anyway, whatever feedback you guys have would be great.  

    John,
        Thanks for your answer, and I did check out the link you suggested. However, I have Windows 7 Home Premium as most people have I think, and the XP program was not available. I started with Vista and now have Win 7. I never could navigate around in XP anyway so am content to just leave User Account Control off.
        Interesting side note to turning UAC off is that my desktop seemed to be using more of the CPUs, where half of them were "Parked" as I wrote in also about 7 months ago when I 1st got my desktop. I am pleased to say that video processing seems to go quicker with UAC off. Have to observe things some more to confirm.
        I just thought if others were having problems after installing Windows Updates and leaving UAC off worked then they could possibly pinpoint a problem.

  • Flash games, serious problem with windows 7 bootcamp

    when you open a webpage that has a flash game, windows 7 bootcamp stucks from time to time, with no reason! why is this happening? using firefox. tried safari the same thing, and tried latest flash player on both. try facebook games or google random flash games most of them cause windows 7 to stuck or go slow! suggestions please, does anyone else have the same problem?

    sorry the whole win 7 freezee on mbp 13", wait for apple update (i hope they notice)! now running XP SP3 and runs great!!! "laptop 2 go drivers" and runs games perfect!

  • Controlling- Problem with Assigned

    Hi
    I have the following problem:
    I implemet the budget control through customizing.
    I start to creat internal orders and i settlement the original budget.
    Then i continued working as normal creating PO, invoices etc, all against the internal order.
    When i go to KO22 to see the original budget and the assigned, in some orders, the assigned is upper than the budget (its wrong becuase i implement the Bugdget controlling for internal orders). And in some cases the commitments plas the actual postings is upper than the assigned (its is wrog becuase the assigned is form by the commitments and actual postings).
    I dont know why it happend.
    Please if someone have a similar problem or know about this issue, please help me.
    Thanks in advance for your help
    Enzo

    Commitments and actual postings can exceed budget if tolearnce limt for 100% of usage is set up at warning / information i.e. it will allow to post additional costs /create PO against the order with information / warning message to the user.
    Check the tolearnce limit in customizing and it should be level '3' (error message) against 100% usage.
    Assigned value should be equal to actual & commitments. You can 'Set up Availability Control again' (KO31) to reconstruct the database for the orders with error.
    Regards,

  • Game Control Problems

    Hithere seems to be an issue when I play games on the ps4. For example if I play call of duty I randomly start to sway left and right without me guiding it with the controller, in rocket league I would randomly turn left or right without me guiding. I exchanged my console in for a new one, bought a new controller and the issue still seems to be happening the only reason I could think of this happening is the Internet connection, but the random controls even happened in the menu offline going left to right without any control over itany suggestions? I've tried everything I've thought of 

    It could be interference between wifi and bluetooth.
    If you are not using ta Vita for remote play, turn off the setting for connecting directly to Vita /ps tv. This is because the ps4 broadcasts a wifi hotspot.
    Try setting your console on it's side, it changes the alignment of the wifi and bluetooth antennas.
    Try changing the channel your router's using for wifi.
    If you are connected to the internet via wifi, try connecting your console to your router via ethernet cable, or powerline /homeplug adapters.

  • Volume control problem with 30GB iPod 3G, Universal Dock, and Apple Remote

    I just hooked up my 30GB iPod 3G to the Universal Doc and tried the my new Apple remote. The only buttons on the remote that work in controlling my iPod while on the dock are the play/pause and the skip forward and back buttons.
    For some reason the volume up and down buttons don't adjust the volume on my iPod while it's on the dock. Any ideas? Shouldn't the volume up and down work? Or do they only work on the newer iPods?
    Any info appreciated.
    17' Aluminum Powerbook Mac OS X (10.4.3) 30GB iPod 3G
    17' Aluminum Powerbook   Mac OS X (10.4.3)  

    btabz,
    Thanks for your response. No the iPod doesn't respond at all--the volume display doesn't come up or change.
    When I press the play/pause or either skip forward or back on the remote, the iPod, while on the dock, lights up and responds to the command as it should. But when I press volume up or down on the remote the iPod does not respond at all.
    According to Apple's store info on the remote, the volume up/down controls should work. I am wondering if maybe it doesn't work with the 3Gs for some reason. But if that is the case, Apple should indicate that. It is kind of frustrating to not be able to adjust the volume.
    Thanks,
    gerb38
    17' Aluminum Powerbook   Mac OS X (10.4.3)  
    17' Aluminum Powerbook   Mac OS X (10.4.3)   30GB 3G iPod

  • Creative X-Treme Gamer has Problems with the HS-

    hi,
    my x-treme gamer doesn`t like headsets my old speed link and my new hs-900 doesnt work, windows doesnt recognize them, i've tried to activate mic boost and deactivate it, nothing works, i have coneccted it through my front connectors in my case i can hear sound but mic never works.
    Also i got another problem, my master volume is only on 40% when i go over it its to loud even when my speaker are at volume 0 i hear something. also when i plug my headset in it can be that i have weird sounds. also i have to reinstall my card all 4-5 days because its not in the device manage rin windows, i think the card is defect maybe

    Hi,
    Do you have any other USB devices plugged into the pc? If you do, please remove them to see if it makes a difference. You can also try connecting the webcam to a different USB port.
    Customer Support can be contacted through this email form if you need further assistance.

  • SSC web control problem with Crossover

    Hey i've installed Crossover to work with a control...and this one sscwebcontrol will not work. any advice? is there a program that I can download through crossover to get it working?

    You should probably contact Crossover support. If the app is not supported by Crossover then you'll probably need to install Windows on your Mac via Boot Camp or Parallels.

  • ActiveX control Problem with Froms 6.0.8.11.3

    Hi,
    I am using Oracle Forms 6.0.8.11.3 version on windows XP.
    I am inserting the activex object in the layout and when runing the form the object is not shown.
    After that when i am checking in the design (FMB) file the object is not shown and asking me to reinsert the object.
    Can any body guide me on this? Is there any bugs are there related to this ?
    Thank you
    Gouri

    Yup - Sounds like 593051 which is to do with the licence information used by the ActiveX not being correctly stored with the control. This is fixed in 6.0.8.15.1

  • Problem with KeyAdapter and JButtons in a Jframe

    yes hi
    i have searched almost most of the threads here but nothing
    am trying to make my frame accepts keypress and also have some button on the frame here is the code and thank you for the all the help
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class RPg extends JFrame implements ActionListener
                private int width;
                private int hight;
                public    JPanel north;
                public    JPanel se;
                public    JButton start;
                public    JButton quit;
                public    Screen s;//a Jpanel
                public    KeyStrokeHandler x;
        public RPg() {
               init();
               setTitle("RPG");
               setSize(width,hight);
               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               setResizable(false);
               setVisible(true);
               x=new KeyStrokeHandler();
             addKeyListener(x);
             public void init() {
               north=new JPanel();
               Container cp = getContentPane();
               s=new Screen();
               this.width=s.width;
               this.hight=s.hight+60;
              start=new JButton("START");
                  start.addActionListener(this);
              quit=new JButton("QUIT");
                  quit.addActionListener(this);
              north.setBackground(Color.DARK_GRAY);
              north.setLayout(new FlowLayout());
              north.add(start);
              north.add(quit);
              cp.add(north, BorderLayout.NORTH);
              cp.add(s, BorderLayout.CENTER);
        public static void main(String[] args) {
            RPg rpg = new RPg();
       public void actionPerformed(ActionEvent ae) {
            if (ae.getActionCommand().equals("QUIT")) {
                     System.exit(0);
            if (ae.getActionCommand().equals("START")) { }
       class KeyStrokeHandler extends KeyAdapter {
               public void keyTyped(KeyEvent ke) {}
               public void keyPressed(KeyEvent ke){
                  int keyCode = ke.getKeyCode();
                  if ((keyCode == KeyEvent.VK_M)){
                     System.out.println("it works");
               public void keyReleased(KeyEvent ke){}

    Use the 'tab' key to navigate through 'contentPane > start > quit' focus cycle. 'M' key works when focus is on contentPane (which it is on first showing). See tutorial pages on 'Using Key Bindings' and 'Focus Subsystem' for more.
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class RPG extends JFrame implements ActionListener {
        private int width;
        private int hight;
        public JPanel north;
        public JPanel se;
        public JButton start;
        public JButton quit;
        public Screen s;    //a Jpanel
        public RPG() {
            init();
            setTitle("RPG");
            setSize(width,hight);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setResizable(false);
            setVisible(true);
        public void init() {
            north=new JPanel();
            Container cp = getContentPane();
            registerKeys((JComponent)cp);
            s=new Screen();
            this.width=s.width;
            this.hight=s.hight+60;
            start=new JButton("START");
                start.addActionListener(this);
            quit=new JButton("QUIT");
                quit.addActionListener(this);
            north.setBackground(Color.DARK_GRAY);
            north.setLayout(new FlowLayout());
            north.add(start);
            north.add(quit);
            cp.add(north, BorderLayout.NORTH);
            cp.add(s, BorderLayout.CENTER);
        public static void main(String[] args) {
            RPG rpg = new RPG();
        public void actionPerformed(ActionEvent ae) {
            if (ae.getActionCommand().equals("QUIT")) {
                     System.exit(0);
            if (ae.getActionCommand().equals("START")) { }
        private void registerKeys(JComponent jc)
            jc.getInputMap().put(KeyStroke.getKeyStroke("M"), "M");
            jc.getActionMap().put("M", mAction);
        Action mAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("it works");
    class Screen extends JPanel {
        int width, hight;
        public Screen() {
            width = 300;
            hight = 300;
            setBackground(Color.pink);
    }

  • RS Control problem with decklink multibridge

    Hello,
    I wish to control a HDCAM VTR true 9-pin RS. I've got a Blackmagic Multibridge Extreme and FCP. I could control the vtr in BM decklink control but I don't see any time code... Need help....
    Greetz,
    Wim

    1)make sure the deck settings match what you shot. the frame rate indicator should show what the deck is set at. while you're in playback if the data on tape doesn't match the deck the frame rate indicator will flash. set the deck to match the tape format.
    2)while the deck is playing make sure the counter is set to TC and see if you're getting numbers there. if you are getting good numbers in the tc window and the frame rate isn't flashing check your decklink and or FCP settings.

  • Problem with "The Movies" game DVD

    Ok, here is my problem.
    I bought the game "The Movies", at first it was working fine, but one evening, I tried to upload a movie and it just didn't do it. I tried 3 or 4 times, but I could never upload my movie.
    So I stop trying and the day after when I tried to play, I put my DVD in my computer, when I tryed to play, it says put the DVD in the computer, but the DVD was already in, so I took it out, cleaned it and put it back, but then the DVD never appeared on the desktop.
    I tried many other DVD/CD and everything works just fine.
    Then I delete everything from the game (maybe I shouldn't have) and tried again, but nothing.
    Later I learn that a Flip4Mac update was causing some problem with the game and I did something when installing a mod, I had the french version, but the .txt in the mod was in english, so I add, the english (en-UK) in a file.
    Now, I rebought the game + the expansion (The Movies + The Movies Stunts & Effects, the english version this time) and the same thing is going on, the "The Movies" DVD won't appear on the desktop, but when I put "Stunts & Effects" in it works fine.
    What can I do?
    Please help.

    "I tried to upload a movie and it just didn't do it. I"
    How much free drive is on your Mac? Right or control click the MacintoshHD icon on your Desktop, then click: Get Info. In the Get Info window, click the discovery triangle so it's facing down. You will see; Capacity and Available Make sure you have 10% available disk space, 15% is better.
    Boot from your restore disk and make sure the boot disk is free of errors. Restart your Mac while holding down the C key, pressing the power button and inserting your restore disk all at the same time. An Installer window will open, but do not proceed with any installations. Instead, from the Menu Bar, select Utilities/Disk Utility. In the Disk Utility window click First Aid, and then click Verify. If Disk Utility reports errors, click Repair. When Disk Utility is finished, from the Menu Bar, select Utilities/Startup Disk. Select MacintoshHD 10.x.x and click Restart.
    Carolyn

  • 7700 speaker problems with control Pod

    ok if anyone has speakers like this they know it comes with a little control pd that controls the volume and bass. well i think the problem is that sometimes the volume goes really high or really soft. and if you turn the volume up or down, the sound will go back to normal and you will be blasted or can't hear it. its very anoying to listen to music or game and this happens. it does this regardless of the number of speakers attached, and my drivers are up todate. i have onboard sound on a asus A8V mobo so its a newer board. some people said that the pod can get wonky, and i am checking to see if anyone has any suggestions on what i should maybe try short of RMA the speakers, its not that i don't want to RMA it but i would have to pay shipping on a set of speakers witch is kinda gay. any help would be great.. thanks

    fox_9 wrote:
    ok if anyone has speakers like this they know it comes with a little control pd that controls the volume and bass. well i think the problem is that sometimes the volume goes really high or really soft. and if you turn the volume up or down, the sound will go back to normal and you will be blasted or can't hear it. its very anoying to listen to music or game and this happens. it does this regardless of the number of speakers attached, and my drivers are up todate. i have onboard sound on a asus A8V mobo so its a newer board. some people said that the pod can get wonky, and i am checking to see if anyone has any suggestions on what i should maybe try short of RMA the speakers, its not that i don't want to RMA it but i would have to pay shipping on a set of speakers witch is kinda gay. any help would be great.. thanks
    I have the same problem with my inspire 6.'s volume pod, after well under a year of flawless perfomance, the volume lowers randomly especially when I adjust the pod's volmue or just touch or move it. If I try to simply adjust for the loss of volume by cranking it up, it likes to blast me outta nowhere when it decides to work again. The only thing I can do to shortcut the 'down time' while waiting for it to adjust to normal level is to tap on the pod repeatedly usually causing the volume to adjust quicker...but I'm sure thats not the right method for fixing this irritating problem...PLEASE wont someone HELP ME?! Feel free to e-mail me @ [email protected], thanx!
    Message Edited by BigLovan on 10-28-2004 01:16 AM

  • I can't download from app store apps. if i try then open account and ask me shange payment type because there is a billing problem with a previous purchase. I download one game 0.99 and nothing more! help me. How can i contact with apple?

    I can't download from app store apps. if i try then open account and ask me shange payment type because there is a billing problem with a previous purchase. I download one game 0.99 and nothing more! help me. How can i contact with apple?

    Click here and ask the iTunes Store staff for assistance.
    (102938)

  • Problem with java and yahoo games

    hello everyone,
    i have a mac 10.6.8 with java SE 6 1.6.0_33-603-424 and i have problems with yahoo games.... anyone can help me please, i have a mac 10.6.8 with java SE 6 1.6.0_33-603-424 and i have problems with yahoo games.... anyone can help me please

    So here is what comes to pass.
    Recall this started as a Lion Safari/5+ environment that SU took to version 6.
    Two weeks ago I just updated Flash Player. Today I fire up Java Preferences and I am told that I need to download Java. Did so, intentionally hit the "factory reset" button and then installed the Flash Player from teh Adobe site. The most recent install suggests automatic update but I choose "ask first."
    Restart Safari and we seem to be in order
    Thanks

Maybe you are looking for

  • Opinion of this hard drive?

    Hi All, I really need to just buckle down and buy a new mac book, but in the meantime I was thinking about upgrading my drive (again). I'm worried it might create too much heat for the mac book pro though.....i have some issues sometimes with freezin

  • WBS Issue

    Dear Experts, We have created Project PO with following taxes(VAT 5%, Excise Duty 10.3% and Freight)and simultaneously done the GRN for the same,but the issue is that when we are checking the budget in CJ33 only BasicExcise dutyFreight value is assig

  • Changing Query Component's criteria items at runtime

    Hi All, As per my requirement i am changing query component's criteria items on some user input. So initially there were 9 components on query panel form and after user input it will become 7. I programatically used to set this two component's UI Hin

  • My JSF Page not load in firefox3  woodstock4_3.widget is undefined

    Hi I am test the jsp page in firefox 3 and the page is loading for a long time, sometimes im refresh de page and load normally, somebody know what happen?. Why in iexplorer is 3 o 4 more slow than firefox 2. Additionali en the errors console i see Er

  • No sound from .avi movies in QuickTime

    Hi -- I have two questions, really: 1. Why no audio from .avi movies played in QuickTime? 2. How do I install audio codecs? Thanks!