如何在屏幕上多次复制一个精灵,每个精灵都具有相同的代码?
How do I duplicate a sprite on screen multiple times with each sprite having the same code?
我正在尝试在屏幕上复制 sprite,但如果不制作多个 sprite,我无法做到这一点。
我发现其他人也问过同样的问题,但我想做的是,例如,单击 window 中的某处并使精灵出现在那里。我尝试在屏幕上的两个位置绘制该精灵(有效),但我还为精灵和玩家精灵添加了碰撞,出于某种原因,这并没有阻止玩家进入精灵。
#include <SFML/Graphics.hpp>
#include <SFML/Window/Keyboard.hpp>
bool collision(sf::FloatRect r1, sf::FloatRect r2)
{
sf::FloatRect intersection;
return r1.intersects(r2, intersection);
}
bool collision(sf::Shape const & r1, sf::Shape const & r2)
{
return collision(r1.getGlobalBounds(), r2.getGlobalBounds());
}
int main(){
sf::RenderWindow window(sf::VideoMode(800,600),"INSERT_WINDOW_TITLE",
sf::Style::Titlebar | sf::Style::Close);
sf::RectangleShape player(sf::Vector2f(20.f,20.f));
player.setFillColor(sf::Color::Blue);
sf::RectangleShape rect(sf::Vector2f(20.f,20.f));
rect.setFillColor(sf::Color::Red);
int mousex = 400;
int mousey = 240;
rect.setPosition(400,240);
while(window.isOpen()){
sf::Event event;
if(collision(player,rect) == false){
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
player.move(0.f,-1.f);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
player.move(0.f,1.f);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
player.move(-1.f,0.f);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
player.move(1.f,0.f);
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){
mousex = sf::Mouse::getPosition().x;
mousey = sf::Mouse::getPosition().y;
}
while(window.pollEvent(event)){
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Black);
window.draw(player);
rect.setPosition(400,240); // did not have any collision with this rect
window.draw(rect);
rect.setPosition(mousex,mousey); // I set this position after the
previous one, which is why it HAD collision
window.draw(rect);
window.display();
}
}
如您所见,我在 "error" 发生的地方添加了注释。问题是只有第二个 sf::RectangleShape
发生了碰撞,并且精灵被绘制得太快而无法进行任何真正的碰撞(或者至少这是我的猜测)。 (我正在考虑复制精灵而不复制代码 1000 次)我将如何解决这个问题?
问题是您每个循环只检查一次碰撞。因此,如果您两次绘制矩形,则只有其中一个会发生碰撞。要解决此问题,您应该检查
if(collision(player,rect) == false)
顺便说一下,可以缩写为
if(!collision(player,rect))
在此行上更改矩形的位置后再次:
rect.setPosition(mousex,mousey); // I set this position after the previous one, which is why it HAD collision
我正在尝试在屏幕上复制 sprite,但如果不制作多个 sprite,我无法做到这一点。
我发现其他人也问过同样的问题,但我想做的是,例如,单击 window 中的某处并使精灵出现在那里。我尝试在屏幕上的两个位置绘制该精灵(有效),但我还为精灵和玩家精灵添加了碰撞,出于某种原因,这并没有阻止玩家进入精灵。
#include <SFML/Graphics.hpp>
#include <SFML/Window/Keyboard.hpp>
bool collision(sf::FloatRect r1, sf::FloatRect r2)
{
sf::FloatRect intersection;
return r1.intersects(r2, intersection);
}
bool collision(sf::Shape const & r1, sf::Shape const & r2)
{
return collision(r1.getGlobalBounds(), r2.getGlobalBounds());
}
int main(){
sf::RenderWindow window(sf::VideoMode(800,600),"INSERT_WINDOW_TITLE",
sf::Style::Titlebar | sf::Style::Close);
sf::RectangleShape player(sf::Vector2f(20.f,20.f));
player.setFillColor(sf::Color::Blue);
sf::RectangleShape rect(sf::Vector2f(20.f,20.f));
rect.setFillColor(sf::Color::Red);
int mousex = 400;
int mousey = 240;
rect.setPosition(400,240);
while(window.isOpen()){
sf::Event event;
if(collision(player,rect) == false){
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
player.move(0.f,-1.f);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
player.move(0.f,1.f);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
player.move(-1.f,0.f);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
player.move(1.f,0.f);
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){
mousex = sf::Mouse::getPosition().x;
mousey = sf::Mouse::getPosition().y;
}
while(window.pollEvent(event)){
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Black);
window.draw(player);
rect.setPosition(400,240); // did not have any collision with this rect
window.draw(rect);
rect.setPosition(mousex,mousey); // I set this position after the
previous one, which is why it HAD collision
window.draw(rect);
window.display();
}
}
如您所见,我在 "error" 发生的地方添加了注释。问题是只有第二个 sf::RectangleShape 发生了碰撞,并且精灵被绘制得太快而无法进行任何真正的碰撞(或者至少这是我的猜测)。 (我正在考虑复制精灵而不复制代码 1000 次)我将如何解决这个问题?
问题是您每个循环只检查一次碰撞。因此,如果您两次绘制矩形,则只有其中一个会发生碰撞。要解决此问题,您应该检查
if(collision(player,rect) == false)
顺便说一下,可以缩写为
if(!collision(player,rect))
在此行上更改矩形的位置后再次:
rect.setPosition(mousex,mousey); // I set this position after the previous one, which is why it HAD collision