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

2016/1/8

problem with code

mollmatt2 mollmatt2

2016/1/8

#
hi all I have tried to make some code that will slow down my character to a stop once I have taken my hand off the movement key. I'm not to sure were I have gone wrong. there is probably a better way of doing this but this is what I came up with at the time, so much help would be appreciated. thanks guys public void MovementLeft() { int counter = 0; if (Greenfoot.isKeyDown("a")) { setLocation(getX()-4,getY()); counter = 25; while (counter > 0) { if (counter <= 25 && counter > 20) { setLocation(getX()-4,getY()); counter --; } if (counter < 20 && counter > 15) { setLocation(getX()-3,getY()); counter --; } if (counter < 15 && counter > 10) { setLocation(getX()-2,getY()); counter --; } if (counter < 10 && counter > 5) { setLocation(getX()-1,getY()); counter --; } if (counter <5 && counter > 0) { setLocation(getX()+0,getY()); counter --; } } } }
danpost danpost

2016/1/8

#
Using a 'while' loop does not allow any act cycles to process -- putting it another way, the 'while' loop will process completely during a single call to the method which is done in one instance in time. Your actor is probably jumping to the left instantly to a stop. The condition for this to happen is if the 'a' key is down; however, you said "once I have taken my hand off the movement key". This is a contradiction which needs clarifying. At any rate, to help make the code process over multiple instances in time, you would start by changing 'while' to 'if' and moving 'int counter = 0;' to outside the method. The initial 'if' block would need to be somewhere else as well, as you cannot keep setting the value of counter to '25'. This may require that you detect not the state of the key, but the changes in the state of the key. It appears that the 'counter' variable is to represent 'speed' in a way (using a ratio between pixels and its value). The name, 'counter', could be made to describe its use a little better. Instead of all the individual 'if' conditions to check the value of 'counter', you could just 'setLocation(getX()-(counter/5), getY());'. I will presume that, since you have a MovementLeft method, you have a MovementRight method as well -- and you are using the 'd' key for movement in that direction. Both deal with movement along the horizontal and to work properly, one should know what the other is doing (that is, the action to take as far as movement horizontally, should take into account the state of both keys). My suggestion is to scratch the 'MovementLeft' and 'MovementRight' methods add use a 'movementHorizontally' method with a 'speed' (or 'speedX') field declared outside the method. Determine the states of the keys and code the movement for what will happen at each instance in time.
mollmatt2 mollmatt2

2016/1/9

#
Thanks so much Danpost. I did what you suggested and I got it to work great!! thanks again.
You need to login to post a reply.