2013-02-04 71 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