Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: Movement

  1. #1
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Movement

    Hello all,
    First of all, i have been googling this issue quite much. And i have also read the api. But i cannot seem to find the solution. The following code is being used:
    Java Code:
        public void keyPressed(KeyEvent e) {
    
            int key = e.getKeyCode();
            
            if (key == KeyEvent.VK_1){
            	selection = 1;
            }
            if (key == KeyEvent.VK_2){
            	selection = 2;
            }
            if (key == KeyEvent.VK_3){
            	selection = 3;
            }
            if (key == KeyEvent.VK_4){
            	selection = 4;
            }
            
            if (key == KeyEvent.VK_SPACE) {
            	if(timer<=0){
            		fire();
            		timer = selection*10;}
            }
            
            if (key == KeyEvent.VK_LEFT) {
            	direction = 'w';
                dx = -1;
            }
    
            else if (key == KeyEvent.VK_RIGHT) {
            	direction = 'e';
                dx = 1;
            }
    
            else if (key == KeyEvent.VK_UP) {
            	direction = 'n';
                dy = -1;
            }
    
            else if (key == KeyEvent.VK_DOWN) {
            	direction = 's';
                dy = 1;
            }
        }
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            
            if (key == KeyEvent.VK_LEFT) {
                dx = 0;
            }
    
            if (key == KeyEvent.VK_RIGHT) {
                dx = 0;
            }
    
            if (key == KeyEvent.VK_UP) {
                dy = 0;
            }
    
            if (key == KeyEvent.VK_DOWN) {
                dy = 0;
            }
        }
    
        public void move() {
        	timer--;
        	previousX = x;
        	previousY = y;
            x += dx;
            y += dy;
            if(x>320 && x<camera.mapLength-200)
            	camera.x+=dx;
            if(y>240 && y<camera.mapHeight-200)
            	camera.y+=dy;
        }
    The problem with this code is really, that you cant start shooting, and while still having spacebar pressed, move.
    The second problem is, that if i walk left, then right(while still having left pressed) and release right again, that i will not move. Even though i have left still pressed.
    The third problem is, it doesnt really work smooth. Pressing left, then quickly pressing right (right after i stop pressing left) will make the player stop moving. Even though i have right being pressed, I guess this is the same issue as the second problem.

    Thanks in advance!

  2. #2
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Movement

    (i thought it might be a bit clearer to post this in a second post)
    I also have got a problem with my scrolling map and the collision of the player. The problem is the following:
    Whenever i run up against a wall, then press left while up is still being pressed, the camera will move, but the player wont.
    All my objects are relative to the camera x and y position
    the collision code used:
    Java Code:
        public void checkCollisions() {
    
            Rectangle r3 = player_o.getBounds();
            
            for (int j = 0; j<Walls.size(); j++) {
                Object w = (Object) Walls.get(j);
                Rectangle r2 = w.getBounds();
    
                if (r3.intersects(r2)) {
                    player_o.setVisible(false);
                    player_o.setX(player_o.getPreviousX());
                    player_o.setY(player_o.getPreviousY());
                }
            }
    }
    And the following is used in all of my classes:
    Java Code:
    	public int getX(){
    		return x - camera.x;
    	}
    	
    	public int getY(){
    		return y - camera.y;
    	}

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Movement

    Try debuggging your code by adding printlns to print out the values of all the variables that control the execution flow.
    For example: the event passed to the method so you see when it is called and with what value.

  4. #4
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Movement

    Ok, ive done this, and i saw that my dy and dx are zero for a small time when pressing left-right really quickly. I dont know why this is though.
    It might be because of my keyrelease part. Should i add more statements to this? for example:
    Java Code:
            if (key == KeyEvent.VK_LEFT && !(key == KeyEvent.VK_UP && key == KeyEvent.VK_RIGHT && key == KeyEvent.VK_DOWN) {
                dx = 0;
            }
    However im afraid that this solution will bring other problems with it. And its also rather a lot of work. Thats why im asking first if its a good idea to do this.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Movement

    dy and dx are zero for a small time when pressing left-right really quickly
    Your testing of the variable key to have different values does not make sense. It will have one value.
    Java Code:
    if (key == KeyEvent.VK_LEFT && !(key == KeyEvent.VK_UP 
           && key == KeyEvent.VK_RIGHT && key == KeyEvent.VK_DOWN) {
    If it is VK_LEFT that's it. That is its value. Its value will NOT also be something else

    You could keep track of which keys are pressed by having your own variables save keys' status in the key Pressed and Released methods.
    Last edited by Norm; 01-15-2012 at 01:50 PM.

  6. #6
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Movement

    Ok so i fixed the camera problem by just giving it a previous x and y as well.

    Also thats exactly what i want isnt it? that it will set the dx only to zero if the left key is not pressed anymore.

    I dont really know what you mean by, "keep track of the keys which are pressed". How should i implement this?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Movement

    "keep track of the keys which are pressed". How should i implement this?
    If you are interested in knowing which keys are being pressed at the same time:
    When a key is pressed use the value of a variable to remember that. For example set a boolean to true
    When the key is released, change the variable's value. For example set the boolean false

  8. #8
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Movement

    So what i have to do now is make 4 booleans (left right up down). And set one of them if i press the according key? And set it to false as soon as i release the key?
    Ill try that now, will report it once its done. Thanks so far!

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Movement

    make 4 booleans (left right up down)
    If you want to respond to several keys being pressed at the same time.
    What if both left and right are pressed?

  10. #10
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Movement

    then i make an extra statement for that :).

  11. #11
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Movement

    Java Code:
        public void move() {
        	if(left)
        		dx = -1;
        	if(right)
        		dx = 1;
        	if(up)
        		dy = -1;
        	if(down)
        		dy = 1;
        	if(!left)
        		dx = 0;
        	if(!right)
        		dx = 0;
        	if(!up)
        		dy = 0;
        	if(!down)
        		dy = 0;
        	
        	
        	
        	timer--;
        	previousX = x;
        	previousY = y;
            x += dx;
            y += dy;
            if(x>320 && x<camera.mapLength-200){
            	camera.previousX=camera.x;
            	camera.x+=dx;
            }
            if(y>240 && y<camera.mapHeight-200){
            	camera.previousY=camera.y;
            	camera.y+=dy;
            }
            System.out.println("dx: " + dx  +" dy: " + dy);
        }
    this doesnt seem to work, at all.
    I cant even seem to get the basic movement this way
    Java Code:
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            
            if (key == KeyEvent.VK_LEFT) {
            	left = false;
                //dx = 0;
            }
    
            if (key == KeyEvent.VK_RIGHT) {
            	right = false;
               // dx = 0;
            }
    
            if (key == KeyEvent.VK_UP) {
            	up = false;
                //dy = 0;
            }
    
            if (key == KeyEvent.VK_DOWN) {
            	down = false;
               // dy = 0;
            }
        }
        public void keyPressed(KeyEvent e) {
    
            int key = e.getKeyCode();
                   
            if (key == KeyEvent.VK_LEFT) {
            	direction = 'w';
                //dx = -1;
            	left = true;
            }
    
            else if (key == KeyEvent.VK_RIGHT) {
            	direction = 'e';
                //dx = 1;
            	right = true;
            }
    
            else if (key == KeyEvent.VK_UP) {
            	direction = 'n';
                //dy = -1;
            	up = true;
            }
    
            else if (key == KeyEvent.VK_DOWN) {
            	direction = 's';
                //dy = 1;
                down = true;
            }
        }

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Movement

    this doesnt seem to work, at all.
    Print out the values of the variables that control its working and see where and why they get the values they have.

  13. #13
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Movement

    Thx i fixed it! for the people who might have the same problem, solution:
    Java Code:
        private void move(){
            if(left && !right)
                dx = -1;
            if(right && !left)
                dx = 1;
            if(up && !down)
                dy = -1;
            if(down && !up)
                dy = 1;
            if(!left && !right)
                dx = 0;
            if(!up&&!down)
                dy = 0;
            if(up&&down)
            	dy = 0;
            if(left&&right)
            	dx = 0;
        	previousX = x;
        	previousY = y;
            x += dx;
            y += dy;
            System.out.println("L: "+left+" R: "+right+" U: "+up+" D: "+down);
          }
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            
            if (key == KeyEvent.VK_LEFT) {
            	left = false;
            }
    
            if (key == KeyEvent.VK_RIGHT) {
            	right = false;
            }
    
            if (key == KeyEvent.VK_UP) {
            	up = false;
            }
    
            if (key == KeyEvent.VK_DOWN) {
            	down = false;
            }
            if (key == KeyEvent.VK_SPACE){
            	isShooting = false;
            }
        }
    
        public void keyPressed(KeyEvent e) {
    
            int key = e.getKeyCode();
            
            if (key == KeyEvent.VK_1){
            	selection = 1;
            }
            if (key == KeyEvent.VK_2){
            	selection = 2;
            }
            if (key == KeyEvent.VK_3){
            	selection = 3;
            }
            if (key == KeyEvent.VK_4){
            	selection = 4;
            }
            
            if (key == KeyEvent.VK_SPACE) {
            	isShooting = true;
            }
            
            if (key == KeyEvent.VK_LEFT) {
            	direction = 'w';
            	left = true;
            }
    
            else if (key == KeyEvent.VK_RIGHT) {
            	direction = 'e';
                right = true;
            }
    
            else if (key == KeyEvent.VK_UP) {
            	direction = 'n';
                up = true;
            }
    
            else if (key == KeyEvent.VK_DOWN) {
            	direction = 's';
                down = true;
            }
        }
        public void update() {    
        move();
        }
    However you couldve been a bit more helpful here, took me around 1 hour to see what exactly was going wrong. Only debugging doesnt always give a solution to the problem.
    But thanks anyway
    Last edited by elamre; 01-16-2012 at 05:04 PM. Reason: Fixing some brackets missing

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Movement

    My objective is to help you find your problems without giving you the solution. It is expected that you to spend the time and effort to find out what is wrong with your code.
    Personally I never looked at the code close enough to find your problem. I left that for you, since I expected you knew what you wanted the code to do and would recognize a problem when you saw it.
    Without code that compiles and executes that is very hard to debug an isolated piece of code.

    Glad you found it and can now move on to the next problem.

    BTW What was the problem and what was your fix?

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Movement

    Just as an aside:
    Java Code:
        if(!left && !right)
             dx = 0;
         if(left&&right)
             dx = 0;
    You could do that as a single statement:
    Java Code:
    if (left == right) dx = 0;
    Just to shorten things a bit.

  16. #16
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Movement

    @Tolls, but ofcourse! how silly of me not to see that. Thanks for pointing it out!

    @Norm, I know. Its not needed to give me the solution. But i mostly only post on forums as a last resort, which means that ive tried quite a lot already.

    Next problem, no clue yet. Trying to get the multiplayer to work. So far so good! But i probably do have a problem with my collision check. Will try to figure it out myself first ;).

  17. #17
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Movement

    Oki. next problem. I have a big delay with my network code. this is my logic (only works on the server for now):
    Server waits for connection. A client joins. The client will keep sending a string to the server. Namely the folowing:
    P,15,12,w,3,1,
    Where p is a way for me to see what kind of data it is about. The 15 is the x and the 12 is the y. Then the w is the direction of the other player (used for shooting) and the 1 is if the other player is shooting (is either 1 or 0). Im doing this with multithreading. The reading part:
    Java Code:
       	public void update(){
    		conLine = connect.getLine();			    
    		line = conLine.split(",");
    		x = Integer.parseInt(line[1]); 
    		y = Integer.parseInt(line[2]);
    		direction = line[3].charAt(0);
    		weapon = Integer.parseInt(line[4]);
    		shooting = Integer.parseInt(line[5]);
    		System.out.println("X: "+x+" Y: "+y+" D: "+direction+" W: "+weapon+" S: "+shooting);
    	}
    and my sending class:
    Java Code:
    public class NetworkSend extends Thread{
        ServerSocket Server = null;
        PrintStream os;
        Socket Client = null;
        
    	NetworkSend(int port){
            try {
                Server = new ServerSocket(port);
             }
             catch (IOException e) {
                System.out.println(e);
             } 
    	}
    	public void connect(){
    	    try {
    	    	   Client = Server.accept();
    	           os = new PrintStream(Client.getOutputStream());
    	        }   
    	    catch (IOException e) {
    	           System.out.println(e);
    	        }
    	}
    	public void run(){
    		while(camera.running){
    			Send(camera.player_o.getX(),camera.player_o.getY(),camera.player_o.getDirection(),camera.player_o.selection,1);
    			try {
    				Thread.sleep(1);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	public void Send(int x, int y, char direction, int weaponType, int shoot){
    		os.println("P,"+x+","+y+","+direction+","+weaponType+","+shoot+",");
    	}
    }
    What am i doing wrong?

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Movement

    I have a big delay with my network code
    You will have to provide a SSCCE for this. Your snippets of code don't show enough.

  19. #19
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Movement

    Sscce?

  20. #20
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Movement

    And also, is the way correct how im doing it now?

Page 1 of 2 12 LastLast

Similar Threads

  1. Checkers Movement
    By Hollowsoul in forum Advanced Java
    Replies: 14
    Last Post: 08-28-2011, 08:12 PM
  2. Movement/KeyListener freezing..
    By AndroidAppNewbie in forum New To Java
    Replies: 4
    Last Post: 03-10-2011, 04:31 PM
  3. Key/mouse movement
    By falkon114 in forum New To Java
    Replies: 3
    Last Post: 02-21-2011, 02:12 AM
  4. Sprite Movement
    By Curtiz in forum Java Gaming
    Replies: 1
    Last Post: 04-26-2010, 01:31 PM
  5. Movement of balls
    By BlitzA in forum New To Java
    Replies: 8
    Last Post: 01-09-2008, 03:30 PM

Posting Permissions

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