C++ SFML精灵不是';窗口上没有显示,但它是';别给我任何错误
我正在使用SFML创建一个用于练习的小游戏,它是我年轻时在某种类型的控制台上玩的旧坦克游戏的复制品 当我试图吸引球员时,我遇到了一个障碍。我说奇怪是因为一开始它是显示出来的,所以我不认为我的函数有什么问题,但是在我为敌人的坦克创建了一个派生类并创建了一个新文件之后,我在将所有文件链接到一起时遇到了一些嵌套问题(5个文件,1个cpp和4个标题)。在我弄明白这一点后,我遇到了这个问题,找不到任何解决办法。在那个问题出现之前它是有效的,现在它不再有效了 我检查了纹理,如果它正在加载,我将其设置到的位置,它们都正常 这是玩家坦克的课程C++ SFML精灵不是';窗口上没有显示,但它是';别给我任何错误,c++,sfml,C++,Sfml,我正在使用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;
}
在我看来,你没有设置纹理矩形,看看这是否对你有帮助。设置坦克精灵时,请尝试: tankSprite.setTexture(myTankTexture,true) 传递true会将纹理矩形重置为精灵的大小,有关详细信息,请参阅 另外,在调用init()之前创建Tank类,myTankTexture将为空,以便Tank构造函数使用它,稍后加载纹理
正如他们在您的语言中所说,“multa bafta”:)请提供一个最小的可验证示例。此外,你的代码充满了“代码味道”(例如,使用new创建一个项目符号并将其分配给成员指针字段,通过非常量ptr返回项目符号,等等。)@VittorioRomeo当我运行我的应用程序时,我知道我可以四处走动,因为我可以射击,我看到项目符号按预期进行,但我就是看不到自己,如果我按下触发shot()函数的按钮,它从我所在的位置开始,我看到了子弹。我不知道我是否正确理解了你所说的最小可验证示例的意思,如果这不是你所要求的,很抱歉。@L Alex:我可以在我的系统上轻松复制粘贴和编译的东西。@LAlex现在尝试了,不幸的是它不起作用,但无论如何谢谢。我现在看到问题了,抱歉。交换这些线路:坦克我;init();您正在创建未加载纹理的水箱,因此setTexture将设置空白纹理。:)哦,对了,它成功了,非常感谢,愚蠢的我没有注意到,不知怎的,我把它们换了。
#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;
}
#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();
}
#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);
}