将新精灵的位置设置为最后一个销毁的精灵的位置。

huangapple go评论68阅读模式
英文:

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() &lt; max_asteroids)
		{
			asteroid_vector.push_back(asteroid);
		}

		// Draw and move asteroids
		for (long unsigned int i = 0; i &lt; 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&amp; asteroid_dead, bool&amp; bullet_dead, float&amp; asteroid_scale, std::vector&lt;Bullet&gt; bullet_vector, bool&amp; level_2)
{
	for (unsigned int i = 0; i &lt; 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&amp; 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.

huangapple
  • 本文由 发表于 2023年5月29日 00:31:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定