#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    9
    Rep Power
    0

    Smile Space Shooter - Movement Issues


    Hello, I just started working on a space shooter in Javascript.

    The Problem:
    When i am moving in one direction and press a key for another direction the ship stops for a second and then goes the other way. once it starts the direction its fine though its just when I want to change directions (change keypressed).

    I am using onKeyDown and onKeyUp to trigger movement.

    My Question:
    How can I make my ship move to key events without a delay such as pausing when I go another way?
  2. #2
  3. No Profile Picture
    http://stealthwd.ca
    Devshed Novice (500 - 999 posts)

    Join Date
    Dec 2005
    Posts
    691
    Rep Power
    200
    Originally Posted by DigitalCrayons
    Hello, I just started working on a space shooter in Javascript.

    The Problem:
    When i am moving in one direction and press a key for another direction the ship stops for a second and then goes the other way. once it starts the direction its fine though its just when I want to change directions (change keypressed).

    I am using onKeyDown and onKeyUp to trigger movement.

    My Question:
    How can I make my ship move to key events without a delay such as pausing when I go another way?
    Hard to say what the problem might be without looking at code. I don't know much about game dev, but my guess would be you have a ton of stuff going on in your main logic loop so there is a delay in reacting to the key press.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    9
    Rep Power
    0
    Here is the code I wrote.

    Code:
    function _keyDown(e){ 
    switch (e.keyCode) { 
    case 87: 	up = true; 	
                    move();   
                    break; 
    case 83:   	down = true; 	
                    move();   
                    break; 
    case 68: 	right = true; 	
                    move();   
                    break; 
    case 65:   	left = true; 
    	        move();   
                    break; 
    } }  
    function _keyUp(e){ 
    switch (e.keyCode) { 
    case 87: 	up = false;   
                    break; 
    case 83:   	down = false;  
                    break; 
    case 68: 	right = false;   
                    break; 
    case 65:   	left = false;   
                    break; 
    } }  
    function move(){ 
    if (up == true && right == true){ 	
                    shipY = shipY - 10; //up 	
                    shipX = shipX + 10; //right 
    } else if (up == true && left == true){ 	
                    shipY = shipY - 10; //up 	
                    shipX = shipX - 10; //left 
    } else if (down == true && right == true){ 	
                    shipY = shipY + 10; //down 	
                    shipX = shipX + 10; //right 
    } else if (down == true && left == true){ 	
                    shipY = shipY + 10; //down 	
                    shipX = shipX - 10; //left 
    } else if (up == true){ 
                    shipY = shipY - 10; //up 
    } else if (right == true){ 	
                    shipX = shipX + 10; //right 
    } else if (down == true){ 
                    shipY = shipY + 10; //down 
    } else if (left == true){ 	
                    shipX = shipX - 10; //left 
    } }
  6. #4
  7. No Profile Picture
    http://stealthwd.ca
    Devshed Novice (500 - 999 posts)

    Join Date
    Dec 2005
    Posts
    691
    Rep Power
    200

IMN logo majestic logo threadwatch logo seochat tools logo