SFML - 程序在尝试从向量中绘制精灵时崩溃 [英] SFML - program crashes when trying to draw sprite from vector

查看:124
本文介绍了SFML - 程序在尝试从向量中绘制精灵时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这是我之前的问题从堆栈溢出的重新发布,我遇到了问题。

http://stackoverflow.com/questions/ 23717167 / sfml-program-crashes-when-trying-to-draw-sprite-from-vector [ ^ ]



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



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



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



NOTE: This is a repost of my previous question from stack overflow that I am having issues getting an answer for.
http://stackoverflow.com/questions/23717167/sfml-program-crashes-when-trying-to-draw-sprite-from-vector[^]

I am writing this relatively simple program to fire a small projectile from a moving character when the space bar is pressed.

The program compiles without any errors(in code blocks) and runs until the space bar is pressed. Instead of launching the projectile, the program promptly crashes.

I think I have been able to isolate the crash to the line where I draw the projectiles to the screen(ln. 83). I just used cout statements on the lines before and after and the second statement didn't show.

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



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


I believe that all of my code will be necessary to address this issue so I have posted all of it.

#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;
}





非常感谢您的帮助。



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



Thanks so much for the help.

I tried using the debugger but couldn't figure anything out. A link to a good article on codeblocks debugging would be great.

推荐答案

这篇关于SFML - 程序在尝试从向量中绘制精灵时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