2012-10-08 83 views
0

好吧,所以當我點擊鼠標時,我的圖片就會沿着y軸向下移動,我唯一的問題是我不知道如何讓它停止點擊屏幕底部,有人可以幫忙嗎?正在停止圖像移動

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; 
    String Build = "1.1"; 

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

    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { 
     for (Point p : point) { 
      if (p != null) { 
       m.drawParticle(p.x, p.y += 1); 
      } 
     } 
     g.drawString("Particle Test", 680, 0); 
     g.drawString("Build: " + Build, 680, 15); 
     g.drawString("Pixels: " + num, 10, 25); 
    } 

    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] = new Point(pressedX, pressedY); 
     } 

    public int getID() { 
     return ID; 
    } 

} 

回答

0

我想象的地方,你會想看看顆粒的X/Y POS您呈示它時,它的出界從數組中刪除它之前...

public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { 
    for (int index = 0; index < point.length; index++) { 
     Point p = point[index]; 
     if (p != null) { 
      p.y++; 
      if (p.y > height) { // You'll need to define height... 
       point[index] = null; // Or do something else with it?? 
      } else { 
       m.drawParticle(p.x, p.y); 
      } 
     } 
    } 
    g.drawString("Particle Test", 680, 0); 
    g.drawString("Build: " + Build, 680, 15); 
    g.drawString("Pixels: " + num, 10, 25); 
} 

你也可以做一個搶先檢查,這將讓你知道什麼點在屏幕的底部...

 if (p != null) { 
      if (p.y >= height) { // You'll need to define height... 
       // Do something here 
      } else { 
       p.y++; 
       m.drawParticle(p.x, p.y); 
      } 
     } 
+0

謝謝,現在你有什麼想法,我會讓它停止向下移動? – user1610541

+0

在第二個例子中,如果'py'等於或大於可用高度,那麼它的y位置不應該再次更新......顯然,如果將它從數組中移除(將數組位置設置爲null),它不會只有永遠不會再移動,它不會被畫;) – MadProgrammer

+0

但我可以讓它保持不動? – user1610541