How do I separate codes with classes?

Posted by Trycon on Game Development See other posts from Game Development or by Trycon
Published on 2012-11-01T08:58:12Z Indexed on 2012/11/01 11:16 UTC
Read the original article Hit count: 234

Filed under:
|

I have this main class:

package javagame;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class tests extends BasicGameState{

        public boolean render=false;

        tests1 test = new tests1();
    public tests(int test) {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void init(GameContainer arg0, StateBasedGame arg1)
            throws SlickException {
        // TODO Auto-generated method stub

    }

    @Override
    public void render(GameContainer arg0, StateBasedGame arg1, Graphics g)
            throws SlickException {
        // TODO Auto-generated method stub

        if(render==true)
        {
            g.drawString("Hello",100,100);
        }
    }

    @Override
    public void update(GameContainer gc, StateBasedGame s, int delta)
            throws SlickException {
        // TODO Auto-generated method stub

        test.render=render;
        test.update(gc, s, delta);
    }

    @Override
    public int getID() {
        // TODO Auto-generated method stub
        return 1000;
    }

}

and its sub-class:

package javagame;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;
import org.newdawn.slick.state.StateBasedGame;

public class tests1 {
    public boolean render;
    public void update(GameContainer gc, StateBasedGame s, int delta)
    {
        Input input = gc.getInput();


        if(input.isKeyPressed(Input.KEY_X))
        {
            render=true;
        }
    }
}

I was finding a way to prevent many codes in one class. I'm new to java. When I try running my game, then when I press X, it does not work. How am I suppose to fix that?

© Game Development or respective owner

Related posts about java

Related posts about slick