2012-10-08 176 views
0

所以我一直在努力讓我的盒子整天保存陣列中的位置,並最終以爲我想出了一些東西(有很多來自你們的幫助),它只是不工作...有人可以告訴我爲什麼嗎?粒子系統陣列

控制類:

import java.awt.Point; 
import org.newdawn.slick.GameContainer; 
import org.newdawn.slick.Graphics; 
import org.newdawn.slick.SlickException; 
import org.newdawn.slick.state.BasicGameState; 
import org.newdawn.slick.state.StateBasedGame; 

public class Control extends BasicGameState { 
    public static final int ID = 1; 

    public Methods m = new Methods(); 
    public Point[] point = new Point[(800 * 600)]; 

    int pressedX; 
    int pressedY; 
    int num = 0; 

    public void init(GameContainer container, StateBasedGame game) throws SlickException{ 
    } 

    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { 
     m.drawParticle(pressedX, pressedY); 
    } 

    public void update(GameContainer container, StateBasedGame game, int delta) { 
    } 

    public void mousePressed(int button, int x, int y) { 
     pressedX = x; 
     pressedY = y; 
     num = num + 1; 
     point[num].x = pressedX; 
     point[num].y = pressedY; 
     } 

    public int getID() { 
     return ID; 
    } 


} 

方法類:

import org.newdawn.slick.Graphics; 

public class Methods { 

    public Graphics g = new Graphics(); 

    public int sizeX = 1; 
    public int sizeY = 1; 

    public void drawParticle(float x, float y){ 
     g.drawRect(x, y, sizeX, sizeY); 
    } 

} 
+0

請比「不工作」更具體。 –

+0

什麼不起作用? – gigadot

+0

@ user1610541當然:)我們想知道代碼是做什麼的嗎?您是否面臨任何錯誤?給出一些相關的信息,以便於做 – gks

回答

0

雖然你已經初始化點數組的大小,你沒有初始化內容。

public void mousePressed(int button, int x, int y) { 
    pressedX = x; 
    pressedY = y; 
    num++; 
    point[num] = new Point(pressedX, pressedY); 
} 

而且認爲你render方法,你需要重新渲染圖形(我可能是錯誤的,我沒有使用過Slick2D)...

Withing類似...

public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { 
    for (Point p : point) { 
     if (p != null) { 
      m.drawParticle(p.x, p.y); 
     } 
    } 
    m.drawParticle(pressedX, pressedY); 
} 

我也好奇你創建你自己的Graphics,尤其是當render方法傳遞一個你,你可能要檢查到進一步,並確保這是可以接受的...

+0

非常感謝!你幫助我終於得到這個:) – user1610541