I’m new to programming, so I apologize if this is rudimentary. I’m trying to make my Slick2 state-based game spawn a horizontal moving projectile every time I press space. I try to achieve this by having an ArrayList that manages all the projectiles and a Projectile class. I don’t know what else to explain, so I apologize in advance but also I’m willing to clarify.
package gamepackage;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.lwjgl.input.Mouse;
import java.util.ArrayList;
public class Play extends BasicGameState{
Image shipImage;
float shipImageY = 0;
float shipImageX = 12;
String shipImageYText, shipPos;
boolean shoot, enemySpawn = false;
ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
public Play(int state){
// required by Slick2D, not sure its use.
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
shipImage = new Image("res/ship.png");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
g.drawImage(shipImage, shipImageX, shipImageY);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
Input keyboardInput = gc.getInput();
for(Projectile proj : projectiles){
proj.update(gc, delta); //call every projectiles' own update();
}
if(keyboardInput.isKeyPressed(Input.KEY_SPACE)){
projectiles.add( new Projectile(shipImageX, shipImageY) ); //create a new Projectile and add it to the list
}
}
}
class Projectile {
float x, y;
int speed = 2;
boolean alive = true;
Image shotImage;
public Projectile(float x, float y){
this.x = x;
this.y = y;
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
shotImage = new Image("res/shot.png");
}
public void update(GameContainer gc, int delta){
x += speed*delta;
if(x > 1024) {
alive = false;
}
if(alive == false) {
//kill the image
}
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
g.drawImage(shotImage, x, y);
if(alive == false) {
g.clear();
}
}
}
I implemented the code from http://slick.ninjacave.com/forum/viewtopic.php?t=6599. There are no compile errors, and I don’t know how to debug or if that would even help. Any advice is appreciated.