我想创建一个数组,它将包含将被绘制到窗口上的所有精灵,文本和形状,我的问题是如何使这个数组同时使用sf :: Drawable和sf :: Transformable?
答案 0 :(得分:1)
您需要创建一个继承Drawable
和Transformable
的类。然后你就可以创建该类的数组了。
class Obj : public sf::Drawable, public sf::Transformable
{
// class code
}
// somewhere in code...
std::array<Obj, ARRAY_SIZE> arr;
确保正确实施Drawable
和Transformable
。
以下是官方文档的链接。
Transformable&amp; Drawable
实现这些类的一种方法是:
class Obj : public sf::Drawable, public sf::Transformable
{
public:
sf::Sprite sprite;
sf::Texture texture;
// implement sf::Drawable
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(sprite, states); // draw sprite
}
// implement sf::Transformable
virtual void SetPosition(const MyVector& v) const
{
sprite.setPosition(v.x(), v.y());
}
}
然后在您的代码中,您可以直接绘制和转换类。
// somewhere in code
// arr = std::array<Obj, ARRAY_SIZE>
for (auto s : arr) {
window.draw(s);
}