Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to move my player when clicking the right button on the mouse. The problem is that my player is just standing and "shaking" in the top left corner. enter image description here

Any idea?

package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;

public class Player {
    private Rectangle playerRec;
    float x, y;
    PlayerJLabel playerJLabel;
    int worldX = 800;
    int worldY = 600;
    int targetX, targetY;


    public Player(int posX, int posY, int size){
        playerRec = new Rectangle(posX, posY, size, size);
        x = posX;
        y = posY;

        playerJLabel = new PlayerJLabel();
        playerJLabel.setBounds(0, 0, 800, 600);

        Game.screen.add(playerJLabel);
    }


    public void update(float timeSinceLastFrame){

        if (Mouse.isMouse(MouseEvent.BUTTON3)) {
            targetX = Mouse.getX();
            targetY = Mouse.getY();
        }

        if ((targetX != 0 && targetX != x) && (targetY != 0 && targetY != y)) {
            float dx = (float) (targetX-x);
            float dy = (float) (targetY-y);

            // normalize
            float length = (float) Math.sqrt(dx*dx+dy*dy);
            dx/=length;
            dy/=length;

            // add speed
            x = dx * timeSinceLastFrame * 300;
            y = dy * timeSinceLastFrame * 300;

            // Move the player
            playerRec.x = (int) x;
            playerRec.y = (int) y;          

        }

    }


    private Rectangle getRec(){
        return playerRec;
    }


    public class PlayerJLabel extends JLabel{
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.fillRect(getRec().x, getRec().y, getRec().width, getRec().height);
        }
    }

}

Edit: I have a GameState method which calls the player update() method and a repaint() method which repains the whole scene.

GameState update method:

@Override
public void update(float lastFrame) {
    player.update(lastFrame);
}

Game repaint method:

public void repaint() {
            // screen is a JFrame Object
    screen.repaint();
}

Main class

package game;

public class Main {

    public static void main(String[] args) {
        Game game = new Game("Mein erstes Spiel", 800, 600);

        long lastFrame = System.nanoTime();

        while(game.isRunning()) {
            long thisFrame = System.nanoTime();
            float timeSinceLastFrame = ((float)(thisFrame-lastFrame))/100000f;
            lastFrame=thisFrame;

            game.nextState();
            game.update(timeSinceLastFrame);
            game.repaint();
        }

    }

}

Update 2:

I updated my code to:

        // Move the player
        x = playerRec.x += (int) (dx * timeSinceLastFrame * 500);
        y = playerRec.y += (int) (dy * timeSinceLastFrame * 500);

My player is moving, but not at the same speed and not very smooth: http://screencast.com/t/pv5M9Un4QhFR

share|improve this question
    
we'll have a hard time answering without your draw code and the code that sets everything up in the first place. –  PlayDeezGames Oct 3 '13 at 20:45
    
Updated my question. –  Chris Oct 3 '13 at 20:53
    
For why it isn't very smooth record your frame rate and see if it is nice and even or not. When you see the jumps in movement my guess is it is after a frame rate hickup aka timesincelastframe got large. You might want to move to a fixed update rate. –  stonemetal Oct 4 '13 at 14:27

1 Answer 1

up vote 5 down vote accepted

I do know.

    // add speed
    x = dx * timeSinceLastFrame * 300;
    y = dy * timeSinceLastFrame * 300;

    // Move the player
    playerRec.x = (int) x;
    playerRec.y = (int) y

Is effectively the same as saying

    // Move the player
    playerRec.x = (int) (dx * timeSinceLastFrame * 300);
    playerRec.y = (int) (dy * timeSinceLastFrame * 300);

You may instead wish to do:

    // Move the player
    playerRec.x += (int) (dx * timeSinceLastFrame * 300);
    playerRec.y += (int) (dy * timeSinceLastFrame * 300);

You needed +=, not =.

Happens to me all the time.

share|improve this answer
    
I'll try that tomorrow. Thanks in advance. –  Chris Oct 3 '13 at 22:39
    
Updated my question with the result. –  Chris Oct 4 '13 at 9:28
    
Thanks that worked for me now (updated my frameRateTime). One more problem: The player is not moving directly to the point. See it here screencast.com/t/5lvmwDHSdGtg –  Chris Oct 4 '13 at 15:49
    
and THAT is because you aren't taking into account that the amount you are updating the position may take you PAST where the position is.... so you should do a distance check, and don't move more than the total distance. –  PlayDeezGames Oct 4 '13 at 16:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.