我试图制作一个游戏,当物体撞击你时你死了,但我没有使用MouseEntered方法。有人能告诉我怎么样?我也想问一下加载所有的精灵图像。我成功地将它们加载但未能使岩石从图像变为图像。
测试者课程
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Tester extends JPanel{
public JFrame window = new JFrame("Rock stuff");
public Timer tmr ;
public ArrayList<Rock> rocks = new ArrayList<Rock>();
public Tester(){
window.setBounds(500, 400, 500, 500);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.add(this);
tmr = new Timer(1, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(Rock r:rocks)
r.move();
repaint();
}
});
this.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) { }
@Override
public void mousePressed(MouseEvent e) {
for(Rock r:rocks) {
if(r.contains(e.getPoint())){
Rock newRock = r.split();
if(newRock != null){
rocks.add(newRock);
rocks.add(newRock);
}
else{
rocks.remove(r);
if(rocks.size()==0){
tmr.stop();
repaint();
JOptionPane.showMessageDialog(window, "Game Over");
}
}
return;
}
}
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
for(Rock r:rocks){
if(r.contains(e.getPoint())){
rocks.clear();
JOptionPane.showMessageDialog(window, "Game Over");
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
});
addrock(10);
tmr.start();;
}
private void addrock(int count) {
Random rnd = new Random();
for(int i=0;i<count;i++)
rocks.add(new Rock( rnd.nextInt(getWidth()),
rnd.nextInt(getHeight()),
32,32,
(rnd.nextBoolean()?1:-1),
(rnd.nextBoolean()?1:-1),
true
) );
}
public void paint(Graphics g){
super.paint(g);
Rock.sw = getWidth();
Rock.sh = getHeight();
for(Rock r:rocks) r.draw(g);
}
public static void main(String[] args) {
new Tester();
}
}
Rocks类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
public class Rock extends Rectangle{
public static int sw, sh;
public int dx, dy;
public boolean isMoving;
public BufferedImage sprite;
//================================================= Constructors
public Rock(int x, int y, int width, int height, int dx, int dy, boolean isMoving) {
super();
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.isMoving = isMoving;
this.sprite = init();
}
//================================================= Methods
public BufferedImage init(){
BufferedImageLoader loader = new BufferedImageLoader();
BufferedImage spriteSheet = null;
try {
spriteSheet = loader.loadImage("hero.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SpriteSheet ss = new SpriteSheet(spriteSheet);
sprite = ss.grabSprite(0, 0, 32, 32);
return sprite;
}
public void move() {
if(isMoving == false) return;
x += dx;
y += dy;
if(x + width > sw) {
x = sw - width;
dx = -dx;
}
if(y + height > sh) {
y = sh - height;
dy = -dy;
}
if(x < 0) {
x = 0;
dx = -dx;
}
if(y < 0) {
y=0;
dy = -dy;
}
}
public Rock split(){
Rock r = null;
Random rnd = new Random();
if(width > 20){
dx=(rnd.nextBoolean()?1:-1);
dy=(rnd.nextBoolean()?1:-1);
r = new Rock(x,y,width,height,dx,dy,true);
}
return r;
}
public void draw(Graphics g) {
g.drawImage(sprite,x,y,null);
}
}
答案 0 :(得分:2)
mouseEntered
问题的基本答案是......
mouseEntered
,当鼠标继续在组件内时,将永远不会调用它。
相反,也许可以考虑使用MouseMotionListener#mouseMoved
来判断鼠标移动到组件上的时间。
你的第二个问题需要更多代码,特别是SpriteSheet
类,但我怀疑你需要知道你正在绘制的当前帧,只需使用SpriteSheet#grabSprite
来获取帧的相应图像。
这需要您知道:
这将允许您计算帧,并从中计算您应该显示的图像
作为一个粗略的例子,您可以查看How to Draw an BufferedImage to a JPanel