2016-03-26 21 views
0

我對slick 2d有點新,在我的項目中,我正在玩弄按鈕等東西。我會畫一幅圖像,並在座標處點擊一下,最後看起來就像50行長,看起來很不必要。slick 2d中如何使用抽象組件類?

但後來我看到這個線程Slick 2d Button Listener;可悲的是它死了,所以沒有幫助。這個人正在做什麼似乎非常有效,但到目前爲止我一直無法找到任何有關如何使用它的信息。如果任何人都可以向我解釋或告訴我如何使用它,這將是非常棒的。

回答

0

我實現了一個按鈕,像這樣的油滑2D

protected Shape hitbox;  
private boolean inside = false;  

public void render(GameContainer container, Graphics g) { 
    . 
    .//drawing the picture of the button 
    . 
    if(inside && container.getInput().isMousePressed(Input.MOUSE_LEFT_BUTTON)){ 
     //someone clicked the button 
    } 
} 
public void init(GameContainer container) { 
    //setting a hitbox up to recognize when the mouse is over the picture/button 
    hitbox = new Rectangle(/*dimensions of your picture*/); 
} 


public void update(GameContainer container, int delta) { 
    //this code finds out if the mouse is over the button 
    int mouseX = container.getInput().getMouseX(); 
    int mouseY = container.getInput().getMouseY(); 
    inside = hitbox.contains(mouseX, mouseY)) 
} 
+0

是的這將工作,但我想要做的是創建一個新的組件,所以我可以用初始化方法初始化它與幾個參數,並呈現在渲染方法。儘管我幾乎可以工作。 – Hamish

+0

您可以創建一個專用陣列。 在初始化時,您可以創建組件並將它們添加到數組中。 在渲染方法中,您遍歷數組中的每個組件並調用這些組件的渲染函數。 – Alex

0

一些測試,我發現我怎麼能做到這一點後,在按鈕類我寫道:

import org.newdawn.slick.Color; 
import org.newdawn.slick.Graphics; 
import org.newdawn.slick.Input; 
import org.newdawn.slick.SlickException; 
import org.newdawn.slick.geom.Rectangle; 
import org.newdawn.slick.geom.Shape; 
import org.newdawn.slick.gui.AbstractComponent; 
import org.newdawn.slick.gui.GUIContext; 

public class Button extends AbstractComponent{ 

protected int width; 
protected int height; 
protected int x; 
protected int y; 
protected int mouseX; 
protected int mouseY; 
protected boolean pressed; 
protected Shape hitbox; 
protected boolean over; 
protected String text; 

public Button(GUIContext container, int x, int y, int width, int height, String text) { 
    super(container); 
    setLocation(x, y); 
    this.width = width; 
    this.height = height; 
    this.text = text; 
    hitbox = new Rectangle(x, y, width, height); 
} 

@Override 
public int getHeight() { 
    return height; 
} 

@Override 
public int getWidth() { 
    return width; 
} 

@Override 
public int getX() { 
    return x; 
} 

@Override 
public int getY() { 
    return y; 
} 

public boolean isPressed() { 
    return pressed; 
} 
public void update(GUIContext container) { 
    mouseX = container.getInput().getMouseX(); 
    mouseY = container.getInput().getMouseY(); 
    over = hitbox.contains(mouseX, mouseY); 

    if (over && container.getInput().isMousePressed(Input.MOUSE_LEFT_BUTTON)) { 
     pressed = true; 
    }else{ 
     pressed = false; 
    } 
} 

@Override 
public void render(GUIContext container, Graphics g) throws SlickException { 
    g.setColor(Color.blue); 
    g.fillRect(x, y, width, height); 
    g.setColor(Color.black); 
    g.drawString(text, x + width/2, y + height/2); 
} 
@Override 
public void setLocation(int x, int y) { 
    this.x = x; 
    this.y = y; 
} 

,並使用它在我的框架,我只是寫:

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 Menu extends BasicGameState{ 
    public static final int ID = 1; 
    private StateBasedGame game; 
    private Button button; 

    public int getID() { 
     return ID; 
    } 

    @Override 
    public void init(GameContainer container, StateBasedGame game) throws SlickException { 
    this.game = game; 
    button = new Button(container, 1280/2, 720/2, 200, 200, "Works"); 
    } 

    @Override 
    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { 
    button.render(container, g); 
    } 

    @Override 
    public void update(GameContainer container, StateBasedGame game, int delta) { 
    button.update(container); 
    if (button.isPressed()) { 
     System.out.println("Works"); 
    } 
} 

}