void GS_Juego::HandleCollisions() {
unsigned i=0,j=0;
//Manejar colisión con armas de la nave
while(i< EnemyQueue.size()) {
while(j<WeaponsQueue.size()) {
if(EnemyQueue[i]->CheckCollision(WeaponsQueue[j])) {
//crear explosión en (GetX(),GetY()) <-----¡¡¡EXPLOSIóN AQUÍ!!!
delete EnemyQueue[i];
delete WeaponsQueue[j];
EnemyQueue.erase(EnemyQueue.begin()+i);
WeaponsQueue.erase(WeaponsQueue.begin()+j);
}
else {
j++;
}
}
i++;
}
i=0;
//Manejar colisión con nave
while(i<EnemyQueue.size()) {
if(EnemyQueue[i]->CheckCollision(Player1)) {
//crear explosión en (GetX(),GetY()) <-----¡¡¡EXPLOSIóN AQUÍ!!!
EnemyQueue.erase(EnemyQueue.begin()+i);
//Player1->addLives(-1);
//DESTRUIR ENEMIGOS
while(!EnemyQueue.empty()){
//crear explosion en (Get(),GetY()) <<---¡¡AquÍ otra!!
EnemyQueue.erase(EnemyQueue.begin());
}
}
i++;
}
}
This is the header with the declaration of the function and the two deques in black:
#ifndef GAMESTATE_JUEGO
#define GAMESTATE_JUEGO
#include "c_gamestate.h"
#include <allegro5\allegro5.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5\allegro_primitives.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_audio.h>
#include <allegro5\allegro_acodec.h>
#include "Constants.h"
#include "EnemyChaser.h"
#include "Player.h"
#include "WeaponLaser.h"
#include <deque>
using namespace std;
class GS_Juego :
public C_GameState
{
public:
void Init();
void Run();
void Shutdown();
GS_Juego(ALLEGRO_DISPLAY *display);
private:
void drawBackground();
void HandleEvents();
[b]void HandleCollisions();[/b]
void UpdateElements();
void RenderElements();
void ShootWeapon();
void SpawnEnemy(int EnemyType);
int create_random_number(const int min, const int max );
ALLEGRO_DISPLAY *display;
ALLEGRO_BITMAP *b_background, *b_enemychaser, *b_enemychaser_mask, *b_player1, *b_rotationBuffer, *b_laser;
ALLEGRO_SAMPLE *s_music;
ALLEGRO_EVENT_QUEUE *event_queue;
ALLEGRO_EVENT evento;
ALLEGRO_TIMER *timer;
//Background Effect
int background_Y;
//THINGS
bool done;
bool redraw;
bool calculateLogic;
bool keys[NUM_OF_KEYS];
int tc_weaponsDelay;
int SelectedWeapon;
[b] deque<C_Enemy*> EnemyQueue;
deque<C_Weapons*> WeaponsQueue;[/b]
Player *Player1;
};
#endif
I'd really aprecciate any feedback at all, because as far as I know, it should work perfectly, and I'm hitting my head against the wall repeatedly trying to find a solution. I checked the function collision and it works fine. The problem is that when it detects a collision with a bullet that it's not the first in the queue the program crashes, and if there is more than one enemy in the screen, if it's not the first one in the queue and I try to hit it with a bullet, it doesn't detect the collision. I'm sure the solution it's super silly, but I just can't find it. Please help! :-S











