Ed_RockStarGuy
Junior Newbie
|
 |
«
Posted
2013-03-16 16:14:52 » |
|
The problem i am having is when making the games block colliton center onto the blocks and for some reason the colision box is a few blocks NW from where it should be. Player file: package com.AdamWalters.com.zain.mob;
import com.AdamWalters.com.zain.Level.Level; import com.AdamWalters.com.zain.Level.tiles.Tile; import com.AdamWalters.com.zain.graphics.Screen; import com.AdamWalters.com.zain.graphics.Sprite; import com.AdamWalters.com.zain.input.InputHandler;
public class Player extends Mob {
private InputHandler input; private Sprite sprite; private int anim = 0; private boolean walking = false; static final int maxHealth = 2000; static int health = 2000;
int speed = 40; public Player(Level level, int x, int y, InputHandler input) { super(level, "Player", x, y, 1); this.input = input; sprite = Sprite.player_forward; }
public void update() { boolean running; int xa = 0, ya = 0; if (anim < 7500) anim++; else anim = 0; if (input.shift){ running = true; speed = 20; }else{ running = false; speed = 40; } if (input.up&&running) ya--; if (input.down&&running) ya++; if (input.left&&running) xa--; if (input.right&&running) xa++; if (input.up) ya--; if (input.down) ya++; if (input.left) xa--; if (input.right) xa++;
if (xa != 0 || ya != 0) { move(xa, ya); walking = true; } else { walking = false; } }
public void render(Screen screen) { int flip = 0; if (dir == 2) { sprite = Sprite.player_forward; if (walking) { if (anim % speed > speed/2) { sprite = Sprite.player_forward_1; } else { sprite = Sprite.player_forward_2;
} } } if (dir == 1) { sprite = Sprite.player_side; if (walking) { if (anim % speed > speed/2) { sprite = Sprite.player_side_1; } else { sprite = Sprite.player_side_2; } } } if (dir == 0) { sprite = Sprite.player_back; if (walking) { if (anim % speed > speed/2) { sprite = Sprite.player_back_1; } else { sprite = Sprite.player_back_2; } } }
if (dir == 3) { sprite = Sprite.player_side; if (walking) { if (anim % speed > speed/2) { sprite = Sprite.player_side_1; } else { sprite = Sprite.player_side_2; }
} flip = 1; }//this has to be changed for the player sprite. e.g. 16 for 32 * 32 sprites and 32 for 16 * 16 sprites. screen.renderPlayer(x - 16, y - 16, sprite, flip); } public boolean hasCollided(int xa, int ya) { int xMin = 0; int xMax = 15; int yMin = 7; int yMax = 15; for (int x = xMin; x < xMax; x++) { if (isSolidTile(xa, ya, x, yMin)) { return true; } } for (int x = xMin; x < xMax; x++) { if (isSolidTile(xa, ya, x, yMax)) { return true; } } for (int y = yMin; y < yMax; y++) { if (isSolidTile(xa, ya, xMin, y)) { return true; } } for (int y = yMin; y < yMax; y++) { if (isSolidTile(xa, ya, xMax, y)) { return true; } } return false; }
public static int getHealthPersentage() { int persentage; persentage = (health/100)/maxHealth; return persentage; } public static int maxHealth(){ return maxHealth; } public static int health(){ return health; } public static void addDamage(int damage){ health = health - damage; }
}
|
mob file: package com.AdamWalters.com.zain.mob;
import com.AdamWalters.com.zain.Level.Level; import com.AdamWalters.com.zain.Level.tiles.Tile; import com.AdamWalters.com.zain.entity.Entity; import com.AdamWalters.com.zain.graphics.Sprite;
public abstract class Mob extends Entity {
protected Sprite sprite; protected int dir = 0; // 0 = north, 1 = east, 2 = south, 3 = west protected boolean moving = false; int speed; public Mob(Level level, String name, int x, int y, int speed) { super(level); this.x = x; this.y = y; this.speed = speed; } public void move(int xa, int ya) { if (xa > 0) dir = 1; if (xa < 0) dir = 3; if (ya > 0) dir = 2; if (ya < 0) dir = 0; if (!hasCollided(xa, ya)) { if (ya < 0) dir = 0; if (ya > 0) dir = 2; if (xa < 0) dir = 3; if (xa > 0) dir = 1; x += xa * 1; y += ya * 1; } } public abstract boolean hasCollided(int xa, int ya); protected boolean isSolidTile(int xa, int ya, int x, int y) { if (level == null) { return false; } Tile lastTile = level.getTile((this.x + x) >> 7, (this.y + y) >> 7); Tile newTile = level.getTile((this.x + x + xa) >> 7, (this.y + y + ya) >> 7); if (!lastTile.equals(newTile) && newTile.isSolid()) { return true; } return false; }
public void update() { } public void render() { } }
|
level file: package com.AdamWalters.com.zain.Level;
import com.AdamWalters.com.zain.graphics.Screen; import com.AdamWalters.com.zain.Level.tiles.Tile;
public class Level { protected static int width; protected static int height; protected int[] tilesInt; protected static int[] tiles; public static Level spawn = new SpawnLevel ("/levels/level.png"); public Level(int width, int height) { this.width = width; this.height = height; tilesInt = new int [width * height]; generateLevel(); } public Level(String path) { loadLevel(path); generateLevel(); }
protected void generateLevel() { } protected void loadLevel(String path) { }
public void tick() { } @SuppressWarnings ("unused") private void time() { } public void render(int xScroll, int yScroll, Screen screen) { if (xScroll < 0) xScroll = 0; if (xScroll > ((width << 3) - screen.width)) xScroll = ((width << 3) - screen.width); if (yScroll < 0) yScroll = 0; if (yScroll > ((height << 3) - screen.height)) yScroll = ((height << 3) - screen.height); screen.setOffset(xScroll, yScroll); /** >> 4 = /16 **/ int x0 = xScroll >> 4; int x1 = (xScroll + screen.width + 16) >> 4; int y0 = yScroll >> 4; int y1 = (yScroll + screen.height + 16) >> 4; for (int y = y0; y < y1; y++) { for (int x = x0; x < x1; x++) { getTile(x, y).render(x, y, screen); } } } //Grass = 0xFF00 / 0x00FF00 //Flower = 0xFFFF00 //Rock = 0x878787 //first two ff = 100% opaque & first two 00 = 100% transparent. public Tile getTile(int x, int y) { if (x < 0 || y < 0 || x >= width || y >= height){ return Tile.voidTile; } if (tiles[x + y * width] == Tile.col_spawn_grass){ return Tile.grass; } if (tiles[x + y * width] == Tile.col_spawn_log){ return Tile.log; } if (tiles[x + y * width] == Tile.col_spawn_leaves){ return Tile.leaves; } return Tile.voidTile; } public static int getTileID(int x, int y) { if (x < 0 || y < 0 || x >= width || y >= height){ return 1; } if (tiles[x + y * width] == Tile.col_spawn_grass){ return 2; } if (tiles[x + y * width] == Tile.col_spawn_log){ return 3; } if (tiles[x + y * width] == Tile.col_spawn_leaves){ return 4; } return 1; }
}
|
if anyone has a solution to this it would be appreciated if you left me a message or drop me a skype PM at :mythirdleg35
|