Java 前敌人运动
嗯,具体来说,我确实有正确的敌人移动,但我想实施一种方法,使敌人不会从悬崖上摔下来。 我尝试过几种方法,但都不起作用: 以下是我发现与问题相关的代码: 实体超类:Java 前敌人运动,java,awt,Java,Awt,嗯,具体来说,我确实有正确的敌人移动,但我想实施一种方法,使敌人不会从悬崖上摔下来。 我尝试过几种方法,但都不起作用: 以下是我发现与问题相关的代码: 实体超类: package com.cutthedamntree.main.entity; import java.awt.Graphics; import java.awt.Rectangle; import com.cutthedamntree.main.Id; import com.cutthedamntree.main.handle
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));
}
}
敌人等级:
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));
}
}
公共类simplenemy扩展实体{
私有随机r=新随机();
公共simplenemy(int x,int y,int width,int height,boolean solid,Id,Handler){
超级(x、y、宽度、高度、实体、id、处理器);
int random=r.nextInt((int)1);
如果(随机==0){
velX=3;
}else if(随机==1){
velX=-3;
}
}
@凌驾
公共空间渲染(图形g){
g、 drawImage(Game.simple敌手.getBuffereImage(),x,y,宽度,高度,null);
}
@凌驾
公共空白勾号(){
x+=velX;
y+=0;
对于(int i=0;i 0){
System.out.println(“右”);
}if(getBoundsBottom().intersects(t.getBounds())==false&&velX<0){
系统输出打印项次(“左”);
}
否则{
setVelY(0);
如果(下降)下降=错误;
y=t.getY()-getHeight();
System.out.println(“SOMETHNI”);
}
如果(getBoundsLeft().与(t.getBounds())相交)
velX*=-1;
如果(getBoundsRight().与(t.getBounds())相交)
velX*=-1;
}
}
如果(跳跃){
重力-=0.4;
设置((int)-重力);
如果(重力>16)&0xff;
绿色整数=(像素>>8)&0xff;
蓝色整数=(像素)&0xff;
如果(红色==0&&绿色==0&&蓝色==0)添加瓷砖(新墙(x*64,y*64,64,64,true,Id.Wall,this));
if(红色==0&&绿色==0&&蓝色==255)加法(新播放器(x*64,y*64,64,64,true,Id.Player,this));
if(红色==255&&绿色==0&&蓝色==0)加法(新蘑菇(x*64,y*64,64,64,true,Id.蘑菇,this));
if(红色==100&&绿色==100&&蓝色==0)加法(新的simplenemy(x*64,y*64,64,64,true,Id.simplenemy,this));
}
}
您确定getBoundsX方法正确吗?您可以共享调用SimpleNemy的构造函数的代码吗?当然:我从处理程序类调用SimpleNemy: