please help me with this logic (using a for loop with and if statement)

Started by
2 comments, last by graveyard filla 20 years, 1 month ago
high, ive recently added bombs to my pacman clone (dont ask ). anyway, i have a bomb class and i make an array of bombs for how many bombs are on a map. (4 since i only have one map so far). anyway, everything is fine and dandy, except when i take input, when the user goes to blow up a bomb, if he has more then one bomb on him, if he presses the bomb button, he will drop ALL of his bombs at once, then when he presses the button again, he blows up all the bombs at once. anyway, i fixed the problem by when i take input- i make it all one desision (IE ONE BIG IF STATEMENT), that way, if he drops one bomb, he will only be able to drop that one bomb per-frame, therfore he doesnt drop all the bombs at once and blow them all up at once. anyway, the only way i could do this was to hard-code my array.if what im saying so far makes no sence, this is a really simple question and im sorry if i confused you so far. heres what the code looks like:

 //if user is pressing space-bar....

//took out a big portion of the statement here

 else if (bomb[0].state == DROPPED)
 {
      bomb[0].state = BLOWING_UP;
 }
 else if (bomb[1].state == DROPPED)
 {
  bomb[1].state = BLOWING_UP;
 }
 else if (bomb[2].state == DROPPED)
 {
 bomb[2].state = BLOWING_UP;
  }
  else if (bomb[3].state == DROPPED)
  {
  bomb[3].state = BLOWING_UP;
 }
now, as you can see, i have hard-coded each element of the bomb array. i could have done:

//if user is pressing space bar

for (int i = 0; i < MAX_BOMBS(4) ; i++)
{
   if (bomb[i].state == DROPPED)
  {   
   bomb[i].state = BLOWING_UP;
  }

}
do you see the difference? the first one is very sloppy (i only showed you guys a small snippet of this if statement, i was kind enough to leave out a huge portion where this takes up a lot of room because im hard-coding it) anyway, the first one is very sloppy, but its ONE DECISION. so if bomb1 is blowing up when the user presses space bar, then bomb 1 will blow up, and it will BAIL OUT of the statement, so you wont blow up/drop all the bombs at once. now, the second version is much much cleaner, but its 4 SEPERATE DECISIONS, so the user presses space, and ALL of his bombs drop, which sucks because they will just all be on top of each other, and it looks like there is only 1 there. the explosion isnt cumulative or anything, either. so, is there a way to use a for loop and combine it with a single desision like this? or am i just stuck hard-coding it??thanks for any help!!!
FTA, my 2D futuristic action MMORPG
Advertisement
Just break out of the loop whenever you change a bomb''s state ...?
break; will work with a for loop?
FTA, my 2D futuristic action MMORPG
god damn... i cant believe i didnt think of this sooner *feels stupid*
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement