This site requires JavaScript, please enable it in your browser!
Greenfoot back
joshuadswa
joshuadswa wrote ...

2017/2/18

Movement problem

joshuadswa joshuadswa

2017/2/18

#
Hello, i have some problem here, so the code work but some key are unable to activate when "x" key is pressed .-. is theres something wrong with my code? Thx :) public void heroMovement() { int x = getX(); int y = getY(); if(Greenfoot.isKeyDown("Up")) { setLocation(x,y-5); } if(Greenfoot.isKeyDown("Down")) { setLocation(x,y+5); } if(Greenfoot.isKeyDown("Left")) { setLocation(x-5,y); } if(Greenfoot.isKeyDown("Right")) { setLocation(x+5,y); } }
Super_Hippo Super_Hippo

2017/2/18

#
I don't see anything in the code causing the X key having any effect. But in some games (not related to Greenfoot), I see that one for example can't shoot when walking up and left, but it is possible when walking down and left. I am not sure if a bad written code is causing that or the keyboard key pressed detection.
joshuadswa joshuadswa

2017/2/18

#
i mean the "x" can be Up Left Right or Down >.< not the X its self >.< sorry to cause ambiguous here
Super_Hippo Super_Hippo

2017/2/18

#
Right now, you should only be able to move in one direction at once. Let's say the object is at (100 | 100) and you press Up and Right. x and y are set to 100. It detects the Up key and the object moves to (x | y-5) = (100 | 100-5) = (100 | 95). Then it also detects the Right key, so it moves to (x+5 | y) = (100+5 | 100) = (105 | 100). x and y are not updating after the first location change. If you would like to end up at (105 | 95) when Up and Right are pressed, you could use this:
int dx=0, dy=0;
if (Greenfoot.isKeyDown("up")) dy--;
if (Greenfoot.isKeyDown("down")) dy++;
if (Greenfoot.isKeyDown("right")) dx++;
if (Greenfoot.isKeyDown("left")) dx--;
setLocation(getX()+dx*5, getY()+dy*5);
If you want diagonal movement to be "slower" than horizontal and vertical (so more realistic), you can change the 5 to a 4 when dx and dy are not equal to 0. If it is a SmoothMover, you don't have to use 4, but you can use the real speed which is 5/sqrt(2) = about 3.5355.
joshuadswa joshuadswa

2017/2/18

#
o. o okay i understand why my code doesn't work now xD thx a lot, but can you explain a little about your code? well im new here and need more knowledge of Java and greenfoot xD, about dx and why we must set it location. .-.
Super_Hippo Super_Hippo

2017/2/18

#
If you would change all 'x' to 'getX()' and 'y' to 'getY()' in your code, you would have the same result. The difference is that in my code, the object is set to its destination once while in your code, the object sets a new location for every keypress. dx and dy are there to see in which direction the object should move. '++' is adding one, '--' is subtracting one. So if you press Up and Right, dy will be -1 and dx will be 1. The last line multiplies the direction with the speed (5) and sets the new location accordingly. In the Up and Right example from (100 | 100), it would be 100+1*5=105 for the x-coordinate and 100-1*5=95 for the y-coordinate.
joshuadswa joshuadswa

2017/2/19

#
Okay thank you very much ^^ have a nice day sir
You need to login to post a reply.