Slick Forums

Discuss the Slick 2D Library
It is currently Sun May 19, 2013 2:00 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Jul 11, 2012 1:06 am 
Offline

Joined: Mon Apr 09, 2012 8:03 pm
Posts: 9
I draw my map from a txt file. Would I have to write to the text file to notice the changes I made? Right now it changes the number in the array but the tile texture doesn't change. Do I have to do more than just change the number in the array?

Code:
public class Tiles {
   
    public Image[] tiles = new Image[5];

    public int[][] map = new int[64][64];
   
    public Image grass, dirt, fence, mound;
   
    private SpriteSheet tileSheet;
   
    public int tileWidth = 32;
    public int tileHeight = 32;
   
    Player player = new Player();

    public void init() throws IOException, SlickException {
        tileSheet = new SpriteSheet("assets/tiles.png", tileWidth, tileHeight);
       
        grass = tileSheet.getSprite(0, 0);
        dirt = tileSheet.getSprite(7, 7);
        fence = tileSheet.getSprite(2, 0);
        mound = tileSheet.getSprite(2, 6);
        tiles[0] = grass;
        tiles[1] = dirt;
        tiles[2] = fence;
        tiles[3] = mound;
         
        int x=0, y=0;
   BufferedReader in = new BufferedReader(new FileReader("assets/map.dat"));
   String line;
   while ((line = in.readLine()) != null) {
            String[] values = line.split(",");
            x = 0;
       for (String str : values) {
                int str_int = Integer.parseInt(str);
                map[x][y]=str_int;
                //System.out.print(map[x][y] + " ");
                x++;
            }
   //System.out.println("");
   y++;
   }
        in.close();
    }
   
    public void update(GameContainer gc) {
       
    }
   
    public void render(GameContainer gc) {
        for(int y = 0; y < map.length; y++) {
            for(int x = 0; x < map[0].length; x ++) {
                int textureIndex = map[x][y];
                Image texture = tiles[textureIndex];
                texture.draw(x*tileWidth,y*tileHeight);
            }
        }
    }
}


Mouse Picking

Code:
float mouseX = input.getMouseX();
        float mouseY = input.getMouseY();
       
        double mousetileX = Math.floor((double)mouseX/tiles.tileWidth);
        double mousetileY = Math.floor((double)mouseY/tiles.tileHeight);
       
        double playertileX = Math.floor(playerX/tiles.tileWidth);
        double playertileY = Math.floor(playerY/tiles.tileHeight);
       
        double lengthX = Math.abs((float)playertileX - mousetileX);
        double lengthY = Math.abs((float)playertileY - mousetileY);
        double distance = Math.sqrt((lengthX*lengthX)+(lengthY*lengthY));
       
        if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON) && distance < 4) {
            System.out.println("Clicked"); 
            if(tiles.map[(int)mousetileX][(int)mousetileY] == 1) {
                tiles.map[(int)mousetileX][(int)mousetileY] = 0;
            }
        }


Top
 Profile  
 
PostPosted: Wed Jul 11, 2012 12:03 pm 
Offline

Joined: Mon May 07, 2012 11:36 pm
Posts: 93
Is your Mouse Picking code in the Tiles class?
I'm thinking that its not changing because its not being updated due to there being nothing in the update method.


Top
 Profile  
 
PostPosted: Wed Jul 11, 2012 3:35 pm 
Offline

Joined: Mon Apr 09, 2012 8:03 pm
Posts: 9
Sorry I should have explained it's in another class. The code works and it changes the number of the tile I click, but the texture does not change


Top
 Profile  
 
PostPosted: Wed Jul 11, 2012 4:08 pm 
Offline

Joined: Mon May 07, 2012 11:36 pm
Posts: 93
I'm not sure i can work your code out exactly, but i think the number changes at your x and y ints, right? if so, try putting x = x; and y = y; in the update method.


Top
 Profile  
 
PostPosted: Fri Jul 13, 2012 2:52 am 
Offline

Joined: Mon Apr 09, 2012 8:03 pm
Posts: 9
Yeah the numbers are changing correctly it's just the texture doesn't change. Idk if it is something with my texture loading or what


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

All times are UTC


Who is online

Users browsing this forum: No registered users 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