We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Strange hitTest with an Array list
Page Index Toggle Pages: 1
Strange hitTest with an Array list (Read 1949 times)
Strange hitTest with an Array list
Dec 13th, 2009, 11:25am
 
Hello again, my game program is hitting wall after walls of bugs, this time I've tried to apply a hitTest conditonal to an ArrayList of enemies the player must avoid. Out of nowhere the player dies while not being hit by any of the minions, my theory is that he's doing hitTests with all enemies at once. How could I fix this?

Here's the code:

Code:

int screenSize, arraySize, timer;

boolean startGame;
boolean pauseGame;
boolean endGame;

SPACE zone;
AVATAR player;
MINION enemy;
ArrayList enemies;

void setup()
{
 screenSize = 400;
 size (screenSize,screenSize);
 frameRate(30);
 
 startGame = false;
 pauseGame = false;
 endGame = false;
 
 zone = new SPACE
 (
 loadImage("figura1.png"), loadImage("figura2.png"),
 loadImage("figura3.png"), color(27,214,219), (screenSize/2),
 (screenSize/2), (screenSize/15)
 );
 
 player = new AVATAR
 (
 color(31,155,16), (screenSize/2), (screenSize/2),
 (screenSize/15),(screenSize/80)
 );
 
 enemies = new ArrayList();
}

void mousePressed()
{
 startGame =! startGame;
 pauseGame = true;
 
 if(endGame == true)
 {
   endGame = false;
   timer = 0;
 }
}

void keyPressed()
{
 if(key == 't')
 {
   endGame =! endGame;
 }
}

void draw()
{
 background(color(#141B5D));
 //smooth();
 zone.display();
 
 if (endGame == false)
 {
   player.avatarSize = screenSize/15;
   player.display();
   
   if (startGame == true)
   {
     zone.alphaTest = true;
     timer++;
     player.move();
   
     if(timer % 100 == 0 && enemies.size() < 10)
     {
       enemies.add (new MINION(color(193,12,12), player.xpos,
       player.ypos, player.avatarSize, (screenSize/15),
       (screenSize/80)));
     }
     
     for(int i = enemies.size()-1; i >= 0; i--)
     {
       MINION enemy = (MINION) enemies.get(i);
       println(enemies);
       enemy.move();
       enemy.display();
       
       if(enemy.xpos + (enemy.minionSize/2) >= player.xpos - (player.avatarSize/2) &&
          enemy.xpos + (enemy.minionSize/2) <= player.xpos + (player.avatarSize/2) ||
          enemy.xpos - (enemy.minionSize/2) >= player.xpos - (player.avatarSize) &&
          enemy.xpos - (enemy.minionSize/2) <= player.xpos + (player.avatarSize))
          {
            if(enemy.ypos + (enemy.minionSize/2) >= player.ypos - (player.avatarSize/2) &&
               enemy.ypos + (enemy.minionSize/2) <= player.ypos + (player.avatarSize/2) ||
               enemy.ypos - (enemy.minionSize/2) >= player.xpos - (player.avatarSize/2) &&
               enemy.ypos - (enemy.minionSize/2) <= player.xpos + (player.avatarSize/2))
               {
                 endGame = true;
               }
          }
      }
   }
     
   if(pauseGame == true && startGame == false)
   {
     zone.pause();
   }
 }
 
 if(endGame == true)
 {
   startGame = false;
   pauseGame = false;
   
   player.end();
   zone.end();
   
   /*for(int i = enemies.size()-1; i >= 0; i--)
   {
     MINION enemy = (MINION) enemies.get(i);
     enemy.end();
   }
   
   if(enemy.materialize == screenSize)
   {*/
     for(int i = enemies.size()-1; i >= 0; i--)
     {
       enemies.remove(i);
     }
 //}
 }
}
Re: Strange hitTest with an Array list
Reply #1 - Dec 13th, 2009, 1:18pm
 
Why not just use the dist function:

if(dist(player.xpos, player.ypos, enemy.xpos, enemy.ypos)
<player.avatarSize/2 + enemy.minionSize/2)

It would replace all those cumbersome statements.....and it would be easier to figure out what is going wrong....
Re: Strange hitTest with an Array list
Reply #2 - Dec 13th, 2009, 3:35pm
 
Something else you could do to simplify your main draw loop; though not a direct solution to this problem:  put the collision detect code into the enemy and get it to return a boolean:

Code:
// add method to MINION class
boolean checkHit() {
if (enemy in range of player) {
return true;
}
else {
return false;
}
}


Then in draw all you need is:

Code:
 for(int i = enemies.size()-1; i >= 0; i--)	{
MINION enemy = (MINION) enemies.get(i);
println(enemies);
enemy.move();
enemy.display();
if(enemy.checkHit()) {
endGame = true;
}


As for solving the problem: knowing which enemy triggers the hit event may help identify the problem.  If you follow the above suggestion you could just add code to draw something (e.g. a rect) at the enemy position before 'return true;'.
Re: Strange hitTest with an Array list
Reply #3 - Dec 13th, 2009, 4:08pm
 
Blindfish, I've became interested in your method, but how do I get the position and size ints from the player class into the minion class?
Re: Strange hitTest with an Array list
Reply #4 - Dec 14th, 2009, 1:44am
 
Well - you declare your AVATAR object 'player' globally so you can reference its properties directly in MINION with 'player.propertyName' just as you would anywhere else.  For the MINIONS themselves, because you're writing this directly in the class, you can just reference the property without any object name: e.g. 'xpos'.

I've just realised I didn't take this approach in my Asteroids classes and am wondering why - I'm sure I had a good reason at the time Huh
Re: Strange hitTest with an Array list
Reply #5 - Dec 14th, 2009, 9:28am
 
Problem solved. Thanks for the help everyone! The problem was at a shameful Ctrl+C/Ctrl+V I've done at the lines 96 and 97, makind the y coordinates make hitTest with the x coordinates
Page Index Toggle Pages: 1