Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Player wont move

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Player wont move

    Hello,

    I have created this code. This code wil create a tile map on the screen.

    Game.java
    import org.newdawn.slick.AppGameContainer;
    import org.newdawn.slick.BasicGame;
    import org.newdawn.slick.GameContainer;
    import org.newdawn.slick.Graphics;
    import org.newdawn.slick.Input;
    import org.newdawn.slick.SlickException;
     
     
    public class Game extends BasicGame {
     
    	private map map;
    	private Player player;
     
     
    	public Game() {
    		super("one class barebone game");
    	}
     
    	public void init(GameContainer container) throws SlickException {
    		map = new map();
    		player = new Player();
    		map.createArray();
    	}
     
     
    	public void render(GameContainer container, Graphics g)  {
    		for(int y=0;y<14;y++){
    			for(int x=0;x<14;x++){
    				if(map.getMap(x, y).equals("g")){
    					g.drawImage(map.getGrass(), x *32, y * 32);
    				}
    				if(map.getMap(x, y).equals("w")){
    					g.drawImage(map.getWall(), x *32, y * 32);
    				}
    			}
    		}
    		player.draw(g);
     
    	}
     
    	public void update(GameContainer gc, int delta) { 
     
    		Input input = gc.getInput();
    		if(input.isKeyDown(Input.KEY_LEFT)){
    			if(map.checkCollision(player.x-1, player.y))
    				player.x-=1*delta;
    			}
     
     
    		if(input.isKeyDown(Input.KEY_RIGHT)){
    			if(map.checkCollision(player.x-1,player.y)){
    				player.x+=0.1*delta;
    			}
    		}
    		if(input.isKeyDown(Input.KEY_UP)){
    			if(map.checkCollision(player.x,player.y-1)){
     
    				player.y-=0.1*delta;
    			}
    		}
    		if(input.isKeyDown(Input.KEY_DOWN)){
    			if(map.checkCollision(player.x,player.y+1)){
     
    				player.y+=1*delta;
    			}
    		}
    	}
     
    	public static void main(String[] args) throws SlickException {
    		AppGameContainer container = new AppGameContainer(new Game(), 14*32, 14*32, false);
    		container.start();		
    	}
     
     
    }

    map.java
    import java.io.File;
    import java.util.Scanner;
    import org.newdawn.slick.Image;
     
     
    public class map {
     
    	private Scanner scanner;
    	private String[] map= new String[14];
    	String[][] blockArray= new String[448][448];
     
    	private Image grass;
    	private Image wall;
     
    	public String getMap(int x, int y){
    		String index = map[y].substring(x,x+1);
    		return index;
     
    	}
     
    	public void createArray(){
    		for(int y=0;y<14;y++){
    			for(int x=0;x<14;x++){
    				if(getMap(x, y).equals("w")){
    					for(int yy=0;yy<32;yy++){
    						for(int xx=0;xx<32;xx++){
    							blockArray[x+xx][y+yy] = "w";
    						}
    					}
    				}
    			}
    		}
    	}
     
    	public boolean checkCollision(int x, int y){
    		return blockArray[x][y] != "w";
    	}
     
    	public map(){
    		try{
    		grass = new Image("data/grass.png");
    		wall = new Image("data/wall.png");
    		}catch(Exception e){
    			System.out.println("Probleem laden afbeeldingen map");
    		}
    		openFile();
    		readFile();
    		closeFile();
     
    	}
     
    	public void openFile(){
    		try{
    			scanner = new Scanner(new File("data/map.txt"));
    		}catch(Exception e){
    			System.out.println("Error loading map");
    		}
    	}
     
    	public void readFile(){
    		while(scanner.hasNext()){
    			for(int i=0;i<14;i++){
    				map[i]=scanner.next();
    			}
    		}
     
    	}
     
    	public void closeFile(){
    		scanner.close();
    	}
     
    	public Image getGrass(){
    		return grass;
    	}
     
    	public Image getWall(){
    		return wall;
    	}
     
    }

    player.java
    import org.newdawn.slick.Graphics;
    import org.newdawn.slick.Image;
     
     
    public class Player {
     
    	public int x=32,y=32;
    	private Image img;
     
    	public Player(){
    		try {
    			img = new Image("data/player.png");
    		} catch(Exception e){
    			System.out.println("Probleem afbeelding player");
    		}
    	}
     
     
    	public void draw(Graphics g){
    		g.drawImage(img,x,y);
    	}
     
    	public int getX(){
    		return x;
    	}
     
    	public int getY(){
    		return y;
    	}
     
    }

    But my player wont move unless I remove this bit of coding.

    if(map.checkCollision(player.x-1,player.y))

    So the error is in that code but I dont know where exactly and I need that code for collision detection.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Player wont move

    When posting code, please narrow it down to an SSCCE- posting a bunch of code split up between multiple files makes it harder to help you. Also, please use standard naming conventions- variables and methods start with a lower-case letter, classes start with an upper-case letter.

    Have you stepped through that method with a debugger to figure out what's going on? Recommended reading: http://www.javaprogrammingforums.com...t-println.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Player wont move

    I have done that debugging and now I have made the conclusion that the error is in the creatArray() method.

    public void createArray(){
    		for(int x=0;x<14;x++){
    			for(int y=0;y<14;y++){
    				if(getMap(x, y).equals("w")){
    					for(int xx=0;xx<32;xx++){
    						for(int yy=0;yy<32;yy++){
    							blockArray[x+xx][y+yy] = "w";
    							System.out.println(x+xx+" : "+y+yy);
    						}
    					}
    				}
    			}
    		}
    	}

    But I can't find the problem in here. First I tought that the problem was that the if the third tile on the first row is a wall it would make array with [3+32], but the third tile is already on 3x32. So I have changed this line of code to blockArray[x*32+xx][y*32+yy] = "w"; now I can move up and down but I cant go left or right.

Similar Threads

  1. [SOLVED] Things Won't Move
    By zeraxis in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 2nd, 2011, 08:46 AM
  2. java game bullets hit player 1 but not player 2
    By ajakking789 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 22nd, 2011, 08:19 AM
  3. java game, both players move for player 2 but not player 1
    By ajakking789 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 21st, 2011, 12:52 PM
  4. How to drag the shape to move?
    By ice in forum AWT / Java Swing
    Replies: 21
    Last Post: December 15th, 2010, 06:45 PM
  5. Node swap (move up)
    By raphytaffy in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 2nd, 2010, 06:56 PM