简体   繁体   English

单独类实例的碰撞检测-Java处理

[英]Collision detection for separate class instances - Processing Java

I have a class which spawns an enemy, and a class that spawns a player. 我有一个产生敌人的职业,还有一个产生玩家的职业。 At the moment they are both 2 circles coming towards each other. 目前,他们都是两个互相靠近的圈子。 I have made a collision detection function for when they crash, but I can't make it work. 我为它们崩溃时提供了碰撞检测功能,但是我无法使其正常工作。 I have made the function outside the classes and try to call the position variables of each class, but it says I can't make a static reference to a non static field. 我已经在类之外创建了函数,并尝试调用每个类的位置变量,但是它说我不能对非静态字段进行静态引用。 I have also tried the collision detection in each class which also doesn't work. 我还尝试了每个类中的碰撞检测,这也行不通。

class Player {
    int Pos_X;
    int Pos_Y;
    int Speed;

    Player(int x, int y, int speed) {
        this.Pos_X = x;
        this.Pos_Y = y;
        this.Speed = speed;
    }

    void Update() {
        ellipse(Pos_X, Pos_Y, 40, 40);
        Pos_X += Speed;
    }
}

class Enemy {
    int Pos_X;
    int Pos_Y;
    int Speed;

    Enemy(int x, int y, int speed) {
        this.Pos_X = x;
        this.Pos_Y = y;
        this.Speed = speed;
    }

    void Update() {
        ellipse(Pos_X, Pos_Y, 40, 40);
        Pos_X -= Speed;
    }
}

Player Player1;
Enemy Enemy1;
void setup() {
    size (500, 500);
    Player1 = new Player (0, 200, 3);
    Enemy1 = new Enemy (500, 200, 3);
}

void draw() {
    background(200);
    Player1.Update();
    Enemy1.Update();
    if (intersectsBox(Player.Pos_X, Player.Pos_Y)) { 
        ellipse(100, 100, 100, 100);
    }
}

boolean intersectsBox(float X, float Y) {
    if (X > Enemy.Pos_X && X < Enemy.Pos_X + 40) {
        if (Y > Enemy.Pos_Y && Y < Enemy.Pos_Y + 40) {
          return true;
        }
    }
}

To use a variable that's in a class, you have to refer to an instance of that class. 要使用类中的变量,您必须引用该类的实例 In your code, Enemy is a class, and Enemy1 is an instance of that class. 在您的代码中, Enemy是一个类, Enemy1是该类的实例。 So when you try to do this: 因此,当您尝试执行此操作时:

if (X > Enemy.Pos_X && X < Enemy.Pos_X + 40) {

Processing gives you an error because you're using the class, not an instance. 处理会给您一个错误,因为您使用的是类,而不是实例。 Processing doesn't know which Enemy you're talking about. 处理不知道您在说哪个敌人 Instead, you have to do something like this: 相反,您必须执行以下操作:

if (X > Enemy1.Pos_X && X < Enemy1.Pos_X + 40) {

Now Processing knows which instance of the Enemy class you're talking about. 现在,Processing知道您正在谈论的Enemy类的哪个实例。 You might also pass in an instance of Enemy as a parameter to that function, that way you can use it to collide with multiple enemies. 您也可以将Enemy的实例作为该函数的参数传递,这样您就可以使用它与多个敌人碰撞。

And just a general piece of advice: your code would be much easier to read if you followed standard formatting and naming conventions- indent your code (Processing will do it for you if you press ctrl+t in the editor), and make sure only classes start with an upper-case letter. 这只是一条一般性建议:如果遵循标准格式和命名约定,您的代码将更容易阅读-缩进代码(如果在编辑器中按ctrl + t,则处理将为您完成)。类以大写字母开头。 Methods and variables should start with lower-case letters. 方法和变量应以小写字母开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM