所以我试图在LWJGL和Slick2D中制作一个滚动系统,按顺序上下滚动项目:
import java.util.ArrayList;
import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
/**
* @author michael_daley
*
* Class which allows any number of high score entries to be loaded with an avatar.
* This list is then scrolled up the screen
*/
public class HighScores {
private int x;
private int y;
private float scrollSpeed;
private UnicodeFont font;
private ArrayList<HighScore> highScores;
/**
* Constructor
*
* @param x
* @param y
* @param scrollSpeed Speed at which to scroll the high scores
* @throws SlickException
*/
public HighScores(int x, int y, float scrollSpeed) throws SlickException{
this.x = x;
this.y = y;
this.scrollSpeed = scrollSpeed;
/* Set up the font we are using */
font = ScreenDashboardHome.font;
highScores = new ArrayList<HighScore>();
}
/**
* Add a high score entry to the HighScore object
*
* @param score Text to be printed in the high score table
* @param avatar Image to be displayed in the high score table
* @param spacing Spacing to be applied between each high score entry
*/
public void addScore(String score, Image avatar, int spacing) {
HighScore hs = new HighScore(x, y+ (highScores.size() * spacing), scrollSpeed, font, score, avatar);
highScores.add(hs);
}
/**
* Update the individual high scores which will update their location
*
* @param delta milliseconds since last update passed from the main Update method
*/
public void update(int delta, boolean direction) {
for (int i=0; i < highScores.size();i++) {
HighScore hs = highScores.get(i);
hs.Update(delta, direction);
}
}
/**
* Render the high scores to the graphics context
*
* @param g Graphics context to which items should be rendered
*/
public void render(Graphics g) {
for (int i=0; i < highScores.size();i++) {
HighScore hs = highScores.get(i);
hs.Render(g);
}
}
/**
* @author michael_daley
*
* Inner Class which is used to store information on each high score and perform an
* update on that high score which will change its location on the screen
*/
public class HighScore {
private String score;
private Image avatar;
private float x;
private float y;
private float speed;
private UnicodeFont font;
private Color c;
private Color c2;
public HighScore(float x, float y, float speed, UnicodeFont font2, String score, Image avatar) {
this.font = font2;
this.x = x;
this.y = y;
this.speed = speed;
this.avatar = avatar;
this.score = score;
this.c = new Color(255, 255, 255, 1.0f);
}
/**
* Update this highscore which will change its location and alpha
*
* @param delta milliseconds since last update
*/
public void Update(int delta, boolean direction) {
if(direction == true){
y -= speed;
}else if(direction == false){
y += speed;
}
if (y <= 300) {
y = 1100;
}
if (y >= 1200) {
y = 299;
}
System.out.println(y);
/* Adjust the alpha of scores entering the screen */
/* TODO: Come up with a better way to fade scores onto and off the screen */
if (y > 800) {
c = new Color (255, 255, 255, (600 - y) / 200);
} else {
c = new Color(255, 255, 255, 1.0f);
}
if (y > 800) {
c2 = new Color(256,256,256,(600 - y) / 200).multiply(new Color(0,160,209));
} else {
c2 = new Color(0,160,209);
}
}
/**
* Render the highscore to the graphics context
*
* @param g Graphics context to render too
*/
public void Render(Graphics g) {
g.setColor(c);
g.drawRect(x, y, 500, 170); // tweet 1
g.setColor(c2);
g.fillRect(x + 1, y + 1, 499, 169);
g.setColor(c);
g.drawRect(x + 1, y + 1, 82, 82); // tweet 1
avatar.getScaledCopy(80, 80).draw(x + 2, y + 2, c);
font.drawString((x + avatar.getWidth()) + 15, y, score, c);
g.setColor(Color.white);
}
}
}
然而它堆叠起来像这样: 任何人都能解释我做错了什么,并为我提供了一些更好的代码吗?