英文:
Set new sprite position equal to last destroyed sprite position
问题
在你的代码中,你想要在与子弹碰撞的陨石位置生成新的陨石。你已经尝试将spawn_new
函数中的asteroid_vector[i]
的位置传递给它,但仍然出现了问题。下面是可能的解决方案:
Asteroids.cpp
void Asteroids::collision(bool& asteroid_dead, bool& bullet_dead, float& asteroid_scale, std::vector<Bullet> bullet_vector, bool& level_2)
{
for (unsigned int i = 0; i < bullet_vector.size(); i++)
{
auto bullet = bullet_vector[i].bullet;
// If current bullet collides with asteroid,
// set asteroid_dead and bullet_dead to true
if (bullet.getGlobalBounds().intersects(asteroid.getGlobalBounds()))
{
asteroid_dead = true;
bullet_dead = true;
asteroid.scale(asteroid_scale, asteroid_scale);
level_2 = true;
// Get the position of the collided asteroid
sf::Vector2f asteroidPosition = asteroid.getPosition();
// Spawn a new asteroid at the same position
spawn_new(level_2, asteroidPosition);
}
}
}
void Asteroids::spawn_new(bool& level_2, const sf::Vector2f& position)
{
if (level_2)
{
asteroid.setPosition(position);
}
}
在collision
函数中,当碰撞发生时,获取了陨石的位置并传递给了spawn_new
函数。然后,在spawn_new
函数中,将新陨石的位置设置为与之前碰撞的陨石相同的位置。
请注意,spawn_new
函数的参数列表中添加了一个const sf::Vector2f& position
,以便传递位置信息给新生成的陨石。
英文:
I'm currently making Asteroids with SMFL c++. The collision detection between the bullet and the asteroid is working. I want a new asteroid to be spawned in the place where the one that was just hit was. But the new asteroid keeps spawning where the original asteroids are supposed to spawn.
P.S (Don't really know how to do an MRE with this because you'd need all my code to reproduce it)
Game.cpp
// Create asteroids
Asteroids asteroid(asteroid_width, asteroid_height);
if (level_2 == false)
{
asteroid.spawnAsteroids(screen_width, screen_height, asteroid_width, asteroid_height);
}
else
{
max_asteroids++;
asteroid.spawn_new(level_2);
level_2 = false;
}
asteroid.chooseAsteroidDirection();
asteroid.chooseTexture();
// Push asteroids to asteroid vector
if (asteroid_vector.size() < max_asteroids)
{
asteroid_vector.push_back(asteroid);
}
// Draw and move asteroids
for (long unsigned int i = 0; i < asteroid_vector.size(); i++)
{
asteroid_vector[i].drawTo(window);
asteroid_vector[i].moveAsteroids(dt);
asteroid_vector[i].screenWrapping(screen_width, screen_height);
asteroid_vector[i].collision(asteroid_dead, bullet_dead, asteroid_scale, bullet_vector, level_2);
// If bullet_dead is true despawn the bullet
if (bullet_dead)
{
bullet_vector.erase(bullet_vector.begin() + i);
bullet_dead = false;
}
Asteroids.cpp
void Asteroids::spawnAsteroids(int screen_width, int screen_height, int asteroid_height, int asteroid_width)
{
random_corner = rand() % 4;
if (random_corner == 0)
{
spawn_point.x = asteroid_width;
spawn_point.y = asteroid_height;
}
else if (random_corner == 1)
{
spawn_point.x = screen_width - asteroid_width;
spawn_point.y = asteroid_height;
}
else if (random_corner == 2)
{
spawn_point.x = screen_width - asteroid_width;
spawn_point.y = screen_height - asteroid_height;
}
else if (random_corner == 3)
{
spawn_point.x = asteroid_width;
spawn_point.y = screen_height - asteroid_width;
}
asteroid.setTexture(asteroid_texture_1);
asteroid.setPosition(spawn_point);
}
void Asteroids::collision(bool& asteroid_dead, bool& bullet_dead, float& asteroid_scale, std::vector<Bullet> bullet_vector, bool& level_2)
{
for (unsigned int i = 0; i < bullet_vector.size(); i++)
{
auto bullet = bullet_vector[i].bullet;
// If current bullet collides with asteroid,
// set asteroid_dead and bullet_dead to true
if (bullet.getGlobalBounds().intersects(asteroid.getGlobalBounds()))
{
asteroid_dead = true;
bullet_dead = true;
asteroid.scale(asteroid_scale, asteroid_scale);
level_2 = true;
}
}
}
void Asteroids::spawn_new(bool& level_2)
{
if (level_2)
{
asteroid.setPosition(asteroid.getPosition().x, asteroid.getPosition().y);
}
}
I want to set the new asteroids position to the same position as the one that just collided with the bullet. I tried doing this by plugging in the asteroid_vector[i]s position into the spawn_new function but it did the same thing.
答案1
得分: 1
为了做你想做的事情,你需要在小行星被删除之前保存它的位置。任何你删除的数据都将无法恢复。因此,如果你想在同一位置重新生成某物,你需要将该位置保存在外部的小行星位置向量中,然后从位置向量中删除该位置。
英文:
To do what you want to do you need to save the position of the asteroid before it gets deleted. any data that you delete will be deleted without being able to get it back. Thus if you want to spawn something back in that position you need to save the position in for example an external vector of vectof2f positions of asteroids that were just deleted, after which you delete that position from the position vector.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论