大佬教程收集整理的这篇文章主要介绍了数组/向量中的SFML 2.0 c精灵,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
#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]); } };
我没有错误,但我的游戏崩溃了.我想,问题在于读取sprite [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]);
然后它再次崩溃!
—更新:现在我再次更改了我的代码,我没有崩溃,但我的纹理是white squares.我的纹理live.png工作,但其他纹理是白色的!这是我的新代码:
Sprite new_sprite; new_sprite.setTexture(textures[i]); sprites.push_BACk(new_spritE);
for(int i=0;i<=sizeof(files_Name);i++){
如果你打印出sizeof(files_Name)的值,你会发现它是16而不是4!不要在这里使用sizeof.另外,不要在这里使用< =,因为即使你用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))填充向量.
以上是大佬教程为你收集整理的数组/向量中的SFML 2.0 c精灵全部内容,希望文章能够帮你解决数组/向量中的SFML 2.0 c精灵所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。