【发布时间】:2014-04-11 14:43:09
【问题描述】:
我在代码块中使用 SFML 2.1,但我不知道如何使用向量来克隆我的小行星精灵。它一直说 asteroid_V 没有被声明,并且弹出一个警告框说它“使用了在所选编码中非法的字符”,并且它们“被更改以保护 [我] 不丢失数据”。
该程序的目标是不断创建小行星精灵,这些精灵将在屏幕上方的随机点生成,然后直接下降。程序中还有其他精灵和方面,但我从这篇文章中删除了它们以适当地浓缩它。毕竟这似乎是唯一的问题。
int n;
int main()
{
RenderWindow window;
window.setFramerateLimit(30);
RenderWindow mainMenu;
srand( time(0));
Texture asteroid_Pic;
asteroid_Pic.loadFromFile("Asteroid.png");
std::vector<sf::Sprite> asteroid(n, Sprite(asteroid_Pic));
for (int i = 0; i < asteroid.size(); i++){
asteroid[n].setOrigin(15, 15);
asteroid[n].getPosition();
asteroid[n].setPosition(x = rand() % 790 + 10, y = rand() % -10 - 50);
}
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == Event::Closed){
window.close();
}
asteroid[n].setPosition(x, y+=1);
asteroid[n].rotate(1);
// clear the window with black color
window.clear(Color::Black);
// draw everything here...
// window.draw(...);
window.draw(player1);
window.draw(asteroid[n]);
// end the current frame
window.display();
}
return 0;
}
【问题讨论】:
-
缩进是可读性的关键。
-
你的主循环在几个方面有问题(
whiles),请查看网站sfml-dev.org/tutorials/2.1上的教程。