Ok, I'm working on a statebased game, and the following code is my intro screen. I wanted to create a listener for the spacebar, but I'm not sure how to go about doing it. I know I'm supposed to get the game's input from the game container and add a listener to it, I'm just fuzzy on the details. Help?
Code:
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
/**
*
* @author Ethan
*/
public class IntroState extends BasicGameState {
int stateId, timePassed;
float alpha;
Color alphaFilter;
Image[] renderList = new Image[3];
int[][] renderPosition = new int[3][3];
boolean increasingAlpha;
Music currentMusic;
public IntroState(int stateId) throws SlickException
{
super();
this.stateId = stateId;
timePassed = 0;
increasingAlpha = true;
alpha = 0;
currentMusic = new Music("data/music/intro.xm");
}
@Override
public int getID()
{
return stateId;
}
@Override
public void init(GameContainer container, StateBasedGame game) throws SlickException
{
renderList[0] = new Image("data/images/backgrounds/background1.png");
renderPosition[0][0] = 0;
renderPosition[0][1] = 0;
renderList[1] = new Image("data/images/introTitle.png");
renderPosition[1][0] = 73;
renderPosition[1][1] = 100;
renderList[2] = new Image("data/images/introStart.png");
renderPosition[2][0] = 207;
renderPosition[2][1] = 181;
currentMusic.loop();
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException
{
renderList[0].draw(renderPosition[0][0], renderPosition[0][1]);
renderList[1].draw(renderPosition[1][0], renderPosition[1][1]);
renderList[2].draw(renderPosition[2][0], renderPosition[2][1], alphaFilter);
}
@Override
public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException
{
Input gameInput = container.getInput();
timePassed = delta;
System.out.println(timePassed);
if(increasingAlpha)
{
if(alpha < 1.0)
alpha += (timePassed/1) * 0.001;
else
increasingAlpha = false;
}
else
{
if(alpha > 0.1)
alpha -= (timePassed/1) * 0.001;
else
increasingAlpha = true;
}
alphaFilter = new Color(1f, 1f, 1f, alpha);
}
}