尝试使用带有SFML的Vectors

时间:2015-02-22 04:28:58

标签: c++ vector sfml

每次我运行代码时都会说

Vector subscript OUT of range.

我已经在" stackoverflow"上查看了其他SFML向量问题。他们都没有回答我的问题或工作过。我想做的是:

vector<sf::Sprite> snake_part;

sf::Sprite sprite;

for (int i = 0; i <= 10; --i) {

    snake_part.push_back(sprite);
    cout << "Vector size : " << snake_part.size() << endl;
    snake_part[i].setTexture(Snake_Part_Texture);

    if (i > 0) {
        x1[i] -= 10;
    }
}

这是代码中不起作用的部分。

输出:

Vector size : 1
Vector size : 2

它应该计入&#34; 10&#34;但是停在&#34; 2&#34;出于某种原因,并附带&#34;矢量下标超出范围&#34;错误。

完整代码:

#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>

using namespace std;

int main() {
//=========================================================

    // Coordinates
    float y = 330; float x = 150;
    float x1[10]; x1[0] = x;
    float y1[10]; y1[0] = y;

    // Texture
    sf::Texture floorText;
    if (!floorText.loadFromFile("Floor_Snake_SFML.png")) {
        cout << "Floor_Snake_SFML.COULD_NOT_LOAD: [Error]" << endl;
    }
    sf::Texture Snake_Part_Texture;
    if (!Snake_Part_Texture.loadFromFile("Snake_Part_Sprite.png")) {
        cout << "Snake_Part_Texture.COULD_NOT_LOAD: [Error]" << endl;
    }

    // Sprite_floor
    sf::Sprite floor;
    floor.setTexture(floorText);
    floor.setPosition(0, 320);

    // vector <sprite> -- (snake)
    vector<sf::Sprite> snake_part;

    sf::Sprite sprite;

    for (int i = 0; i <= 10; --i) {

        snake_part.push_back(sprite);
        cout << "Vector size : " << snake_part.size() << endl;
        snake_part[i].setTexture(Snake_Part_Texture);

        if (i > 0) {
            x1[i] -= 10;
        }
    }


    //=========================================================
    sf::RenderWindow window(sf::VideoMode(640, 480), "<snake>");

    while (window.isOpen()) {

        sf::Event event;
        while (window.pollEvent(event)) {

            switch (event.type) {

            // Close Window
            case sf::Event::Closed:
                window.close();
                break;

            // Do [x] if KeyPressed-
            case sf::Event::KeyPressed:

                switch (event.key.code) {

                // [A] -pressed-
                case sf::Keyboard::A:

                    for (int j = 1; j <= 10; j++) {
                        x1[j] = x1[j - 1]; y1[j] = y1[j - 1];
                    }

                    x1[0] -= 10;

                    for (int k = 1; k <= 10; k++) {
                        snake_part[k].setPosition(x1[k], y1[k]);
                    }

                    break;

                // [D] -pressed-
                case sf::Keyboard::D:



                    break;

                // [Space] -pressed-
                case sf::Keyboard::Space:

                    break;
                }
                break;
            }
        }

        window.clear();   // ===============================

        window.draw(floor);
        window.draw(snake_part[0]);

        window.display(); // ===============================
    }

    return 0;
}

如果你想要一个更详细的错误和本程序的概要,只需评论,还要提前感谢,如果有人知道更好的方式而不是Vector创建10个Sprites而不必这样做10次:

sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setTextureRect(sf::IntRect(0, 0, 10, 10));
float x = 320, y = 240;
sprite.setPosition(x, y);
那时我真的很感激。

- 提前谢谢。

1 个答案:

答案 0 :(得分:3)

错误很可能来自这一行:

 for (int i = 0; i <= 10; --i)

我不知道你对这一行的意图,但很可能它应该是++i而不是--i