Slick Forums

Discuss the Slick 2D Library
It is currently Thu Jun 20, 2013 5:17 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Need help with input.
PostPosted: Thu Oct 07, 2010 4:01 pm 
Offline

Joined: Mon Oct 04, 2010 11:06 pm
Posts: 3
Location: US
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);
    }


}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 07, 2010 4:29 pm 
Offline
Oldbie
User avatar

Joined: Fri Jul 20, 2007 9:25 am
Posts: 410
Location: Croatia
it's just like you said.. nothing complicated
in your init method:
Code:
container.getInput().addListener(yourListener)

where "yourListener" is object that extends InputAdapter or implements whatever it needs to implement, I forgot..

also you need to move evrything in constructor that does not help with construction in the init() method, that's just common sense


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 07, 2010 6:32 pm 
Offline

Joined: Mon Oct 04, 2010 11:06 pm
Posts: 3
Location: US
I'm not sure how to enter a state from the implemented listener though since it doesnt have access to the game container. ?

Code:
import org.newdawn.slick.Input;
import org.newdawn.slick.InputListener;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Ethan
*/
public class IntroListener implements InputListener {

 
    @Override
    public void keyPressed(int key, char c) {
        if(key == Input.KEY_SPACE)
        {
            enterState(1);
        }
    }

    @Override
    public void mouseWheelMoved(int change) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseClicked(int button, int x, int y, int clickCount) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mousePressed(int button, int x, int y) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseReleased(int button, int x, int y) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseMoved(int oldx, int oldy, int newx, int newy) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseDragged(int oldx, int oldy, int newx, int newy) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setInput(Input input) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isAcceptingInput() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void inputEnded() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void inputStarted() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void keyReleased(int key, char c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void controllerLeftPressed(int controller) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void controllerLeftReleased(int controller) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void controllerRightPressed(int controller) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void controllerRightReleased(int controller) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void controllerUpPressed(int controller) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void controllerUpReleased(int controller) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void controllerDownPressed(int controller) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void controllerDownReleased(int controller) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void controllerButtonPressed(int controller, int button) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void controllerButtonReleased(int controller, int button) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

   

}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 07, 2010 6:49 pm 
Offline

Joined: Mon Oct 04, 2010 11:06 pm
Posts: 3
Location: US
Nevermind, I'm certifiably retarded. I just need to pass the Game to the IntroListener class in it's constructor.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group