• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Switch statement, only using one case at a time  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

       
 
Bartender
Posts: 703
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is "p"? What does setTranslateX() do? You call setTranslateX with an argument of p.getX())+5,
but do you ever set the value of x to that ?
 
Will Isom
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:What is "p"? What does setTranslateX() do? You call setTranslateX with an argument of p.getX())+5,
but do you ever set the value of x to that ?


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.
 
Marshal
Posts: 24467
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 5987
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!