我正在使用SFML进行C ++游戏,我想在屏幕上绘制一个冰球,当我按下鼠标左键时,它会在屏幕上看到它“走路”。但是当我按下按钮时,它不会画出冰球。你有解决问题的答案吗?
主:
#include <iostream>
#include <SFML/Graphics.hpp>
#include <string>
int main(int argc, char* argv[])
{
sf::RenderWindow window;
window.create(sf::VideoMode(1320, 840), "Game");
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
sf::Color color(255, 255, 255, 100);
sf::Time time = sf::seconds(2);
sf::Clock clock;
sf::Time time2 = clock.getElapsedTime();
std::cout << time.asSeconds() << std::endl;
std::cout << time2.asSeconds() << std::endl;
//clock.restart();
//TEXTURE
sf::Texture bgTexture;
if (!bgTexture.loadFromFile("file", sf::IntRect(0, 0, 1320, 840)))
{ std::cout << "Error: background not loaded" << std::endl; }
sf::Texture pTexture;
if (!pTexture.loadFromFile("file", sf::IntRect(0, 0, 32, 32)))
{ std::cout << "Error: player not loaded" << std::endl; }
sf::Texture ibTexture;
if (!ibTexture.loadFromFile("file"))
{ std::cout << "Error: ice-ball not loaded" << std::endl; }
//SPRITE
sf::Sprite background;
background.setTexture(bgTexture);
sf::Sprite player;
player.setTexture(pTexture);
player.setScale(2, 2);
player.setPosition(628, 388);
sf::Sprite iceBall;
iceBall.setTexture(ibTexture);
iceBall.setScale(1.5, 1.5);
//FONT
sf::Font font;
if (!font.loadFromFile("file"))
{ std::cout << "Error: font not loaded" << std::endl; }
//TEXT
sf::Text commands;
commands.setFont(font);
commands.setCharacterSize(54);
commands.setColor(sf::Color::Black);
commands.setString("D=Right \nA=Left \nW=Up \nS=DWON \n(also the arrows)");
commands.setPosition(213, 123);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
{ window.close(); }
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::D || event.key.code == sf::Keyboard::Right)
{ player.move(10, 0); }
else if (event.key.code == sf::Keyboard::A || event.key.code == sf::Keyboard::Left)
{ player.move(-10, 0); }
else if (event.key.code == sf::Keyboard::W || event.key.code == sf::Keyboard::Up)
{ player.move(0, -10); }
else if (event.key.code == sf::Keyboard::S || event.key.code == sf::Keyboard::Down)
{ player.move(0, 10); }
}
/*if (event.type == sf::Event::MouseMoved)
{
std::cout << "X: " << event.mouseMove.x << " y: " << event.mouseMove.y << std::endl;
}*/
if (event.type == sf::Event::MouseButtonPressed) {
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
std::cout << "Left mouse button pressed" << std::endl;
iceBall.setPosition(event.mouseMove.x, event.mouseMove.y);
window.draw(iceBall);
iceBall.move(event.mouseMove.x++, event.mouseMove.y++);
}
}
}
window.clear(color);
window.draw(background);
window.draw(player);
window.draw(commands);
window.display();
}
return 0;
}
答案 0 :(得分:2)
让我们来看看你的程序的工作流程:
因此,你实际上从未展示过球。
相反,按如下方式构建程序:
while (window.isOpen) {
sf::Event event;
while (window.pollEvent(event)) {
// Handle event & game *logic* here -- not drawing
// e.g. move entities
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {
iceBall.setPosition(event.mouseButton.x, event.mouseButton.y);
// CAREFUL HERE: you mixed wrong element of the union event.
}
}
// Draw everything here
window.clear();
window.draw(background);
window.draw(player);
window.draw(iceBall);
window.draw(command);
window.display();
}
附加说明:在您的代码中,您使用event.mouseMove
,而event
的类型不是鼠标移动。这是无效的。查看tutorial,尤其是此注释:
再次阅读上面的段落并确保你完全理解它,sf :: Event联合是没有经验的程序员的许多问题的原因。