相交功能在第一次相交后停止工作

Intersect function stops working after the first intersect

我正在制作游戏,但我 运行 在使用函数 "intersect" 时遇到了问题。 senario 看起来像这样;我做了一个游戏,其中玩家是一个矩形,objective 是杀死敌人的矩形。敌人 "boss" 矩形有两个 "simulations" 模拟移动和攻击,这些模拟由矢量驱动。运动是水平来回的,攻击是垂直的,是冲锋式的打法。 Boss 也有一个矩形目标区域,如果在那里交叉,"boss" 将冲过屏幕。 现在问题来了,当我试图做到这一点时,如果玩家与矩形目标区域相交,"boss" 将 attack/charge。 Boss attack/charge 玩家第一次相交但之后就没有了。我希望老板遵循相同的模式,他应该只能水平地左右移动,并且如果目标区域相交; attack/charge垂直度。

(我将在下面包含一些代码。抱歉,如果我的英语不好。) 首先是主要的:

boss b;
Character C;

void setup(){
C = new Character();
b = new boss();
}

void draw(){
if (play) {
    b.simulate();    //horizantal movement
}

if (b.start) {
    b.sim();             //boss vertical attack
}
if (b.intersects(C)){

    play = false;
    b.start = true;
}
C.character();             //player
b.bounce();               //makes it bounce if horizantal. and stop if vertical
b.Display();              //boss
b.display();             //boss target area
}

接下来是老板:

class boss {
int x = 10 ;
int y = 10 ;
boolean start = false;
int RW = 50;
int RH = 700;
boolean up = false;
boolean down = true;

boss() {

  Location = new PVector( x+25, y );   //vector for the location of the boss
  Velocity = new PVector( 5, 0 );      // vector for horizaltal movement
  speed = new PVector( 0, 10 );        // vector for vertical down movement
  speed2 = new PVector(0, -10);       // vector for vertical up movement
}
void bounce() {
  if ((Location.x == width) ||(Location.x == 0)) {   //horizantal movement bounce on screen edge
    Velocity.x = Velocity.x * -1;

}
if ((Location.y == 650) || (Location.y == 0)) {
  start = false;                            //makes boss stop if reaches botton or top of the screen
  play = true;
  if (Location.y == 650) {                   
    RH = -700;
    up = true;
    down = false;      //specificly if it reaches top of screen
  }
  if (Location.y == 0) {
    RH = 700;                               
    down = true;       //specificly if it reaches bottom of screen
    up = false;
  }
  }
  }
void simulate() {
  Location.add(Velocity);    //simulates horizantal movement
}
void sim() {
  if (down) {
    Location.add(speed);      //simulates up and down attacking movemnet
  }
  if (up) {
  Location.add(speed2);
  }
}
boolean intersects(Character C) {

  return  Location.x < C.x + C.w && C.x < Location.x + RW && 
  Location.y < C.y + C.h && C.y < Location.y + RH; //intersect between player and boss targeting area

}
void Display() {
  rect( Location.x, Location.y, 50, 50 );   //boss
}
void display() {
  rect( Location.x, Location.y+50, RW, RH );   //boss targeting area

}

}

如果有任何不清楚的地方,我很乐意澄清任何困惑。 :)

感谢您分享代码。它使它变得容易得多。

当我评论这篇文章时(不是 if-condition if 之后 start 的重置)

if ((Location.y == 650) || (Location.y == 0)) { // start = false;

然后老板开始回到顶部,当坐标匹配时仍然调用与角色相交。不过,post这个boss一直在蹦蹦跳跳

考虑到发射的子弹和对 boss 的命中,肯定还有更多工作要做。

希望这对您有所帮助。 :) 调试这段代码很有趣。在此之前我从未使用过 PDE。

好的,这是 "edit".. 现在,

好消息:只有相交的时候我才能做到双向

坏消息:只要相交,它就会一直往另一边走。因此,如果Character是静止的,那么boss会以当前速度保持至少8-10次相交并通过另一侧。

无论如何,这里是总结。我添加了 isAttacking 标志,告诉程序如果老板到达框架底部就不要阻止老板越过。我改变的另一件事是相交条件。现在它只检查 X 轴上的交点。如果您也必须比较 Y 轴上的交点,那么 intersects 就是您需要更改和测试的地方。

经过冗长的解释:P 这是代码。希望这会更好。

主要

boss b;
Character C;
Inventory I;
Bullet B;
int previousKey = 0;
int lastKey;
int lastKeyCode;



