简体   繁体   English

如何在java中测试多个交叉点?

[英]How to test for multiple intersections in java?

I am currently making a tower defence game.我目前正在制作一个塔防游戏。

Simply put it works like this: green circles (enemies) move across the screen.简单地说它的工作原理是这样的:绿色圆圈(敌人)在屏幕上移动。 By clicking, you can place a tower.通过点击,你可以放置一座塔。 The enemy has a rectangle hidden underneath it, and the tower has a large opaque rectangle around it indicating its hit range.敌人下方隐藏着一个矩形,塔周围有一个大的不透明矩形,指示其命中范围。 If they are colliding, the enemy starts to lose health, until it dies.如果它们发生碰撞,敌人就会开始失去生命值,直到它死亡。

Before, I used this test to see if the enemy was within range of the tower:之前,我用这个测试看看敌人是否在塔的范围内:

for(int i=0;i<enemy.length; i++) //Runs for the total amount of enemies
{
  for(int j=0; j<boxes.length;j++) //Runs for the total amount of towers placed
  {
    if(enemy[i].getEBox().intersects(boxes[j]))  //boxes[j] is the towers' range box
    {
      enemy.setHealth(enemy.getHealth()-1);
    }
  }
}

However, I would like the towers to only be able to shoot one enemy at a time, most preferably the enemy at the front.但是,我希望塔一次只能射击一个敌人,最好是正面的敌人。 To do this I need java to detect that there are multiple enemies colliding with the rectangle and only damage the front enemy.要做到这一点,我需要 java 来检测有多个敌人与矩形碰撞,并且只对前面的敌人造成伤害。

Here is the test I ran to do this (The enemies array value go backwards so the first one to appear is enemy[0] and the last enemy[10] for example):这是我运行的测试(例如,敌人数组值向后移动,因此出现的第一个是敌人 [0] 和最后一个敌人 [10]):

for(int i=1;i<enemy.length; i++) 
{
 for(int j=0; j<boxes.length;j++) 
  {
   if(enemy[i].getEBox().intersects(boxes[j])&&!(enemy[i-1].getEBox().intersects(boxes[j]))) 
    {
  enemy.setHealth(enemy.getHealth()-1);
    }
  }
}

however, the second condition always returns a positive.然而,第二个条件总是返回一个正数。 How can I change my if statement to conduct a successful test?如何更改我的 if 语句以进行成功的测试?

To make a tower only shoot at one enemy, flip the loops and break the inner loop when shooting starts.要使塔只向一个敌人射击,请在射击开始时翻转循环并打破内循环。

for(int j=0; j<boxes.length;j++) //Runs for the total amount of towers placed
{
  for(int i=0;i<enemy.length; i++) //Runs for the total amount of enemies
  {
    if(enemy[i].getEBox().intersects(boxes[j]))  //boxes[j] is the towers' range box
    {
      enemy[i].setHealth(enemy[i].getHealth()-1);
      break; // skip remaining enemies, since towers has used
             // up it's shooting capability for this round
    }
  }
}

If a tower can prioritize which enemy to shoot at, loop through all the enemies to find the best candidate, then shoot at it.如果塔可以优先射击哪个敌人,则遍历所有敌人以找到最佳候选者,然后对其进行射击。

for (int j=0; j<boxes.length; j++) //Runs for the total amount of towers placed
{
   int enemyToShootIdx = -1;
   for (int i=0; i<enemy.length; i++) //Runs for the total amount of enemies
      if (enemy[i].getEBox().intersects(boxes[j]))  //boxes[j] is the towers' range box
         if (enemyToShootIdx == -1 || betterCandidate(boxes[j], enemy[i], enemy[enemyToShootIdx]))
            enemyToShootIdx = i;
   if (enemyToShootIdx != -1)
      enemy[enemyToShootIdx].setHealth(enemy[enemyToShootIdx].getHealth()-1);
}

Now you just have to implement the betterCandidate() method.现在你只需要实现betterCandidate()方法。

You could store the enemies in the order you detect them so you will know which is the closest.您可以按照检测到的顺序存储敌人,以便您知道哪个距离最近。 Like an ArrayList for detected enemies or an ID kind of variable for each enemy object, which initialized when detected.就像检测到的敌人的 ArrayList 或每个敌人对象的 ID 类型的变量,在检测到时初始化。 Im pretty sure there are a lot ways like this, so storing an order somehow could work.我很确定有很多这样的方法,所以以某种方式存储订单可以工作。 Once you have an order you can attack only that one and there is no need to test the detected objects anymore while still looking for coming enemies and keep storing them so on.一旦你有一个命令,你就可以只攻击那个,不再需要测试检测到的物体,同时仍然寻找即将到来的敌人并继续存储它们。 In your case在你的情况下

if(enemy[i].getEBox().intersects(boxes[j])&&!(enemy[i- 1].getEBox().intersects(boxes[j])))

I don't really see (or maybe just too tired) why you need these conditions to attack.我真的不明白(或者可能只是太累了)为什么你需要这些条件来攻击。 If your array of enemies is already in order why dont you just attack the first?如果你的敌人已经排列整齐,你为什么不攻击第一个? If inRange && isAlive if not you go on the next one.如果 inRange && isAlive 如果不是,则继续下一个。 I hope i did not misunderstand anything and probably could help a bit.我希望我没有误解任何东西,可能会有所帮助。 :) :)

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

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