簡體   English   中英

數組/向量中的SFML 2.0 c ++精靈

[英]SFML 2.0 c++ sprite in array/vector

我在我的文件blocks.h有這個:

  #include <vector>
    class Blocks{
        public:
        string files_name[4];
        vector < Sprite > sprites;
        void load(){
            for(int i=0;i<=sizeof(files_name);i++){
                Texture my_texture;
                my_texture.loadFromFile(this->files_name[i]);
                sprites[i].setTexture( my_texture );

            }
        }
        Blocks(){
            this->files_name[0] = "wall.png";
            this->files_name[1] = "floor.png";
            this->files_name[2] = "live.png";
            this->files_name[3] = "coins.png";
            this->load();
        }
        void show(int id, int X, int Y){
            sprites[id].setPosition(X, Y);
            window.draw(sprites[id]);
        }
    };

我沒有錯誤,但我的游戲崩潰了。 我想,問題在於讀取sprites[i].setTexture(...)

我只有消息:進程終止,狀態為-1073741819(0分,2秒)

我的IDE是Code :: Blocks 10.05,我有Windows 8。

當然,在main.cpp文件中,我已經定義了窗口:

RenderWindow window( VideoMode(920, 640, 32 ), "Game" );

#include "blocks.h"
Blocks BLOCKS;

----更新:好的,現在它沒有崩潰,謝謝! 但是,現在我看不到紋理了! 我讀了本傑明林德利的帖子,我添加了一個帶紋理的新矢量。 我的代碼現在看起來像這樣:

const int arraySize = 4; 
string files_name[4]; 
vector < Sprite > sprites; 
vector < Texture > textures; 

並且,在load() ,我有:

for(int i = 0; i < arraySize; i++){ 
Texture new_texture; 
new_texture.loadFromFile(this->files_name[i]); 
textures.push_back(new_texture); 
sprites[i].setTexture(textures[i]); 

然后它再次崩潰!

---更新:現在我再次更改了我的代碼,我沒有崩潰,但我的紋理是白色方塊 我的紋理live.png有效,但其他紋理是白色的! 這是我的新代碼:

    Sprite new_sprite;
    new_sprite.setTexture(textures[i]);
    sprites.push_back(new_sprite);

SFML sprite通過指針存儲其關聯的紋理。 您創建的紋理是循環的局部,因此在每次迭代結束時都會被銷毀,從而使精靈中的指針無效。 你需要在任何精靈使用它們的過程中保持你的紋理活着。

此外,您的sprites矢量沒有元素,您需要指定大小,或在循環中使用push_back

此外,正如Scott指出的那樣, sizeof(files_name)不是循環的適當終止值,因為它為您提供了sizeof(string) *數組中的元素數量。 您只需要數組中的元素數量。

問題是這一行:

for(int i=0;i<=sizeof(files_name);i++){

如果你打印出sizeof(files_name)的值,你會發現它是16而不是4! 不要在這里使用sizeof。 另外,不要使用<= here,因為即使你用sizeof(files_name)替換了4,你也會嘗試訪問files_name [4],這也會給你一個錯誤。

相反,你可以使用:

const int arraySize = 4;
string files_name[arraySize];
...
for(int i = 0; i < arraySize; i++)

或類似的東西。

另外,希望你在某些時候初始化sprite。 在調用load()之前,需要使用Sprites(類似sprites.push_back(mySprite))填充向量。

好的,它只能在兩個循環中工作:感謝您的幫助:)

vector < Sprite > sprites;
vector < Texture > tekstures;

和:

for(int i = 0; i < arraySize; i++){
    Texture new_texture;
    new_texture.loadFromFile(this->files_name[i]);
    tekstures.push_back(new_texture);
}

for(int i = 0; i < arraySize; i++){
    Sprite new_sprite;
    new_sprite.setTexture(tekstures[i]);
    sprites.push_back(new_sprite);
}

並在一個循環中它不起作用,我有白色紋理:

for(int i = 0; i < arraySize; i++){
    Texture new_texture;
    new_texture.loadFromFile(this->files_name[i]);
    tekstures.push_back(new_texture);
    Sprite new_sprite;
    new_sprite.setTexture(tekstures[i]);
    sprites.push_back(new_sprite);
}

問候 !

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM