我正在尝试制作塔防游戏。到目前为止,我已经制作了显示所有内容的JFrame
(称为GameFrame
),添加了背景图像(.PNG
),我正在尝试创建怪物并让它们在我创建的路径上“移动”。所以,我创建了一个GlassPane
类,我glassPane
在GameFrame
.
这是代码GlassPane
:
public class GlassPane extends JComponent implements ActionListener{
private ArrayList<MyPoint> path; //list of Points-pixel positions- (x,y pair) that monsters will follow
private ArrayList<Monster> wave; //list of monsters that will try to reach the rift
private Timer timer; //javax.swing.Timer
private boolean waveEnd;//signing that the wave has ended(ignore it)
public GlassPane(){
super();
createPath();//method is below
wave = new ArrayList<Monster>();//monsters added in nextWave()
timer = new Timer(4,this);//timer in order to slow down and smooth the movement of monsters
waveEnd = false;
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
for(Monster m:wave){
if(m.isVisible())//visible is a private variable in Monster class(defines whether i want to paint that monster or not)
{
g.drawImage(m.getImage(), m.getX(), m.getY(), this);
}
}
}
public void nextWave(){
waveEnd = false;
for(int i =0;i<20;i++)
wave.add(new Monster());
wave.get(0).setVisible(true);//sets the first monster to be visible(paintable)
int visibles = 1;//index 0 is visible,so index 1 is next to become visible
while(!waveEnd){
if(visibles<19 && wave.get(0).getPathIndex()%50 == 0){
//meaning until all 20 monsters are visible(indexes 0-19),"every 50 steps(pixels) the first monster moves"
wave.get(visibles).setVisible(true);
//make another monster visible and start moving it
visibles++;//visible(moving) monsters increased by 1
timer.start();//"move all visible monsters one step forward with a small delay until the next movement to make it soft"
}
JOptionPane.showMessageDialog(new JFrame(), "Finished!","All monsters reached the rift!",JOptionPane.INFORMATION_MESSAGE);//let me know then wave is finished
}
private void createPath(){
//just making a lsit of pixels that I want monsters to follow
path = new ArrayList<MyPoint>();
for(int x=0;x<175;x++){
path.add(new MyPoint(x,0));
}
for(int y=0;y<175;y++){
path.add(new MyPoint(174,y));
}
for(int x=175;x<325;x++){
path.add(new MyPoint(x,174));
}
for(int y=175;y<425;y++){
path.add(new MyPoint(299,y));
}
for(int x=325;x<575;x++){
path.add(new MyPoint(x,424));
}
for(int y=424;y>175;y--){
path.add(new MyPoint(574,y));
}
for(int x=575;x<1001;x++){
path.add(new MyPoint(x,174));
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
for(Monster m:wave)
if(m.isVisible())
m.move(path);
//"move every visible monster by one step"
if(m.get(19).getPathIndex() == 1674)//1674 is the path's last index
waveEnd = true;//if the last monster reaches the end,then all have
//meaning the wave is finished
this.repaint();//refresh any changes to the monsters' positions
}
}
问题是只有第一个怪物在路径中移动,这意味着当我调用时只有一个被绘制repaint()
。我在 p 做错了aintComponent(Graphics g)
什么?因为我相信那是我的错误所在……我怎样才能一次又一次地绘制所有“可用”图像?
.PNG
注意:如果这很重要,它们是格式的缓冲图像。
我虽然关于添加Monster
类代码,但我相信解释方法的功能足以理解发生了什么。如果有人需要更多详细信息,请告诉我。提前致谢。