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.

Results 1 to 8 of 8

Thread: help with java game source code..

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Unhappy help with java game source code..

    Hello there..


    Any ideas as to how can i move objects?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help with java game source code..moving object

    i would like to set the velocity of the object
    By velocity do you mean the number of pixels moved per unit of time?
    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.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: help with java game source code..moving object

    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..

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help with java game source code..moving object

    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.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help with java game source code..moving object

    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.

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: help with java game source code..moving object

    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

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help with java game source code..moving object

    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.

  8. #8
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: help with java game source code..moving object

    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:
    	public void setVelocityY(double f) {
                    vy = f;
    	}
    	public void setVelocityX(double f) {
    	       vx = f; 
    	}
    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.
    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.
    	public ControllableObject(GameObject objectToWrap) {
    		object =objectToWrap;
    	}
    The setter method, setObject() has exactly the same body as this constructor.
    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):
    	public void goLeft(double speed) {
    	        object.setVelocityX(-speed);
    	}
    Here, I've added one argument, double speed. It's for convenience, using it, you can specify the exact velocity.
    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.

Similar Threads

  1. i need help with java source code
    By Daruga in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2013, 07:47 AM
  2. How To Track A Moving Object
    By mahdi.nikoo in forum Algorithms & Recursion
    Replies: 7
    Last Post: November 2nd, 2012, 01:03 PM
  3. How to implement YAHTZEE game in java using switch statements?
    By SeriousJavaHelp in forum Object Oriented Programming
    Replies: 1
    Last Post: October 18th, 2012, 07:46 AM
  4. Java TV Board Game/Grid + Moving objects
    By Massaslayer in forum Java Theory & Questions
    Replies: 6
    Last Post: December 13th, 2011, 08:03 AM
  5. Replies: 3
    Last Post: November 10th, 2011, 07:11 AM