Skip to main content
added 82 characters in body
Source Link
Corey
  • 85
  • 2
  • 8

Edit - Solved

public boolean collision() {
    playerRect = new Rectangle(x,y, playerWidth, playerHeight);
     
    for(int i = 0; i < tiles.map.length; i++) {
        for(int j = 0; j < tiles.map.length; j++) {
            if(tiles.map[i][j]map[j][i] == 2 || tiles.map[j][i] == 3) {
                tileRect = new Rectangle(i*tiles.tileWidth, j*tiles.tileHeight, tiles.tileWidth,tiles.tileHeight);
                rect.add(tileRect);
            }
        }
    }  
    for(Rectangle collision : rect) {
        if(collision.intersects(playerRect)) { 
            System.out.println("Collided");return true;
        }
    }
    return false;
}

Edit

playerRect = new Rectangle(x,y, playerWidth, playerHeight);
     
    for(int i = 0; i < tiles.map.length; i++) {
        for(int j = 0; j < tiles.map.length; j++) {
            if(tiles.map[i][j] == 2) {
                tileRect = new Rectangle(i*tiles.tileWidth, j*tiles.tileHeight, tiles.tileWidth,tiles.tileHeight);
                rect.add(tileRect);
            }
        }
    }  
    for(Rectangle collision : rect) {
        if(collision.intersects(playerRect)) { 
            System.out.println("Collided");
        }
    }

Edit - Solved

public boolean collision() {
    playerRect = new Rectangle(x,y, playerWidth, playerHeight);
     
    for(int i = 0; i < tiles.map.length; i++) {
        for(int j = 0; j < tiles.map.length; j++) {
            if(tiles.map[j][i] == 2 || tiles.map[j][i] == 3) {
                tileRect = new Rectangle(i*tiles.tileWidth, j*tiles.tileHeight, tiles.tileWidth,tiles.tileHeight);
                rect.add(tileRect);
            }
        }
    }  
    for(Rectangle collision : rect) {
        if(collision.intersects(playerRect)) { 
            return true;
        }
    }
    return false;
}
Tweeted twitter.com/#!/StackGameDev/status/213987892979183618
added 35 characters in body
Source Link
Corey
  • 85
  • 2
  • 8

I tried something like this, but I it doesn't ever "collide". X and y are my player position.Edit

ifplayerRect (tiles.map[= new Rectangle(int)x/32][(int),y/32], ==playerWidth, 2playerHeight)
    {;
        System.out.println("Collided");
    }

Edit

for(int i = 0; i < tiles.map.length; i ++i++) {
        for(int j = 0; j < tiles.map.length; j++) {
            if(tiles.map[i][j] == 2) {
                tileRect = new Rectangle(i*tiles.tileWidth, j*tiles.tileHeight, tiles.tileWidth,tiles.tileHeight);
                rect.add(tileRect);
            }
        }
    }  
    for(Rectangle collision : rect) {
        if(collision.intersects(playerRect)) { 
            System.out.println("Collided");
        }
    }

I tried something like this, but I it doesn't ever "collide". X and y are my player position.

if (tiles.map[(int)x/32][(int)y/32] == 2)
    {
        System.out.println("Collided");
    }

Edit

for(int i = 0; i < tiles.map.length; i ++) {
        for(int j = 0; j < tiles.map.length; j++) {
            if(tiles.map[i][j] == 2) {
                tileRect = new Rectangle(i*tiles.tileWidth, j*tiles.tileHeight, tiles.tileWidth,tiles.tileHeight);
                rect.add(tileRect);
            }
        }
    }

Edit

playerRect = new Rectangle(x,y, playerWidth, playerHeight);
     
    for(int i = 0; i < tiles.map.length; i++) {
        for(int j = 0; j < tiles.map.length; j++) {
            if(tiles.map[i][j] == 2) {
                tileRect = new Rectangle(i*tiles.tileWidth, j*tiles.tileHeight, tiles.tileWidth,tiles.tileHeight);
                rect.add(tileRect);
            }
        }
    }  
    for(Rectangle collision : rect) {
        if(collision.intersects(playerRect)) { 
            System.out.println("Collided");
        }
    }
added 369 characters in body
Source Link
Corey
  • 85
  • 2
  • 8

Edit

for(int i = 0; i < tiles.map.length; i ++) {
        for(int j = 0; j < tiles.map.length; j++) {
            if(tiles.map[i][j] == 2) {
                tileRect = new Rectangle(i*tiles.tileWidth, j*tiles.tileHeight, tiles.tileWidth,tiles.tileHeight);
                rect.add(tileRect);
            }
        }
    }

Edit

for(int i = 0; i < tiles.map.length; i ++) {
        for(int j = 0; j < tiles.map.length; j++) {
            if(tiles.map[i][j] == 2) {
                tileRect = new Rectangle(i*tiles.tileWidth, j*tiles.tileHeight, tiles.tileWidth,tiles.tileHeight);
                rect.add(tileRect);
            }
        }
    }
Source Link
Corey
  • 85
  • 2
  • 8
Loading