Results 1 to 2 of 2
  1. #1
    sspitfire is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default Marte Engine / slick2d game - problem with input

    Hi all. Im following the tutorials on the ME wiki https://github.com/Gornova/MarteEngine/wiki and Im stuck at the input tutorial (#4) - it doesnt work for me in the Player and Level class - it only works in the HelloWorld Class using the slick2d methods for input. Can you give me some idea whats wrong in my code ? Here are my classes following the tutorials. ( I tried the slick2d forum but it seems its not active anymore)
    Java Code:
    import org.newdawn.slick.AppGameContainer;
    import org.newdawn.slick.GameContainer;
    import org.newdawn.slick.SlickException;
    import org.newdawn.slick.state.StateBasedGame;
    
    /**
     * Launch Hello World example
    **/
    public class HelloWorldTest extends StateBasedGame {
    
        public HelloWorldTest(String title) {
            super(title);
        }
    
        @Override
        public void initStatesList(GameContainer container) throws SlickException {
                    // add HelloWorld to current game with id 1
            addState(new HelloWorld(1, container));
        }
    
        public static void main(String[] argv) {
            try {
                AppGameContainer container = new AppGameContainer(new HelloWorldTest(
                        "Hello World Marte Engine"));
                container.setDisplayMode(800, 600, false);
                container.setTargetFrameRate(60);
                container.start();
                
            } catch (SlickException e) {
                e.printStackTrace();
            }
        }
    
    }
    ========
    import it.randomtower.engine.World;
    
    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.StateBasedGame;
    
    //import it.randomtower.engine.World;
    
    //slick input works
    
    public class HelloWorld extends World {
    private Image img;
    private boolean one = false;
    public HelloWorld(int id, GameContainer container) throws SlickException {
            super(id, container);
            
    			 img = new Image("data/Alien.png");
    		} 
        
    
        @Override
        public void render(GameContainer container, StateBasedGame stateBasedGame, Graphics g)
                throws SlickException {
            super.render(container,stateBasedGame,g);
          
            g.drawString("Hello World", 300, 200);
            img.draw(100, 100, 0.2f);
        //   if (one) g.drawString("one pressed", 100, 100);
        }
    
    
    	@Override
    	public void init(GameContainer container, StateBasedGame game)
    			throws SlickException {
    		// TODO Auto-generated method stub
    		super.init(container, game);
    		
    	}
    
    
    	@Override
    	public void update(GameContainer arg0, StateBasedGame arg1, int arg2)
    			throws SlickException {
    		// TODO Auto-generated method stub
    		super.update(arg0, arg1, arg2);
    		Input in = container.getInput();
    	//	if(in.isKeyDown(Input.KEY_1)) one = true;
    	//	if(in.isKeyDown(Input.KEY_4)) System.out.println("4 pressed");
    		
    	}
    
    }
    ============
    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 it.randomtower.engine.entity.Entity;
    
    //no input works
    
    public class Player extends Entity {
    
        /**
         * @param x, x coordinate on screen where Player starts
         * @param y, y coordinate on screen where Player starts
         * @throws SlickException 
         */
    	
    	
        public Player(float x, float y) throws SlickException {
            super(x, y);
            // load Image from disk and associate it as player image
            Image img = new Image("data/Alien.png");
            setGraphic(img);
    
            // define a command to handle input
            define("RIGHT", Input.KEY_RIGHT);
        }
    
    	@Override
    	public void update(GameContainer container, int delta) throws SlickException {
    		// TODO Auto-generated method stub
    		super.update(container, delta);
    		 // check if a key is down 
           if(check("RIGHT")){
                // do anything you like, for example:
              x = x+10;
               System.out.println("right pressed!");
            
           }
           
          //  Input in = container.getInput();
            
            
    	//	if(in.isKeyDown(Input.KEY_2)) System.out.println("2 is pressed!");
    		
    	}
    
    	
    	@Override
    	public void render(GameContainer arg0, Graphics arg1) throws SlickException {
    		// TODO Auto-generated method stub
    		super.render(arg0, arg1);
    	}
    
       
       
        }
    ==========
    import org.newdawn.slick.GameContainer;
    import org.newdawn.slick.Graphics;
    import org.newdawn.slick.Input;
    import org.newdawn.slick.SlickException;
    import org.newdawn.slick.state.StateBasedGame;
    
    import it.randomtower.engine.ME;
    import it.randomtower.engine.World;
    
    import it.randomtower.engine.entity.Entity;
    
    //slick input not working
    
    public class Level extends World {
    private boolean five = false;
    	/**
    	 * @param id, unique identifier for World
    	 * @param container, container for World
    	 */
    	public Level(int id, GameContainer container) {
    		super(id, container);
    		// TODO Auto-generated constructor stub
    		
    		
    	}
    
    	@Override
    	public void add(Entity e) {
    		// TODO Auto-generated method stub
    		super.add(e);
    	}
    
    	@Override
    	public  void init(GameContainer container, StateBasedGame game)
    			throws SlickException {
    		super.init(container, game);
    		
    		Player player = new Player(100,100);
    		add(player) ;// cant resolve World.GAME as in the tutorial
    	
    	
    	}
    	
    	@Override
    	public void update(GameContainer container, StateBasedGame game, int delta)
    			throws SlickException {
    		super.update(container, game, delta);
    		Input in = container.getInput();
    		
    		if(in.isKeyDown(Input.KEY_5)) {
    			 five = true;
    		}
    	}
    	
    	@Override
    	public void render(GameContainer container, StateBasedGame game, Graphics g)
    			throws SlickException {
    		super.render(container, game, g);
    		if(five) System.out.println("five!");
    	}
    	
    	
    
    }

  2. #2
    sspitfire is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default Re: Marte Engine / slick2d game - problem with input

    SOLVED : I had to add Level to the main state based game and this wasnt shown anywhere in the tutorials. Please remove the thread

Similar Threads

  1. Review request: Slick2D Game Development (free review copies)
    By velindak in forum Reviews / Advertising
    Replies: 1
    Last Post: 11-21-2013, 02:04 PM
  2. Replies: 0
    Last Post: 05-08-2013, 11:46 PM
  3. {FPS} Issue in my OWN Java Game ONLY+Slick2D
    By Breton in forum Java Gaming
    Replies: 0
    Last Post: 06-27-2012, 04:40 AM
  4. Replies: 6
    Last Post: 07-25-2011, 12:51 PM
  5. Replies: 1
    Last Post: 06-02-2011, 09:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •