Welcome, Guest. Please login or register.

Author Topic: A C++ Vectors question  (Read 3046 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
A C++ Vectors question
« on: August 27, 2007, 04:19:38 PM »
I'm writing a GP2X game in C++. I have created a class called "Enemy", and then created a vector of Enemy called "enemies". When an enemy is killed, the enemy is removed from the vector using "enemies.erase(enemies.begin()+i)". This works, but when the last enemy is killed (enemies becomes empty?) the game quits. Any ideas why this would happen?

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: A C++ Vectors question
« Reply #1 on: August 27, 2007, 04:53:04 PM »
Ok I coded around my problem with the game quitting. I just changed my for loops from 0 -> enemies.size()-1 to 1 -> enemies.size()-1. So enemies[0] never gets moved, shown or checked for collision. Because it can't be seen or destroyed, it remains in the vector, so all of the (visible) enemies can be destroyed and the game doesn't quit. I still want to understand this problem, but hey, it works now  :)  I will look in to lists when the time comes to optimize my game, but for now I want to get back to writing my game :D

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10