(注意:從遊戲開發這個職位是複製粘貼,因爲我得到的評論說,在堆棧溢出發佈此)如何添加碰撞檢測的死亡特徵?
我想要的是,當我的球員與我的瓷磚碰撞(這是固體)他們」將被髮送到menustate(或menuscreen)。我已經爲我的瓷磚進行了碰撞檢測(使用isSolid)。所以,如果我的玩家碰到一塊磚塊(那塊磚塊是堅實的),它會被髮送給我的組合單位。
對不起,如果我添加不需要的類,它的我的玩家,生物和實體類相互延伸。
這是我的碰撞檢測存在的生物類。
public abstract class Creatures extends Entity {
public static final int DEFAULT_HEALTH = 1;
public static final float DEFAULT_SPEED = 5.0f;
public static final int DEFAULT_CREATURE_WIDTH = 128;
public static final int DEFAULT_CREATURE_HEIGHT = 128;
protected int health;
protected float speed;
public static float xMove;
public static float yMove;
protected int MinHeight;
public Creatures(Handler handler, float x, float y, int width, int height) {
super(handler, x, y, width, height);
health = DEFAULT_HEALTH;
speed = DEFAULT_SPEED;
xMove = 0;
yMove = 0;
MinHeight = -1;
}
public void move(){
moveX();
moveY();
}
//COLLISION DETECTION BELOW
public void moveX(){
if(xMove > 0){//Moving right
int tx = (int) (x + xMove + bounds.x + bounds.width)/Tile.TILEWIDTH;
if(!collisionWithTile(tx, (int) (y + bounds.y)/Tile.TILEHEIGHT) &&
!collisionWithTile(tx, (int) (y + bounds.y + bounds.height)/Tile.TILEHEIGHT)){
x += xMove;
}else{
x = tx * Tile.TILEWIDTH - bounds.x - bounds.width - 1;
}
}else if(xMove < 0){//Moving left
int tx = (int) (x + xMove + bounds.x)/Tile.TILEWIDTH;
if(!collisionWithTile(tx, (int) (y + bounds.y)/Tile.TILEHEIGHT) &&
!collisionWithTile(tx, (int) (y + bounds.y + bounds.height)/Tile.TILEHEIGHT)){
x += xMove;
}else{
x = tx * Tile.TILEWIDTH + Tile.TILEWIDTH - bounds.x;
}
}
}
public void moveY(){
if(yMove < 0){//Up
int ty = (int) (y + yMove + bounds.y)/Tile.TILEHEIGHT;
if(!collisionWithTile((int) (x + bounds.x)/Tile.TILEWIDTH, ty) &&
!collisionWithTile((int) (x + bounds.x + bounds.width)/Tile.TILEWIDTH, ty)){
y += yMove;
}else{
y = ty * Tile.TILEHEIGHT + Tile.TILEHEIGHT - bounds.y;
}
}else if(yMove > 0){//Down
int ty = (int) (y + yMove + bounds.y + bounds.height)/Tile.TILEHEIGHT;
if(!collisionWithTile((int) (x + bounds.x)/Tile.TILEWIDTH, ty) &&
!collisionWithTile((int) (x + bounds.x + bounds.width)/Tile.TILEWIDTH, ty)){
y += yMove;
}else{
y = ty * Tile.TILEHEIGHT - bounds.y - bounds.height - 1;
}
}
}
//This code below checks if the tile im colliding is Solid or not
protected boolean collisionWithTile(int x, int y){
return handler.getWorld().getTile(x, y).isSolid();
}
//GETTERS AND SETTERS
public float getxMove() {
return xMove;
}
@SuppressWarnings("static-access")
public void setxMove(float xMove) {
this.xMove = xMove;
}
public float getyMove() {
return yMove;
}
@SuppressWarnings("static-access")
public void setyMove(float yMove) {
this.yMove = yMove;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public float getSpeed() {
return speed;
}
public void setSpeed(float speed) {
this.speed = speed;
}
}
然後我的實體類(我的生物類擴展到這一類)
import java.awt.Graphics;
import java.awt.Rectangle;
public abstract class Entity {
protected Handler handler;
protected float x, y;
protected int width, height;
protected Rectangle bounds;
public Entity(Handler handler, float x, float y, int width, int height){
this.handler = handler;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
bounds = new Rectangle(0, 0, width, height);
}
public abstract void tick();
public abstract void render(Graphics g);
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
我的播放器類(我的播放器類擴展到我的生物類)
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Player extends Creatures{
//Animations
private Animation animRight, animLeft;
public Player(Handler handler, float x, float y) {
super(handler, x, y, Creatures.DEFAULT_CREATURE_WIDTH, Creatures.DEFAULT_CREATURE_HEIGHT);
bounds.x = 32;
bounds.y = 32;
bounds.width = 92;
bounds.height = 96;
//Animations
animRight = new Animation(100, Assets.DerpDino_right);
animLeft = new Animation(100, Assets.DerpDino_left);
}
@Override
public void tick() {
//Animations
animRight.tick();
animLeft.tick();
//Movement
getInput();
move();
handler.getGameCamera().centerOnEntity(this);
}
@SuppressWarnings("unused")
private void Dead(){
//what to put in here
}
private void getInput(){
xMove = 6;
//Gravity
yMove = 6;
MinHeight = 0;
if(handler.getKeyManager().left)
xMove = -speed;
if(handler.getKeyManager().right)
xMove = speed;
if(handler.getKeyManager().jumping)
//this makes my player fly
yMove = -speed;
}
@Override
public void render(Graphics g) {
g.drawImage(getCurrentAnimationFrame(), (int) (x - handler.getGameCamera().getxOffset()), (int) (y -handler.getGameCamera().getyOffset()), width, height, null);
private BufferedImage getCurrentAnimationFrame(){
if(xMove < 0){
return animLeft.getCurrentFrame();
}else{
return animRight.getCurrentFrame();
}
}
}
瓷磚類( isSolid()is)
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Tile {
//STATIC STUFF HERE
public static Tile[] tiles = new Tile[256];
public static Tile grassTile = new GrassTile(0);
public static Tile cactusTile = new CactusTile(1);
public static Tile dirtTile = new DirtTile(2);
public static Tile skyTile = new SkyTile(3);
public static Tile cloudTile = new CloudTile(4);
public static Tile caveTile = new CaveTile(5);
public static Tile stoneTile = new StoneTile(6);
//CLASS
public static final int TILEWIDTH = 128, TILEHEIGHT = 128;
protected BufferedImage texture;
protected final int id;
public Tile(BufferedImage texture, int id){
this.texture = texture;
this.id = id;
tiles[id] = this;
}
public void tick(){
}
public void render(Graphics g, int x, int y){
g.drawImage(texture, x, y, TILEWIDTH, TILEHEIGHT, null);
}
//HERE!!
public boolean isSolid(){
return false;
}
public int getId(){
return id;
}
}
我已經做了狀態(不是與枚舉),如果你想我把它添加到我的帖子然後告訴我。 – SreeLegend
那麼問題是什麼?如果某個實體與某個圖塊發生衝突,則將該狀態設置爲菜單。進一步解釋問題,然後我將能夠更好地幫助你。 – Erninger
我不知道該怎麼做 – SreeLegend