void setup() {

  C = new Character();
  b = new boss();
  I = new Inventory();
  background(128, 128, 128);
  size( 700, 700 );
  strokeWeight( 10 );
  frameRate( 30 );
}
void keyPressed() {

  if (key == CODED) {
    previousKey = keyCode;
    if (keyCode == UP) {
      C.MoveUP();
    }
    if (keyCode == LEFT) {
      C.MoveLEFT();
    }
    if (keyCode == DOWN) {
      C.MoveDOWN();
    }
    if (keyCode == RIGHT) {
      C.MoveRIGHT();
    }
  }

  if (key == 'w' || key == 'W') {
    attack();
  }
  if ( key == 'q' || key == 'Q' ) {
    if (I.Shoot == true) {
      B = new Bullet(C.x, C.y);
      this.Shoot();
    }
  } else if (key == 'e' || key == 'E') {
    I.changePop();
  }
  if (keyPressed) {
    if (key == 'a' || key == 'A') {
      //play = false;
      //b.start = true;
    }
  }
}


void attack() {
  if (I.Attack == true) {
    if (previousKey == UP) {
      C.AttackUP();
    }
    if (previousKey == LEFT) {
      C.AttackLEFT();
    }
    if (previousKey == DOWN) {
      C.AttackDOWN();
    }
    if (previousKey == RIGHT) {
      C.AttackRIGHT();
    }
  }
}
void Shoot() {

  if (I.Shoot == true) {
    if (previousKey == UP) {
      B.ShootUP();
    }
    if (previousKey == LEFT) {
      B.ShootLEFT();
    }
    if (previousKey == DOWN) {
      B.ShootDOWN();
    }
    if (previousKey == RIGHT) {
      B.ShootRIGHT();
    }
  }
}

boolean play = true;
void keyReleased() {
  lastKey = 0;
  lastKeyCode = 0;
}

void draw() {
  background(128, 128, 128);


  if (play) {
    b.simulate();//side to side
  }

  if (b.start) {
    b.sim();      //boss rush
  }
  if (b.intersects(C)) {
    b.isAttacking = true;
    play = false;
    b.start = true;
  } else {
    b.isAttacking= false;
  }
  C.character();
  b.bounce();
  b.Display();//boss
  b.display();//rush area
  C.HPbar();
  I.popUp();
  if ( key == 'q' || key == 'Q' ) {
    if (I.Shoot == true) {
      B.bullet();
      B.Simulate();
    }
  }
}

敌人Boss

class boss {
  PVector Location;
  PVector Velocity;
  PVector speed;
  PVector speed2;
  int x = 10 ;
  int y = 10 ;
  boolean start = false;
  int RW = 50;
  int RH = 700;
  boolean up = false;
  boolean down = true;

  boolean isAttacking = false;

  boss() {

    Location = new PVector( x+25, y );
    Velocity = new PVector( 5, 0 );
    speed = new PVector( 0, 10 );
    speed2 = new PVector(0, -10);
  }
  void bounce() {
    if ((Location.x == width) ||(Location.x == 0)) {
      Velocity.x = Velocity.x * -1;
    }
    if ((Location.y == 650) || (Location.y == 0)) {
      if (!isAttacking) {
        start = false;
      }
      play = true;
      if (Location.y == 650) {
        RH = -700;
        up = true;
        down = false;
      }
      if (Location.y == 0) {
        RH = 700;
        down = true;
        up = false;
      }
    }
  }

  void simulate() {

    Location.add(Velocity);
  }
  void sim() {
    //print("\n In Sim UP: [" + up + "] Down: [" + down + "] Location [" + Location + "]");
    if (down) {
      Location.add(speed);
    }
    if (up) {
      Location.add(speed2);
    }
  }





  boolean intersects(Character C) {
    //print ("\nUP: [" + up + "] Down: [" + down + "] X: [" + (Location.x < (C.x + C.w) && (Location.x + RW) > C.x) 
    //+ "] Y: [" + (Location.y < (C.y + C.h) && (Location.y + RH) > C.y) + "]");

    return  Location.x < (C.x + C.w) && (Location.x + RW) > C.x;  
    //&& 
    //  Location.y < (C.y + C.h) && (Location.y + RH) > C.y ;
  }



  void Display() {
    pushStyle();
    stroke(0);
    fill(255, 0, 0);
    rect( Location.x, Location.y, 50, 50 );

    popStyle();
  }
  void display() {
    pushStyle();
    stroke(0);
    strokeWeight(0);
    fill(255, 0, 0, 20);
    rect( Location.x, Location.y+50, RW, RH );
    popStyle();
  }
}