@d_stranz I wanted to say thanks for the help i have learned a lot thanks to you.
i tested what u said and i did:
if(spaceships[0]->y() >= 100 && spaceships.size() == 5){
qDebug() << "you lost";
Spaceship * spaceship = spaceships[ 1 ];
scene->removeItem(spaceship);
spaceships.remove(1); // i have tried Spaceships.remove(spaceship) but it says: cannot convert QVector::remove() Spaceship * to int // i dont know why tho
delete(spaceship);
}
if(spaceships[0]->y() >= 100 && spaceships.size() == 5){
qDebug() << "you lost";
Spaceship * spaceship = spaceships[ 1 ];
scene->removeItem(spaceship);
spaceships.remove(1); // i have tried Spaceships.remove(spaceship) but it says: cannot convert QVector::remove() Spaceship * to int // i dont know why tho
delete(spaceship);
}
To copy to clipboard, switch view to plain text mode
anyway when i tested it , it did remove the second spaceship from the row and kept moving from side to side. but when i added a function:
void Game::delSpaceshipVector(int x)
{
Spaceship * spaceship = spaceships[x-1];
scene->removeItem(spaceship);
spaceships.remove(x-1);
delete(spaceship);
return;
}
void Game::delSpaceshipVector(int x)
{
Spaceship * spaceship = spaceships[x-1];
scene->removeItem(spaceship);
spaceships.remove(x-1);
delete(spaceship);
return;
}
To copy to clipboard, switch view to plain text mode
this function is called by this:
void Bullet::move()
{
QList<QGraphicsItem *> SpaceshipHit= collidingItems();
for(int i = 0, n = SpaceshipHit.size(); i < n; i++){
if(typeid(*(SpaceshipHit[i])) == typeid(Spaceship)){
scene()->removeItem(this);
game->delSpaceshipVector(i);
delete this;
return;
}
}
setPos(x(),y()-10);
}
void Bullet::move()
{
QList<QGraphicsItem *> SpaceshipHit= collidingItems();
for(int i = 0, n = SpaceshipHit.size(); i < n; i++){
if(typeid(*(SpaceshipHit[i])) == typeid(Spaceship)){
scene()->removeItem(this);
game->delSpaceshipVector(i);
delete this;
return;
}
}
setPos(x(),y()-10);
}
To copy to clipboard, switch view to plain text mode
the last fucntion cause the bullet move forward when it collide type"spaceship" it is delete the bullet object then goes to delSpaceshipVector func inside game.cpp-> in delSpaceshipVector function does delete spaceship POINTER, spaceship from the scene and the object it self. when i tried the first code in this message it did worked. but when i used another function that calls from another cpp file (bullet.cpp) it crushed and says:
"QVector index out of range"
Bookmarks