2013-02-04 37 views
0

我正在尝试制作塔防游戏。到目前为止,我已经制作了JFrame(称为GameFrame),其中显示了所有内容,添加了背景图像(.PNG),并且正在尝试创建怪物并使它们“移动”到已创建的路径上。所以,我创建了一个GlassPane类,我在GameFrame中将其设置为glassPane重写paintComponent();绘制多个缓冲图像

下面是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类代码,但我相信解释方法的功能足以了解发生了什么。如果有人需要更多的细节让我知道。提前致谢。

回答

0

我认为你必须在你的循环周围放置鬼脸。 速记只遍历循环

//Output: 
//AAAAAAAAAAAAAAAAAA 
//AAAAAAAAAAAAAAAAAA 
//AAAAAAAAAAAAAAAAAA 
//AAAAAAAAAAAAAAAAAA 
//BBBBBBBBBBBBBBBBBB 
for(Monster m:wave) 
System.out.println("AAAAAAAAAAAAAA"); 
System.out.println("BBBBBBBBBBBBBBB"); 


//Output: 
//AAAAAAAAAAAAAAAAAA 
//AAAAAAAAAAAAAAAAAA 
//AAAAAAAAAAAAAAAAAA 
//AAAAAAAAAAAAAAAAAA 
//BBBBBBBBBBBBBBBBBB 
//BBBBBBBBBBBBBBBBBB 
//BBBBBBBBBBBBBBBBBB 
//BBBBBBBBBBBBBBBBBB 
for(Monster m:wave) 
{ 
System.out.println("AAAAAAAAAAAAAA"); 
System.out.println("BBBBBBBBBBBBBBB"); 
} 
+0

似乎并非是问题之后的第一行“因为其实我有一个FOR循环然后在它的内部IF只有一行(如果我有只有一个命令在一个IF,FOR,WHILE等我不必放置括号)。虽然在代码的复制粘贴过程中出现错误,所以命令g.drawImage(....);似乎超出了FOR循环。无论如何,我甚至尝试了你的建议,但仍然没有任何结果。对此感谢!任何其他想法? – oBlivion