嗯,具体而言我确实让敌人的运动正确,但我想实施一种让敌人不会从悬崖上掉下来的方式。 我尝试了几件事,但它只是不起作用: 以下是我发现与问题相关的代码:
实体超级班:
package com.cutthedamntree.main.entity;
import java.awt.Graphics;
import java.awt.Rectangle;
import com.cutthedamntree.main.Id;
import com.cutthedamntree.main.handlers.Handler;
public abstract class Entity {
protected int x, y, width, height, velX, velY;
protected boolean solid;
protected Id id;
protected Handler handler;
public boolean jumping = false;
public boolean falling = true;
public double gravity = 0.0;
public int direction = 0; // 0 is right , 1 is left
public Entity(int x, int y, int width, int height, boolean solid, Id id, Handler handler){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.solid = solid;
this.id = id;
this.handler = handler;
}
public abstract void render(Graphics g);
public abstract void tick();
public void die(){
handler.removeEntity(this);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int 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;
}
public int getVelX() {
return velX;
}
public void setVelX(int velX) {
this.velX = velX;
}
public int getVelY() {
return velY;
}
public void setVelY(int velY) {
this.velY = velY;
}
public boolean isSolid() {
return solid;
}
public void setSolid(boolean solid) {
this.solid = solid;
}
public Id getId() {
return id;
}
public void setId(Id id) {
this.id = id;
}
public Rectangle getBounds(){
return new Rectangle(getX(), getY(), width, height);
}
public Rectangle getBoundsLeft() {
return new Rectangle(getX(), getY()+3, 3, height-6);
}
public Rectangle getBoundsRight() {
return new Rectangle(getX()+width-3, getY()+3, 3, height-6);
}
public Rectangle getBoundsTop() {
return new Rectangle(getX()+3, getY(), width-6, 3);
}
public Rectangle getBoundsBottom() {
return new Rectangle(getX()+3, getY()+height-3, width-6, 3);
}
}
敌人类:
public class SimpleEnemy extends Entity {
private Random r = new Random();
public SimpleEnemy(int x, int y, int width, int height, boolean solid, Id id, Handler handler) {
super(x, y, width, height, solid, id, handler);
int random = r.nextInt((int)1);
if(random == 0){
velX = 3;
}else if(random == 1){
velX = -3;
}
}
@Override
public void render(Graphics g) {
g.drawImage(Game.simplenemy.getBufferedImage(), x, y, width, height, null);
}
@Override
public void tick() {
x+=velX;
y+=velY;
for(int i=0;i<handler.tile.size();i++) {
Tile t = handler.tile.get(i);
if(t.isSolid()) {
if(getBoundsTop().intersects(t.getBounds())) {
setVelY(0);
}
if(getBoundsBottom().intersects(t.getBounds()) == false && velX > 0){
System.out.println("RIGHT");
}if(getBoundsBottom().intersects(t.getBounds()) == false && velX < 0){
System.out.println("LEFT");
}
else{
setVelY(0);
if(falling) falling = false;
y = t.getY() - getHeight();
System.out.println("SOMETHNI");
}
if(getBoundsLeft().intersects(t.getBounds()))
velX *= -1;
if(getBoundsRight().intersects(t.getBounds()))
velX *= -1;
}
}
if(jumping){
gravity-=0.4;
setVelY((int)-gravity);
if(gravity <= 0.5){
jumping = false;
falling = true;
}
}
if(falling) {
gravity += 0.4;
setVelY((int)gravity);
}
}
}
编辑: 处理程序类我称之为简单类:
int w = image.getWidth();
int h = image.getHeight();
for(int x = 0; x < w;x++ ){
for(int y = 0; y < h;y++){
int pixel = image.getRGB(x, y);
int red = (pixel >> 16)& 0xff;
int green = (pixel >> 8)& 0xff;
int blue = (pixel)& 0xff;
if(red==0&&green==0&&blue==0)addTile(new Wall(x * 64, y * 64, 64, 64, true, Id.Wall, this));
if(red==0&&green==0&&blue==255)addEntity(new Player(x * 64, y * 64, 64, 64, true, Id.Player, this));
if(red==255&&green==0&&blue==0)addEntity(new Mushroom(x*64, y*64, 64, 64, true, Id.Mushroom, this));
if(red==100&&green==100&&blue==0)addEntity(new SimpleEnemy(x*64,y*64,64,64,true,Id.SimpleEnemy,this));
}
}