1
我想每次按下空格按鈕時重複RectangleShape rect1,但不是這樣做,它似乎只是刪除我的向量Vec的rect1對象我釋放空間鍵。我無法弄清楚爲什麼,有人幫助我嗎?無法創建使用向量push_back代碼的對象的副本
這裏是我的代碼:
int main() {
class shape {
public:
RectangleShape rect1;
};
shape getShape;
getShape.rect1.setSize(Vector2f(100, 100));
getShape.rect1.setFillColor(Color(0, 255, 50, 30));
RenderWindow window(sf::VideoMode(800, 600), "SFML Game");
window.setFramerateLimit(60);
window.setKeyRepeatEnabled(false);
bool play = true;
Event event;
while (play == true) {
while (window.pollEvent(event)) {
if (event.type == Event::Closed) {
play = false;
}
}
window.clear();
vector <shape> Vec;
if (Keyboard::isKeyPressed(Keyboard::Space)) {
Vec.push_back(getShape);
}
for (int i = 0; i < Vec.size(); ++i) {
window.draw(Vec[i].rect1);
}
window.display();
}
window.close();
return 0;
}
噢,我明白現在非常感謝幫助! – SSLukeY
你不想在這裏使用'Keyboard :: isKeyPressed' ...只是打印你的矢量的大小,你會明白爲什麼。改用事件。 – Hiura