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

2011/10/6

Using velocity class

rgadue rgadue

2011/10/6

#
Working on a project that requires the following: 7
Duta Duta

2011/10/6

#
You might want to use some code tags, as they will make it a LOT easier to read.
davmac davmac

2011/10/7

#
One problem with your code is in the move() method, where you are calling getX() and getY() methods without storing the result. When you call setLocation you are using the getX and getY variables, whose values have not changed. You are also needlessly calling vel.getXSpeed() and vel.getYSpeed() twice.
rgadue rgadue

2011/10/7

#
How do I store the result?
rgadue rgadue

2011/10/7

#
@Duta, can you give me an example of what you mean about using code tags? Sorry I'm new at this and not getting much help from the teacher of my class.
davmac davmac

2011/10/7

#
Surely your teacher has explained to you how to use variables? You store values using assignment. The general form is: "variable_name = expression;". In this case you could use:
getX = getX();
... because you've declared a variable called "getX" ("private int getX;") and you seem to be using it as if it contains the current location ("setLocation((int) getX + vel.getXSpeed(),(int) getY + vel.getYSpeed());"). The "getX = getX();" statement calls the getX() method, and stores the value it returns into the getX variable. While on the topic, why not change the name of the variable to something sensible - such as simply "x". "getX" is better suited as a method name because it contains a verb / action word. Also, you can declare the variable inside the method, seeing as you only need its value inside the method and you don't need the value to survive between different method calls (because you fetch it each time).
davmac davmac

2011/10/7

#
By the way, I suggest you go through the tutorials on this web site (in the "Documentation" area). They cover basic movement and should help you get a better grasp on things.
rgadue rgadue

2011/10/7

#
Ok, thanks.
Duta Duta

2011/10/7

#
@rgadue I mean place BBCode code tags around the sections of code in your post. If you understand html, its kind of like that - except instead of <code> (whatever you want to put here) </code>, you replace the <>'s with 's. So for instance: Here's a method I made <code> move() </code>, but again change the <>'s. This makes:
 move() 
rgadue rgadue

2011/10/12

#
move()
Oh I see, thanks.
Duta Duta

2011/10/12

#
No problem :)
You need to login to post a reply.