java - 关于如何使用我这里的代码创建 "Object collision detection"有什么想法吗?

标签 java processing

我正在尝试制作一个游戏,如果主角(gubbe)与敌人(fiende)相撞,角色将受到伤害(Healthdisplay/HW) )。我尝试通过多种方式进行碰撞检测,主要是 if(x.intersects(y)){}。我被困住了,不知道如何解决。我把主要代码放在这里VVV。如果需要更多代码,只需说出并输入所需的代码。

HW 是应该造成损坏的生命值池。

创建主要角色/矩形时显示无效。

敌人正在另一个页面中处理。

class gubbe {

    float x = 0;
    float y = 0;
    int HW = 20;
    //rect sidor
    int h1 = 100;
    int w1 = 100;
    //----

    //VV movement VV
    void move() {

    if (keyPressed) {
        if (key == 'w' || key == 'W') {
            //terms, move forward, Y-led.
            y -= 100;
        }
    }
    if (keyPressed) {
        if (key == 'a' || key == 'A') {
            //terms, move right, X-led.
            x -= 100;
        }
    }
    if (keyPressed) {
        if (key == 's' || key == 'S') {
            //terms, move backwards, Y-led.
            y += 100;
        }
    }
    if (keyPressed) {
        if (key == 'd' || key == 'D') {
            //terms, move left, X-led.
            x += 100;
        }
    }
}

// VV visual VV
void display() {
    //Grabb
    fill(127);
    stroke(0);
    rect(x, y, w1, h1);
}

void display2() {
    fill(127);
    stroke(0);
    rect(x, y, w1, h1);
}

// VV Edgechechning VV
void checkEdges() {
    if (x + 100 > width) {
        x -= 50;
    } else if (x < 0) {
        x += 50;
    } else if (y > height) {
        y -= 50;
    } else if (y < 0) {
        y += 50;
    } else {
    }
}

// VV Fighting VV
void Fight() {
    if (keyPressed) {

        if (key == 'm'|| key == 'M') {
            //villkor, flytta höger, X-led.
            fill(255, 0, 0);
            stroke(255, 0, 0);
            rect(x, y-100, 100, 100);
        }
    }
}

// VV  health bars VV
void BarsDisplay() {
    fill(204, 204, 204);
    stroke(204, 204, 204);
    rect(x, y-30, 100, 9);
}

void HealthDisplay() {
    fill(0, 0, 255);
    stroke(255, 0, 0);
    rect(x, y-31, HW, 3);
    !!! here there should be collision detection!!!

}

void XpDisplay() {
    fill(255, 255, 0);
    stroke(255, 255, 0);
    rect(x, y-22, 2, 1);
    //if(om dödar fiende){
    //  width+=x
    //}
    //else{
    //}
}
}

最佳答案

您的对象看起来是矩形形状,因此如果沿 x 轴和 y 轴的两个中心之间的距离小于所添加的该轴方向上每个对象大小的一半,则会发生碰撞向上。

拿一张纸画下来,你就会看到它!

关于java - 关于如何使用我这里的代码创建 "Object collision detection"有什么想法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58067158/

相关文章:

java - 与 Guava HashBiMap 和 synchronizedBiMap 同步

java - 如何让球顺利滚出角落?

java - 为什么会出现这个错误? - 加工

azure - 在 azure 网站上运行processing.js

java - 在java中使用slick2D库的xml FileNotFoundException

java - 如何为JAXB创建的java文件创建jar?

java - Java 中的 SimpleDateFormat 异常

java - okhttp validator 是否处理 403 错误?

php - MySQL 数据到 Arduino

java - 当一个矩形悬停在其上方时,如何对其进行专门着色?