【问题标题】:SFML Sprite isn't showing on the window but it isn't giving me any errorsSFML Sprite 没有显示在窗口上,但它没有给我任何错误
【发布时间】:2016-09-26 08:16:53
【问题描述】:

我正在使用 SFML 创建一个用于练习的小游戏,它是我年轻时在某种游戏机上玩的旧坦克游戏的复制品。

我在尝试绘制玩家时遇到了一件奇怪的事情。我说很奇怪,因为起初它正在显示,所以我认为我的函数没有问题,但是在我为敌方坦克创建了一个派生类并为此创建了一个新文件之后,我在链接时遇到了一些嵌套问题所有文件加在一起(5 个文件,1 个 cpp 和 4 个标头)。在我弄清楚之后,我遇到了这个问题,找不到任何解决方案。在那个问题之前它可以工作,现在它不再起作用了。

我检查了纹理,如果它正在加载,我设置的位置,它们没问题。

这是玩家坦克的类

class tank{

protected:
sf::FloatRect boundingBox;
sf::Sprite tankSprite;

bullet *mBullet;
public :
tank(); // constructor

bullet * get_ptr(){return mBullet;}
sf::FloatRect get_boundingBox(){return boundingBox;}

void setRotation(float);
void show(){game.draw(tankSprite);}
void update();
void shoot(){mBullet = new bullet(tankSprite);}
};

这是我更新世界并在我的窗口上绘制的主要代码

if(!stop)
    Me.update();

if(Me.get_ptr()->isFired()==true)
    Me.get_ptr()->update();

// Output on the window
game.clear();
Me.show();
if(Me.get_ptr()->isFired()==true)
    Me.get_ptr()->show();
game.display();

旁注:游戏是渲染窗口并且是全局声明的(我知道这是不好的做法,我正在慢慢改掉这个坏习惯)

main.cpp

#include <SFML/Graphics.hpp>
#include "Tank.h"

int main()
{
tank Me;

init();
while (game.isOpen())
{
    sf::Event event;
    while (game.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            game.close();
        else if (event.type == sf::Event::KeyPressed)
        {
            stop=false;
            switch(event.key.code)
            {
                case sf::Keyboard::W:
                {
                    Me.setRotation(0);
                    break;
                }
                case sf::Keyboard::D:
                {
                    Me.setRotation(90);
                    break;
                }
                case sf::Keyboard::S:
                {
                    Me.setRotation(180);
                    break;
                }
                case sf::Keyboard::A:
                {
                    Me.setRotation(270);
                    break;
                }
                case sf::Keyboard::Escape:
                {
                    game.close();
                    break;
                }
                case sf::Keyboard::Space:
                {
                    if(Me.get_ptr()->isFired()==false)Me.shoot();
                    break;
                }
                case sf::Keyboard::LControl:
                {
                    stop=true;
                    break;
                }
                default:
                    break;
            }
        }
    }

    if(!stop)
        Me.update();

    if(Me.get_ptr()->isFired()==true)
        Me.get_ptr()->update();

    // Output on the window
    game.clear();
    Me.show();
    if(Me.get_ptr()->isFired()==true)
        Me.get_ptr()->show();
    game.display();
}
return 0;
}

init.h

#include<iostream>

bool stop;

sf::RenderWindow game(sf::VideoMode(400, 400), "SFML");

sf::Texture myTankTexture;
sf::Texture bulletTexture;

void init(){

srand(time(NULL));

stop = true;

game.setVerticalSyncEnabled(true);
game.setFramerateLimit(60);

if(!myTankTexture.loadFromFile("tank.jpg"))
{
    std::cout<<"eroare la textura tank"<<std::endl;
}
myTankTexture.setSmooth(true);

if(!bulletTexture.loadFromFile("bullet.jpg"))
{
    std::cout<<"bullet texture error"<<std::endl;
}
bulletTexture.setSmooth(true);
}

sf::Vector2f rotationToDirection(int rotation){

sf::Vector2f dir;
switch (rotation)
{
    case 0:
    {
        dir.x=0.0;
        dir.y=-1.0;
        break;
    }
    case 90:
    {
        dir.x=1.0;
        dir.y=0.0;
        break;
    }
    case 180:
    {
        dir.x=0.0;
        dir.y=1.0;
        break;
    }
    case 270:
    {
        dir.x=-1.0;
        dir.y=0.0;
        break;
    }
}
return dir;
}

