Im trying to translate a square 'p' 5 units left or right depending on what key has been pressed . The problem is that when 'right' or 'left' is pressed it will translate 5 units in that direction but when I press again I can't move and I have to press 'left' to move right again so I never get anywhere.
Does anyone know why this is?
Here's the snippet
Will Isom wrote:Yeah so as I undertsand it 'setTranslateX()' sets the x value to the given value and get(x) returns the x value at the moment in time.
I would have expected that getX() would return the X value and setX(value) would set the X value. I would also have expected that getTranslateX() would return the TranslateX value and setTranslateX(value) would set the TranslateX value, whatever that is. But that's just a guess based on the fact that millions of lines of Java code have been written using those conventions. However I realize that people don't always act reasonably, so you could be right. It would help if you provided more information about those methods, though. Writing code based on guesswork isn't really a good strategy.
If p is a Node (like Rectangle) the setTranslateX() is the correct method to animate it. However, I'm not sure a Task is the correct place to put this. Why not use the setOnKeyPressed() method of the Scene?
All things are lawful, but not all things are profitable.