Hello there..
Any ideas as to how can i move objects?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hello there..
Any ideas as to how can i move objects?
By velocity do you mean the number of pixels moved per unit of time?i would like to set the velocity of the object
For example: 10 pixels/second
You will need to use a timer to control the time between new positions.
The x,y coordinates java uses for plotting have x increasing to the right (decreasing to the left)
and y increases going down (decreases going up).
If you don't understand my answer, don't ignore it, ask a question.
Yes..sorry, I wasn't precise..i meant the number of pixels moved per unit of time as you said
--- Update ---
Could you please write an example for me? like a code example? so i could follow it for the rest of the directions as well..
How does the code control the time and the distance that the shape moves?
An example for moving 10 pixels/sec to the left :
If starting location is 100, 10 (x,y)
then one second later
set the location to 90,10
and one second later
set the location to 80,10
and one second later
set the location to 70,10
etc
If you don't understand my answer, don't ignore it, ask a question.
There is not enough information about what goLeft() should do to make any recommendations.
Where is the code that controls the displaying of the shape that is to move?
If the shape is moving using the normal java x,y coordinates, then to go left you would decrease the x value.
x--; // decrease the x value by 1
If you don't understand my answer, don't ignore it, ask a question.
If you have Netbeans and JavaFX in your enviroment, then give that a try:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ztest; import javafx.animation.PathTransition; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.FlowPane; import javafx.scene.layout.Pane; import javafx.scene.layout.StackPane; import javafx.scene.shape.Circle; import javafx.scene.shape.HLineTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.stage.Stage; import javafx.util.Duration; /** * * */ public class ZTest extends Application { private PathTransition pathTransition; private static final int WIDTH_WINDOW = 500; private static final int HEIGHT_WINDOW = 100; /** * Pure data object. Resposible for holding the values of a velocity */ public static class VelocityInPixelsPerSecond { private final int pixels; private final int seconds; public static VelocityInPixelsPerSecond getInstance(int _pixels, int _seconds) { return new VelocityInPixelsPerSecond(_pixels, _seconds); } private VelocityInPixelsPerSecond(int _pixels, int _seconds) { this.pixels = _pixels; this.seconds = _seconds; } public int getPixels() { return pixels; } public int getSeconds() { return seconds; } } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } /** * Called by JavaFC application * @param primaryStage */ @Override public void start(Stage primaryStage) { primaryStage.setTitle("Velocity Example"); Pane root = new FlowPane(); VelocityInPixelsPerSecond velocity = VelocityInPixelsPerSecond.getInstance(400, 2); Node node = iniCircle(velocity); root.getChildren().add(node); primaryStage.setScene(new Scene(root, WIDTH_WINDOW, HEIGHT_WINDOW)); primaryStage.show(); pathTransition.play(); } /** * * Create a Node/Circle. Cirlce extends Node indirectly. * * @param _velocity Param Object for the velocity * @return a Circle, which is added to the class var pathTransition */ private Node iniCircle(final VelocityInPixelsPerSecond _velocity) { //Create the circle Circle rect = new Circle(); rect.setRadius(20); //Create the path, where the circle will move Path path = new Path(); path.getElements().add(new MoveTo(10, 10)); // the lenght of the way of the path, setted by the pixels of velocity path.getElements().add(new HLineTo(_velocity.getPixels())); // Create a special JavaFX object, it 'controls' the movement pathTransition = new PathTransition(); //set the duration of the movement pathTransition.setDuration(Duration.seconds(_velocity.getSeconds())); pathTransition.setPath(path); pathTransition.setNode(rect); pathTransition.setCycleCount(Timeline.INDEFINITE); return rect; } }
It moves to the right, though but I hope it helps
Where is the object being drawn on the GUI? To move the object to the left, decrement its x location value.
If you don't understand my answer, don't ignore it, ask a question.
As far as I can see, the methods of GameObject class are intended to set the the position and velocity variables of the GameObject. These variables should later be used in the update() abstract method.
So, the implementation of these setters is pretty straightforward:
However, here are few remarks. At first, these methods must not be static. If they are static, than you will not be able to set the object-scope variables vx and xy.public void setVelocityY(double f) { vy = f; } public void setVelocityX(double f) { vx = f; }
Second, vx and vy are double, and the f is float. I recommend to use the same type for both, 'cause if you don't, than in future you may encounter some errors with precision (namely, float is less precise than double).
Third, I can't understand, what's the purpose of setVelocityX1()'s existence
Once you've implemented all the GameObject methods (btw, you'll need to subclass it, 'cause it's an abstract class), you can implement ControllableObject methods.
First, it would be quite reasonable to provide a constructor with the objectToWrap argument - for simplicity.
The setter method, setObject() has exactly the same body as this constructor.public ControllableObject(GameObject objectToWrap) { object =objectToWrap; }
The implementation of goLeft() is also straightforward. Namely, you just have to set the GameObject's velocity of X axis to some negative value (if the positive direction of the X axis is directed rightwards):
Here, I've added one argument, double speed. It's for convenience, using it, you can specify the exact velocity.public void goLeft(double speed) { object.setVelocityX(-speed); }
Finally, the stop() method's job is just to set all the speeds to 0:
public void stop() { object.setVelocityX(0); object.setVelocityY(0); }
Also, I recommend you to read the official java tutorial and to use Java API Javadocs for exclusive description of all the standard classes.