Reputation: 10021
I've been learning how to work with rpg style games in java and found a great tutorial. However, the tutorial shows how to move a sprite using a key listener, what I want to do is make the sprite move by getting a mouse location on the screen, and moving the sprite on its own without any key listeners. heres the code i have so far, thanks for the help! (oh, the problem is the sprite is not moving, forgot to say that... is it because of the while loop in the move method?)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Image;
import java.awt.event.*;
import javax.swing.ImageIcon;
public class SpaceCraft {
private String craft = "craft.png";
private int dx;
private int dy;
private int x;
private int y;
private Image image;
public SpaceCraft() {
ImageIcon spaceCraft = new ImageIcon(this.getClass().getResource(craft));
image = spaceCraft.getImage();
x = 40;
y = 60;
}
public void move() {
while ((x==Board.mouseX)==false) {
if (x < Board.mouseX){
x += 1;
} else if (x > Board.mouseX){
x -= 1;
}
}
while ((y==Board.mouseY)==false) {
if (y < Board.mouseY){
y += 1;
} else if (y > Board.mouseY){
y -= 1;
}
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return image;
}
/* public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT){
dx = -1;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 1;
}
if (key == KeyEvent.VK_UP) {
dy = -1;
}
if (key == KeyEvent.VK_DOWN) {
dy = 1;
}
}*/
/* public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}
if (key == KeyEvent.VK_UP) {
dy = 0;
}
if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}*/
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {
public static int mouseX;
public static int mouseY;
private Timer timer;
private SpaceCraft craft;
public Board() {
//addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.GREEN);
setDoubleBuffered(true);
craft = new SpaceCraft();
timer = new Timer(5, this);
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e) {
craft.move();
repaint();
}
public void mouseReleased(MouseEvent e) {
mouseX = e.getXOnScreen();
mouseY = e.getYOnScreen();
craft.move();
repaint();
}
/*private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
craft.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
craft.keyPressed(e);
}
}*/
}
import javax.swing.*;
import java.applet.*;
import java.awt.Dimension;
import java.net.*;
public class RType extends Applet {
Board b = new Board();
public void init() {
b.setPreferredSize(new Dimension(400, 400));
add(b);
}
}
Upvotes: 0
Views: 150
Reputation: 42176
You need to move your sprite a little bit at a time, painting each step of the way. The way you're doing it now, you don't have any painting as your sprite moves, so it's just going to look like it teleports to the mouse location.
You also have to actually add a MouseListener, not just write the MouseListener methods.
Upvotes: 1
Reputation: 1086
There's a couple approaches you can take.
First, you could log the current position when actionPerformed(ActionEvent e) is called on a mouse event, and keep track of the current X and Y.
Then you just need to make it so that during your game loop, which would have to be in paint() right now, you take the X and Y and apply it to the craft.
So add a call to craft.move(); in paint()
Then if you want to make it even better, instead of logging actionPerformed events, you can directly check the position of the mouse when you call craft.move() instead.
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();
Is one method, although I think that gives you absolute co-ordinates, and you want them relative to your window, right?
Display.getCurrent().getCursorLocation();
or
MouseInfo.getPointerInfo();
Although that just moves the craft exactly to the mouse position. If you want it to have a maximum speed and travel towards the mouse position instead, you'll need to make your craft.move() method more intelligent.
Upvotes: 0
Reputation: 5095
You must add a MouseListener to your Panel Board
.
Change
public class Board extends JPanel implements ActionListener {
to
public class Board extends JPanel implements ActionListener,MouseListener {
In the constructor add
addMouseListener(this);
Upvotes: 2