我最近开始学习如何使用Slick编写Java游戏。 我已经看过一些教程(来自Youtube的Bucky),并在Slick的论坛上阅读了很多内容。我目前正在努力解决让我完全陷入困境的问题。我试图在游戏循环中每隔3秒左右创建一个新对象。 一些人试图在光滑的论坛上帮助我,但我仍然无法让它工作。 我想在stackoverflow上问这个......
一些背景知识: 也许你还记得那个旧的DOS游戏,你让伞兵从天上掉下来,当它们到达地面时,它们朝着大炮移动。当10输入它时,它会爆炸。 作为佳能老板,你想在地面前射击所有伞兵。 所以,我只在java中这样做:)
对于那些从未使用过Slick的人来说,有一个init()函数来初始化东西,一个render()函数负责将所有内容渲染到屏幕上,而update()函数基本上就是游戏循环本身。所有更新都在那里进行,然后相应地渲染函数渲染。
我试图每隔3秒从一个随机X中放下伞兵。
有一个包含queuedObjects的ArrayList。每次更新时,该对象都使用一个函数,该函数根据从update()函数派生的增量来确定天气的部署时间。如果是,则该对象被转移到另一个在render函数中调用的ArrayList。因此,每次将对象传输到renderList时,它都会被渲染。 问题是,更新方法运行速度极快。所以我最后得到的是,所有对象都是即时渲染的,而不是每隔3秒一次渲染一次。
我尝试实现Thread.sleep()技术。但它与Slick的效果不佳。 我也试过用TimerTaks和调度器工作,但我无法弄清楚如何使用它... 有人可以指点我正确的方向吗?谢谢!!
Paratrooper对象:
package elements;
import java.awt.Rectangle;
import java.util.Random;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
public class Paratrooper1 {
//private int strength = 100;
private float yd;
private float x;
private float y;
private int width;
private int height;
private boolean visible;
private Image paratrooperImage;
private int time;
private Random random;
public Paratrooper1() throws SlickException {
random = new Random();
paratrooperImage = new Image("/res/paratrooper1.png");
width = paratrooperImage.getWidth();
height = paratrooperImage.getHeight();
this.x = generateX();
this.y = 0;
visible = true;
time = 0;
}
public Image getParaImage(){
return paratrooperImage.getScaledCopy(0.2f);
}
public void Move(int delta){
yd += 0.1f * delta;
if(this.y+yd > 500){
this.x = generateX();
this.y = 0;
}else{
this.y += yd;
yd = 0;
}
}
public int getX(){
return (int) x;
}
public int getY(){
return (int) y;
}
public boolean isVisisble(){
return visible;
}
public void setVisible(boolean tof){
visible = tof;
}
public Rectangle getBound(){
return new Rectangle((int)x,(int)y,width,height);
}
private int generateX(){
return random.nextInt(940)+30;
}
public boolean isReadyToDeploy(int delta) {
float pastTime = 0;
pastTime += delta;
long test = System.currentTimeMillis();
if(test >= (pastTime + 3 * 1000)) { //multiply by 1000 to get milliseconds
return true;
}else{
return false;
}
}
}
和游戏代码:
package javagame;
import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import elements.Paratrooper1;
public class Play extends BasicGameState{
Paratrooper1 para1;
private Image cursor;
private int mousePosX = 0;
private int mousePosY = 0;
private String mouseLocationString = "";
private int score;
private ArrayList<Paratrooper1> renderParatroopers;
private ArrayList<Paratrooper1> queuedParatroopers;
private int time;
public Play(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
score = 0;
time = 0;
renderParatroopers = new ArrayList<Paratrooper1>();
//populate arraylist with paratroopers
queuedParatroopers = new ArrayList<Paratrooper1>();
for (int i=0; i<5; i++){
queuedParatroopers.add(new Paratrooper1());
}
cursor = new Image("res/cursor.png");
cursor.setCenterOfRotation(cursor.getWidth()/2, cursor.getHeight()/2);
gc.setMouseCursor(cursor.getScaledCopy(0.1f), 0, 0);
para1 = new Paratrooper1();
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
g.drawString(mouseLocationString, 50, 30);
g.drawString("Paratrooper location: X:" +para1.getY()+ " Y:" +para1.getY(), 50, 45);
g.drawString("Current score: " +score, 800, 30);
//go through arraylist and render each paratrooper object to screen
if (renderParatroopers != null){
for (int i = 0; i < renderParatroopers.size(); i++) {
Paratrooper1 para1 = (Paratrooper1)renderParatroopers.get(i);
if(para1.isVisisble()){
g.drawImage(para1.getParaImage(),para1.getX(),para1.getY());
}
}
}
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input input = gc.getInput();
mousePosX = input.getMouseX();
mousePosY = input.getMouseY();
mouseLocationString = "Pointer location --- X:" +mousePosX+ " Y:" +mousePosY;
for (int i=0; i<queuedParatroopers.size(); i++){
if (queuedParatroopers.get(i).isReadyToDeploy(delta)){
renderParatroopers.add(queuedParatroopers.get(i));
}
}
//update the x and y of each paratrooper object.
//Move() method accepts the delta and is calculated in to
//create a new x and y. Render method will update accordingly.
if(renderParatroopers != null){
for (Paratrooper1 para : renderParatroopers){
para.Move(delta);
}
}
}
/*private boolean isItTimeToDeploy(int deltaVar) {
float pastTime = 0;
pastTime += deltaVar;
long test = System.currentTimeMillis();
if(test >= (pastTime + 3*1000)) { //multiply by 1000 to get milliseconds
return true;
}else{
return false;
}
}*/
public int getID(){
return 1;
}
}
答案 0 :(得分:1)
pastTime 是一个局部变量,它应该是一个实例变量。请尝试使用此版本:
long pastTime = 0;
public boolean isReadyToDeploy(long delta) {
if(pastTime < 3 * 1000) { //multiply by 1000 to get milliseconds
pastTime += delta;
return false;
}else{
pastTime = 0;
return true;
}
}
long previousTime = 0;
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
long tmp = System.currentTimeMillis();
long customDelta = tmp - previousTime;
previousTime = tmp;
Input input = gc.getInput();
mousePosX = input.getMouseX();
mousePosY = input.getMouseY();
mouseLocationString = "Pointer location --- X:" +mousePosX+ " Y:" +mousePosY;
for (int i=0; i<queuedParatroopers.size(); i++){
if (queuedParatroopers.get(i).isReadyToDeploy(customDelta)){
renderParatroopers.add(queuedParatroopers.get(i));
}
}
//update the x and y of each paratrooper object.
//Move() method accepts the delta and is calculated in to
//create a new x and y. Render method will update accordingly.
if(renderParatroopers != null){
for (Paratrooper1 para : renderParatroopers){
para.Move(delta);
}
}
}