按键创建新对象并将其添加到列表中(Creating a new object upon key press and adding it to a list)

编程入门 行业动态 更新时间:2024-10-27 00:33:42
按键创建新对象并将其添加到列表中(Creating a new object upon key press and adding it to a list)

我正在用c ++和sfml构建一个太空射击游戏。 现在我有一个子弹类,它只是一个带有一些运动逻辑的圆形。 我需要一种方法来存储子弹对象,绘制它们,并且一旦它们击中某些东西或超出范围,它们也会取消/删除它们。 我将它们添加到列表中,每个项目符号对象都有一个名为alive的bool属性,我根据行进的距离或与其他对象的碰撞不断更新。 每当碰撞或超出范围时,我都会将其设置为false。 然后我遍历列表以删除其alive属性为false的所有对象。 这听起来像是一个可怕的方法,并且在我从列表中删除后,子弹对象甚至会被释放?

这是我到目前为止的代码:

if (Keyboard::isKeyPressed(sf::Keyboard::T)) { Bullet newBullet(mySub.getSprite().getPosition().x + 64, mySub.getSprite().getPosition().y + 20); bulletList.push_back(newBullet); } list<Bullet>::iterator it = bulletList.begin(); while (it != bulletList.end()) { if (it->getNoDraw() == true) { bulletList.erase(it++); } else { it->update(); if (it->getBulletSprite().getPosition().x > mySub.getSprite().getPosition().x + 1200) { it->setNoDraw(); } } } cout << "list size " << bulletList.size() << endl; for (list<Bullet>::iterator it = bulletList.begin(); it != bulletList.end(); ++it) { window.draw(it->getBulletSprite()); }

我正在创建一个新的子弹对象并在每次按T时将其添加到bulletList中,但它实际上并没有将新的子弹添加到列表中,因为bulletList.size()始终打印0.任何想法我怎么能有效地做到这一点?

I'm building a space shooter game with c++ and sfml. Right now I have a bullet class which is just a circleshape with some movement logic to it. I need a way to store the bullet objects, draw them, and also undraw/delete them once they hit something or goes out of range. I'm adding them to a list and each bullet object has a bool attribute called alive which I constantly update based on distance traveled or collision with other objects. I set alive to false whenever it collides or goes out of range. I then iterate through the list to remove all objects whose alive attribute is false. This sounds like a horrible method though, and would the bullet objects even be freed after i delete them from the list?

Here is my code so far:

if (Keyboard::isKeyPressed(sf::Keyboard::T)) { Bullet newBullet(mySub.getSprite().getPosition().x + 64, mySub.getSprite().getPosition().y + 20); bulletList.push_back(newBullet); } list<Bullet>::iterator it = bulletList.begin(); while (it != bulletList.end()) { if (it->getNoDraw() == true) { bulletList.erase(it++); } else { it->update(); if (it->getBulletSprite().getPosition().x > mySub.getSprite().getPosition().x + 1200) { it->setNoDraw(); } } } cout << "list size " << bulletList.size() << endl; for (list<Bullet>::iterator it = bulletList.begin(); it != bulletList.end(); ++it) { window.draw(it->getBulletSprite()); }

I'm creating a new bullet object and adding it to the bulletList each time I press T, but it's not actually adding the new bullet to the list since bulletList.size() always prints 0. Any thoughts how I can do this effectively?

最满意答案

代码中存在一些错误。 您应重新读取erase()以获取list ,它将返回下一个迭代器。 当你没有摧毁子弹时,你也不知道在列表中前进了。

if (Keyboard::isKeyPressed(sf::Keyboard::T)) { Bullet newBullet(mySub.getSprite().getPosition().x + 64, mySub.getSprite().getPosition().y + 20); bulletList.push_back(newBullet); } list<Bullet>::iterator it = bulletList.begin(); while (it != bulletList.end()) { if (it->getNoDraw() == true) { it = bulletList.erase(it); // erase already advance it to the next } else { it->update(); if (it->getBulletSprite().getPosition().x > mySub.getSprite().getPosition().x + 1200) { it->setNoDraw(); } it++; // move on to the next position } } cout << "list size " << bulletList.size() << endl; for (list<Bullet>::iterator it = bulletList.begin(); it != bulletList.end(); ++it) { window.draw(it->getBulletSprite()); }

There are some faults in the code. You should reread erase() for list, it returns the next iterator. Also your missing advancing in the list when you do not destroy a bullet.

if (Keyboard::isKeyPressed(sf::Keyboard::T)) { Bullet newBullet(mySub.getSprite().getPosition().x + 64, mySub.getSprite().getPosition().y + 20); bulletList.push_back(newBullet); } list<Bullet>::iterator it = bulletList.begin(); while (it != bulletList.end()) { if (it->getNoDraw() == true) { it = bulletList.erase(it); // erase already advance it to the next } else { it->update(); if (it->getBulletSprite().getPosition().x > mySub.getSprite().getPosition().x + 1200) { it->setNoDraw(); } it++; // move on to the next position } } cout << "list size " << bulletList.size() << endl; for (list<Bullet>::iterator it = bulletList.begin(); it != bulletList.end(); ++it) { window.draw(it->getBulletSprite()); }

更多推荐

本文发布于:2023-07-28 04:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1300388.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按键   对象   列表中   并将其   Creating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!