偏移纹理

Offsetting a texture

我正在制作一个 space 入侵者克隆体,在生成子弹纹理时,它从船的上边缘出来。 这是我的代码片段:

class Bullet{
public:
    sf::Sprite shape;

    Bullet(sf::Texture *texture, sf::Vector2f pos){
        this->shape.setTexture(*texture);
        this->shape.setScale(3,3);
        this->shape.setPosition(pos);
    }

    ~Bullet() {}

};

并且:

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && shottime >= 20){
            player.bullets.push_back(Bullet(&bt,player.shape.getPosition()));
            shottime=0;
            sound2.play();
        }

现在,我不太确定如何修改此代码以使子弹从船的中间射出。

Image/texture 坐标通常(但不总是)以左上角为原点,然后根据右下角坐标或初始偏移量的高度和宽度来调整大小。

此行将为您提供左上角的偏移量: player.shape.getPosition()

所以,你应该修改它做这样的事情来使子弹与纹理居中:

sf::Vector2f center_pos = player.shape.getPosition();
center_pos.x += player.shape.width() / 2;
player.bullets.push_back(Bullet(&bt, center_pos));

当然,这假设有一个 width() 函数,或类似的东西。