Results 1 to 20 of 25
Thread: Movement
- 01-15-2012, 04:22 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
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:
The problem with this code is really, that you cant start shooting, and while still having spacebar pressed, move.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 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!
- 01-15-2012, 04:27 AM #2
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
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:
And the following is used in all of my classes: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()); } } }
Java Code:public int getX(){ return x - camera.x; } public int getY(){ return y - camera.y; }
- 01-15-2012, 01:02 PM #3
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.
- 01-15-2012, 01:31 PM #4
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
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:
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.Java Code:if (key == KeyEvent.VK_LEFT && !(key == KeyEvent.VK_UP && key == KeyEvent.VK_RIGHT && key == KeyEvent.VK_DOWN) { dx = 0; }
- 01-15-2012, 01:48 PM #5
Re: Movement
Your testing of the variable key to have different values does not make sense. It will have one value.dy and dx are zero for a small time when pressing left-right really quickly
If it is VK_LEFT that's it. That is its value. Its value will NOT also be something elseJava Code:if (key == KeyEvent.VK_LEFT && !(key == KeyEvent.VK_UP && key == KeyEvent.VK_RIGHT && key == KeyEvent.VK_DOWN) {
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.
- 01-15-2012, 02:15 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
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?
- 01-15-2012, 02:23 PM #7
Re: Movement
If you are interested in knowing which keys are being pressed at the same time:"keep track of the keys which are pressed". How should i implement this?
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
- 01-15-2012, 02:27 PM #8
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
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!
- 01-15-2012, 02:30 PM #9
Re: Movement
If you want to respond to several keys being pressed at the same time.make 4 booleans (left right up down)
What if both left and right are pressed?
- 01-15-2012, 02:36 PM #10
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
Re: Movement
then i make an extra statement for that :).
- 01-15-2012, 02:46 PM #11
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
Re: Movement
this doesnt seem to work, at all.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); }
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; } }
- 01-15-2012, 03:06 PM #12
Re: Movement
Print out the values of the variables that control its working and see where and why they get the values they have.this doesnt seem to work, at all.
- 01-16-2012, 05:02 PM #13
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
Re: Movement
Thx i fixed it! for the people who might have the same problem, solution:
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.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(); }
But thanks anywayLast edited by elamre; 01-16-2012 at 05:04 PM. Reason: Fixing some brackets missing
- 01-16-2012, 05:11 PM #14
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?
- 01-16-2012, 05:18 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,446
- Rep Power
- 16
Re: Movement
Just as an aside:
You could do that as a single statement:Java Code:if(!left && !right) dx = 0; if(left&&right) dx = 0;
Just to shorten things a bit.Java Code:if (left == right) dx = 0;
- 01-16-2012, 05:55 PM #16
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
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 ;).
- 01-16-2012, 10:47 PM #17
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
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:
and my sending class: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); }
What am i doing wrong?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+","); } }
- 01-16-2012, 10:50 PM #18
Re: Movement
You will have to provide a SSCCE for this. Your snippets of code don't show enough.I have a big delay with my network code
- 01-16-2012, 11:14 PM #19
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
Re: Movement
Sscce?
- 01-16-2012, 11:19 PM #20
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
Similar Threads
-
Checkers Movement
By Hollowsoul in forum Advanced JavaReplies: 14Last Post: 08-28-2011, 08:12 PM -
Movement/KeyListener freezing..
By AndroidAppNewbie in forum New To JavaReplies: 4Last Post: 03-10-2011, 04:31 PM -
Key/mouse movement
By falkon114 in forum New To JavaReplies: 3Last Post: 02-21-2011, 02:12 AM -
Sprite Movement
By Curtiz in forum Java GamingReplies: 1Last Post: 04-26-2010, 01:31 PM -
Movement of balls
By BlitzA in forum New To JavaReplies: 8Last Post: 01-09-2008, 03:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks