Eu fiz este jogo mas quando eu executo, e movimento com o teclado a sprite fica travando na tela.
o que eu devo fazer.
segue o codigo da janela. DESCULPE TINHA MANDADO ERRADO
package rtype;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class RType extends JFrame {
public RType() {
add(new Board());
setSize(800, 600);
setTitle("Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
Toolkit toolkit = getToolkit();
Dimension size = toolkit.getScreenSize();
setLocation(size.width/2 - getWidth()/2,
size.height/2 - getHeight()/2);
}
public static void main(String[] args) {
RType cos = new RType();
cos.setVisible(true);
}
}
E[b]sta classe representa uma nave espacial e eventos teclado etc.[/b]
package rtype;
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class Craft {
private String craft = "craft.png";
private int dx;
private int dy;
private int x;
private int y;
private Image image;
public Craft() {
ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
image = ii.getImage();
x = 10;
y = 10;
}
public void move() {
x += dx;
y += dy;
}
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_RIGHT) {
dx = -3;
}
if (key == KeyEvent.VK_LEFT) {
dx = 3;
}
if (key == KeyEvent.VK_DOWN) {
dy = -3;
}
if (key == KeyEvent.VK_UP) {
dy = 3;
}
}
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;
}
}
}
esta e a classe board
package rtype;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {
private Timer timer;
private Craft craft;
public Board() {
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
craft = new Craft();
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();
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
craft.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
craft.keyPressed(e);
}
}
}