Hello! I'm trying to do a simple shoot 'em up game, but I've run into a problem.
What I'm trying to do is collision between bullets and enemies (invaders). With one bullet class, one invader class and one Collision class. I've gotten the collision to "kind of" work, meaning that one bullet can kill invaders, the problem I'm facing right now is, how do you make it so all the bullets can hit the invaders? I've tried returning a reference to the bullet vector, a pointer to the vector, the vector, the sprites in the vector and so on. I have identified the error in the code below, the problem is that the i value dosen't increase when I return the sprite.
Here's some example code.
Bullet.cpp
sf::Sprite Bullet::BulletPosition(){
for (int i = 0; i < bullets_straight.size(); ++i){
return bullets_straight[i];
}
}
Collision.cpp
bool Collision::SpriteCollision(sf::Sprite _sprite1, sf::Sprite _sprite2){
if (_sprite1.getPosition().x > _sprite2.getPosition().x || _sprite1.getPosition().x +8 < _sprite2.getPosition().x - 32
|| _sprite1.getPosition().y > _sprite2.getPosition().y + 32 || _sprite1.getPosition().y < _sprite2.getPosition().y - 32){
return false;
}
}
Invader.cpp
for (int i = 0; i < invaders.size(); ++i){
if (SpriteCollision(invaders[i], _sprite)){
invaders.erase(invaders.begin() + i);
check_direction.erase(check_direction.begin() + i);
}
}
}
I will make the collision better later on but right now I just, want to get it working.