bullet.h

#include "init.h"

class bullet{

protected:
sf::Sprite bulletSprite;
sf::FloatRect boundingBox;
bool isBFired = false;
public:
bullet(sf::Sprite); // constructor
~bullet(){isBFired=false;}

sf::FloatRect get_boundingBox(){return boundingBox;}
bool isFired(){if(isBFired)return true;else return false;}
int collision();

void del(){delete this;}
void update();
void show(){game.draw(bulletSprite);}
};

bullet::bullet(sf::Sprite sprite){

isBFired = true;

bulletSprite.setTexture(bulletTexture);
bulletSprite.setOrigin(2.5,5.0);
bulletSprite.setRotation(sprite.getRotation());
     bulletSprite.setPosition(sprite.getPosition().x+rotationToDirection(bulletSprite.getRotation()).x*5.0,sprite.getPosition().y+rotationToDirection(bulletSprite.getRotation()).y*5.0);

boundingBox = bulletSprite.getLocalBounds();
}

int bullet::collision(){

if(bulletSprite.getPosition().x < 0
   || bulletSprite.getPosition().x > 400
   || bulletSprite.getPosition().y > 400
   || bulletSprite.getPosition().y < 0 )return 1;
else
    return 0;
}

void bullet::update(){

           bulletSprite.move(rotationToDirection(bulletSprite.getRotation()).x*6.0,rotationToDirection(bulletSprite.getRotation()).y*6.0);

if(collision()==1)
    delete this;
else
    boundingBox = bulletSprite.getLocalBounds();
}

坦克.h

#include "bullet.h"

class tank{

protected:
sf::FloatRect boundingBox;
sf::Sprite tankSprite;

bullet *mBullet;
public :
tank(); // constructor

bullet * get_ptr(){return mBullet;}
sf::FloatRect get_boundingBox(){return boundingBox;}

void setRotation(float);
void show(){game.draw(tankSprite);}
void update();
void shoot(){mBullet = new bullet(tankSprite);}
};

tank::tank(){

tankSprite.setTexture(myTankTexture);
tankSprite.setOrigin(20,20);
tankSprite.setRotation(0);
tankSprite.setPosition(200,200);

boundingBox = tankSprite.getLocalBounds();
}

void tank::update(){

    tankSprite.move(rotationToDirection(tankSprite.getRotation()).x*3,rotationToDirection(tankSprite.getRotation()).y*3);

boundingBox = tankSprite.getLocalBounds();
}

void tank::setRotation(float rotation){

tankSprite.setRotation(rotation);
}

【问题讨论】:

  • 请提供一个可验证的最小示例。此外,您的代码充满了“代码气味”(例如,使用 new 创建项目符号并将其分配给成员指针字段,通过非 const ptr 返回项目符号等......)跨度>
  • @VittorioRomeo 当我运行我的应用程序时,我知道我可以四处走动,因为我可以射击并且我看到子弹按预期运行,但如果我按下触发按钮,我就看不到自己shoot() 函数从我所在的位置开始,我看到了子弹。我不知道我是否正确理解了您所说的最小可验证示例的意思,如果这不是您所要求的,请见谅。
  • @L Alex:我可以在我的系统上轻松复制粘贴和编译的东西。

标签: c++ sfml


【解决方案1】:

据我所知,您没有设置纹理矩形,看看这是否对您有帮助。当你设置你的坦克精灵时尝试:

tankSprite.setTexture(myTankTexture, true);

传递 true 会将纹理矩形重置为精灵的大小,请参阅SFML docs 了解更多信息。

另外,您在调用 init() 之前创建 Tank 类,myTankTexture 将为空,因此 tank 构造函数使用它,并且稍后加载您的纹理。

正如他们用您的语言所说的那样,“multa bafta”:)

【讨论】:

  • 现在试过了,可惜没用,不过还是谢谢。
  • 我现在看到了问题,抱歉。交换这些行: tank Me;在里面();您正在创建没有加载纹理的坦克,因此您的 setTexture 设置了一个空白纹理。 :)
  • 哦,对了,它成功了,非常感谢,愚蠢的我没有注意到,不知何故我交换了它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-16
  • 2017-03-18
  • 2020-08-04
  • 1970-01-01
  • 1970-01-01
  • 2018-02-25
  • 2018-10-16
相关资源
最近更新 更多