)请帮忙解决问题。我正在做一个小游戏并给它画一枚硬币)给玩家去收集它们。
以下是收集过程(该代码):
if (x + w3 == 190) {
coins++;
mon = null;
} else {
g.drawImage(mon, 190 - x, 265, wm, hm, this);
}
其中190 - 是硬币的位置
其中x + w3是玩家的坐标
也就是说,当触摸硬币的坐标消失,但随着这个硬币消失,其他人,实际上 - mon = null;
我该怎么办?真的为每张硬币制作你的照片吗?
提前感谢。
UPD
所有代码:
package GameTs;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Game extends JPanel {
private static final long serialVersionUID = 1L;
public static final String NAME = "DrakGo Game";
public static final int WIDTH = 500;
public static final int HEIGHT = 460;
private static final int MAP_SIZE_X = 100;
private Image img;
private Image img2;
private Image mon;
private Image mon2;
private Image mon3;
private Image pl;
private Image mag;
private Image drak;
private Input input = new Input(this);
static JFrame frame;
static int x = 0;
int y = 0;
int xp = 110;
int yp = 180;
int coins = 0;
URL url4;
URL url7;
public Game() {
URL url2 = getClass().getResource("images/Grass.png");
img = Toolkit.getDefaultToolkit().getImage(url2);
URL url = getClass().getResource("images/Cloud.png");
img2 = Toolkit.getDefaultToolkit().getImage(url);
URL url3 = getClass().getResource("images/Mon.png");//THIS
mon = Toolkit.getDefaultToolkit().getImage(url3);
mon2 = Toolkit.getDefaultToolkit().getImage(url3);//THIS
mon3 = Toolkit.getDefaultToolkit().getImage(url3);//THIS
url4 = getClass().getResource("images/Right_Pic1.png");
URL url5 = getClass().getResource("images/Mag.png");
URL url6 = getClass().getResource("images/Dr.png");
url7 = getClass().getResource("images/Right_Pic2.png");
pl = Toolkit.getDefaultToolkit().getImage(url4);
mag = Toolkit.getDefaultToolkit().getImage(url5);
drak = Toolkit.getDefaultToolkit().getImage(url6);
}
public void paint(Graphics g) {
render(g);
repaint();
}
private void move(Graphics g) {
/*
* if (input.left) { x--; if (x == -1) { x++; }
*
* }
*/
if (input.right) {
x++;
if (x == -1) {
x--;
}
if ((x % 2) == 0) {
pl = Toolkit.getDefaultToolkit().getImage(url7);
} else {
pl = Toolkit.getDefaultToolkit().getImage(url4);
}
}
}
public void render(Graphics g) {
super.paint(g);
move(g);
g.setColor(Color.BLUE);
g.fillRect(0, 0, 500, 360);
int w = 180;
int h = 180;
for (int i = 0; i < MAP_SIZE_X * 2; i++) {
g.drawImage(img, i * w - x, 260, (int) w, (int) h, this);
}
int w2 = 90;
int h2 = 50;
for (int c = 0; c < MAP_SIZE_X * 100; c += 200) {
g.drawImage(img2, 100 + c - x, 70, w2, h2, this);
}
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
Font[] allFonts = ge.getAllFonts();
Font font = allFonts[4].deriveFont(30.0F);
g.setFont(font);
g.setColor(Color.DARK_GRAY);
g.drawString("DrakGo - Game", 10, 40);
g.drawString("X: " + x, 300, 40);
g.drawString("Coins: " + coins, 400, 40);
int w3 = 50;
int h3 = 50;
int wm = 30;
int hm = 30;
if (x + w3 == 190) {
coins++;
mon = null;
} else {
g.drawImage(mon, 190 - x, 265, wm, hm, this);
}
//THIS!!!!!!!
if (x + w3 == 350) {
coins++;
mon2 = null;
} else {
g.drawImage(mon2, 350 - x, 265, wm, hm, this);
}
if (x + w3 == 650) {
coins++;
mon3 = null;
} else {
g.drawImage(mon3, 650 - x, 265, wm, hm, this);
}
int w4 = 50;
int h4 = 50;
if (x + w3 == 3500) {
drak = null;
} else {
g.drawImage(drak, 3500 - x, 260, w4, h4, this);
}
g.drawImage(mag, 1000 - x, 260, w3, h3, this);
g.drawImage(pl, 0, 260, w3, h3, this);
}
public static void main(String[] args) {
Game game = new Game();
game.setSize(WIDTH, HEIGHT);
frame = new JFrame(NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.add(game);
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
}
}
答案 0 :(得分:0)
删除它:
public void paint(Graphics g) {
render(g);
repaint();
}
而是使用它:
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
render(g);
}
同样改变:
public void render(Graphics g) {
super.paint(g);
move(g);
// ..
要:
public void render(Graphics g) {
move(g);
// ..
这两种方法都会导致无限循环。