SFML - 程序在尝试从向量中绘制精灵时崩溃

时间:2014-05-17 23:36:24

标签: c++ vector sfml

我正在编写这个相对简单的程序,用于在按下空格键时从移动角色发射小型射弹。

程序编译时没有任何错误(在代码块中)并运行直到按下空格键。该程序不是发射射弹,而是迅速崩溃。

我想我已经能够将撞击隔离到我将射弹投射到屏幕的线上(ln.83)。我只是在前后的行上使用了cout语句,而第二个语句没有显示。

for (int ii = 0; ii < inMotion.size(); ii++)
{
    window.draw(inMotion[ii].bulletSprite);
}

我相信我的所有代码都是解决这个问题所必需的,所以我已经发布了所有这些代码。

#include <iostream>
#include <SFML/Graphics.hpp>
#include <string.h>
#include <math.h>
#include <vector>

using namespace std;
class Projectile
{
public:
    Projectile(int, string);
    void moveBullet();
    sf::Sprite bulletSprite;
private:
    sf::Texture bulletTexture;
    int speed;
    string bulletTextureString;
};

Projectile::Projectile(int s, string t)
{
    speed = s;
    bulletTextureString = t;
    if(!bulletTexture.loadFromFile((bulletTextureString).c_str()))
    {
        cout << "error";
    }
    bulletSprite.setTexture(bulletTexture);
}
void Projectile::moveBullet()
{
    bulletSprite.move(speed, 0);
}

class thePlayer
{
public:
    void Fire(sf::Time);
    void Walk();
    void Draw(sf::RenderWindow&);
    void Create(string);
    sf::Time lastShot;
private:
    vector<Projectile> inMotion;
    sf::Texture playerTexture;
    sf::Sprite playerSprite;
    string playerTextureString;
    int moveSpeed = 5;

}; 

void thePlayer::Create(string s)
{
    playerTextureString = s;

    if(!playerTexture.loadFromFile((playerTextureString).c_str()))
    {
        cout << "error";
    }
    playerSprite.setTexture(playerTexture);
}
void thePlayer::Fire(sf::Time time)
{
    sf::Time shotDelay = sf::milliseconds(100);
    if(time - lastShot >= shotDelay)
    {
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
        {
            Projectile bullet(15, "fireball.png");
            bullet.bulletSprite.setPosition(playerSprite.getPosition());
            inMotion.push_back(bullet);
        }
    } 
    for(int ii = 0; ii < inMotion.size(); ii++)
    {
        inMotion[ii].moveBullet();
    }
}
void thePlayer::Draw(sf::RenderWindow& window)
{
    for (int ii = 0; ii < inMotion.size(); ii++)
    {
        window.draw(inMotion[ii].bulletSprite);
    }
    window.draw(playerSprite);
}
void thePlayer::Walk()//awsd standart movement
{
    int x = 0;
    int y = 0;
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
    {
        x-= moveSpeed;
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    {
        x+= moveSpeed;
    }
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
    {
        y-= moveSpeed;
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
    {
        y+= moveSpeed;
    }
    playerSprite.move(x, y);
}

class Game
{
public:
    void Update(sf::RenderWindow&, sf::Time);
    void Start(sf::Time);
    sf::Clock clock;
    thePlayer player;

private:

};

void Game::Start(sf::Time time)
{
    player.Create("block.png");
    player.lastShot = time;
}
void Game::Update(sf::RenderWindow& window, sf::Time time)
{
    player.Fire(time);
    player.Walk();
    player.Draw(window);
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(1000, 1000), "Menu");
    window.setFramerateLimit(60);
    Game theGame;
    sf::Clock clock;
    sf::Time startTime = clock.restart();
    theGame.Start(startTime);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color::White);
        sf::Time elapsed = clock.getElapsedTime();
        theGame.Update(window, elapsed);
        window.display();
    }
    return 0;
}

非常感谢您的帮助。

编辑:我尝试使用调试器,但无法解决任何问题。一篇关于代码块调试的好文章的链接会很棒。

3 个答案:

答案 0 :(得分:1)

我能够自己解决这个问题。我发现我必须通过指针引用Bullet类,并确保纹理也是公共的。我也错误地使用构造函数,并且摆脱了那些。

我的近似解决方案是......

void thePlayer::Fire(sf::Time time)
{
    sf::Time shotDelay = sf::milliseconds(100);
    if(time - lastShot >= shotDelay)
    {
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
        {
            Projectile *bullet = new Bullet;
            bullet->bulletSprite.setPosition(playerSprite.getPosition());
            inMotion.push_back(bullet);
        }
    } 
    for(int ii = 0; ii < inMotion.size(); ii++)
    {
        inMotion[ii]->moveBullet();
    }

还必须更改类

中的矢量声明
//...
vector<Bullet*> Bullets;
//...

再次感谢那些试图回答的人。

答案 1 :(得分:0)

嗯,你把游戏编码错了!

我建议:

  • 您应该创建一个播放器和一个子弹类,

  • 每个类作为渲染方法和更新方法 - 更新方法有一个dt(经过时间到最后一帧)参数,

  • 玩家类有一个&#34;移动&#34;和#34;火&#34;方法,

  • 在游戏循环中,你首先阅读输入(并在需要时调用move或fire方法),更新所有对象(玩家和子弹位置)并全部绘制,

  • 移动方法应该为玩家提供速度,

  • 火法应该用速度创建子弹

  • 更新方法应计算新位置(带速度和dt),

  • 玩家和子弹可以从基础游戏元素类派生(计算位置,碰撞等等)。

答案 2 :(得分:0)

你必须重置时钟:

sf::Time elapsed = clock.restart();

你应该只加载一次射弹的纹理,并在类弹丸的所有实例中使用它。