SFML 2.0有相同的sprite循环 [英] SFML 2.0 Having the same sprite looped

查看:143
本文介绍了SFML 2.0有相同的sprite循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SFML 2.0绘制一个入侵者sprite列表到我的游戏。目前,我必须保持复制和粘贴我的代码以绘制更多的精灵:

I am drawing an invader sprite list into my game using SFML 2.0. At the moment I have to keep copy and pasting my code to draw more sprites in:

//load the invaders images
sf::Texture invaders;
sf::Texture invaders2;
sf::Texture invaders3;
sf::Texture invaders4;
sf::Texture invaders5;
sf::Texture invaders6;
invaders.loadFromFile("images/invaders.png");
invaders2.loadFromFile("images/invaders.png");
invaders3.loadFromFile("images/invaders.png");
invaders4.loadFromFile("images/invaders.png");
invaders5.loadFromFile("images/invaders.png");
invaders6.loadFromFile("images/invaders.png");

//Sprites
sf::Sprite invadersSprite(invaders);
sf::Sprite invadersSprite2(invaders2);
sf::Sprite invadersSprite3(invaders3);
sf::Sprite invadersSprite4(invaders4);
sf::Sprite invadersSprite5(invaders5);
sf::Sprite invadersSprite6(invaders6);

invadersSprite2.setPosition(30,NULL);
invadersSprite3.setPosition(60,NULL);
invadersSprite4.setPosition(90,NULL);
invadersSprite5.setPosition(120,NULL);
invadersSprite6.setPosition(150,NULL);

if(Clock.getElapsedTime().asSeconds()>REFRESH_RATE)
        {

            //carry out updating tasks
            static float spriteTimer=0.0;  //keep track of sprite time
            spriteTimer+=Clock.getElapsedTime().asSeconds();

            static int count=0; //keep track of where the sub rect is
            if(spriteTimer>delay)
            {
                invadersSprite.setTextureRect(area);
                invadersSprite2.setTextureRect(area);
                invadersSprite3.setTextureRect(area);
                invadersSprite4.setTextureRect(area);
                invadersSprite5.setTextureRect(area);
                invadersSprite6.setTextureRect(area);
                ++count;
                invadersSprite.move(xVelocity, yVelocity);  
                invadersSprite2.move(xVelocity, yVelocity); 
                invadersSprite3.move(xVelocity, yVelocity); 
                invadersSprite4.move(xVelocity, yVelocity); 
                invadersSprite5.move(xVelocity, yVelocity); 
                invadersSprite6.move(xVelocity, yVelocity); 


if (invadersSprite.getPosition().x >= 770 || invadersSprite2.getPosition().x >= 770 || invadersSprite3.getPosition().x >= 770 || invadersSprite4.getPosition().x >= 770 || invadersSprite5.getPosition().x >= 770 || invadersSprite6.getPosition().x >= 770)// When it hits the right hand side of the screen it will move back down to the left
            {
                xVelocity = left;
                invadersSprite.move(0,down);
                invadersSprite2.move(0,down);
                invadersSprite3.move(0,down);
                invadersSprite4.move(0,down);
                invadersSprite5.move(0,down);
                invadersSprite6.move(0,down);
            } 
            else if (invadersSprite.getPosition().x <= 0 || invadersSprite2.getPosition().x <= 0 || invadersSprite3.getPosition().x <= 0 || invadersSprite4.getPosition().x <= 0 || invadersSprite5.getPosition().x <= 0 || invadersSprite6.getPosition().x <= 0) // When it hits the left hand side of the screen it will move up to the right.
            {
                invadersSprite.move(0,down);
                invadersSprite2.move(0,down);
                invadersSprite3.move(0,down);
                invadersSprite4.move(0,down);
                invadersSprite5.move(0,down);
                invadersSprite6.move(0,down);
                xVelocity = right;
            }

显然我还没有添加所有代码到这篇文章,坦白地说不要认为它是必需的,因为我只需要告诉你们,我复制了很多代码,只是为了绘制一些更多的精灵。我知道有一个更简单的方法来做到这一点。我知道有,说如果你正在创造另一个游戏,恰好使用精灵,但1000次,那么我知道一些可怜的程序员不会在那里做我在做什么。

Obviously I haven't added all the code to this post, and frankly I don't think it is needed as I just need to show you guys that I am replicating a lot of code just to draw some more sprites. I know that there HAS to be an easier way to do this. I know there is, say if you were creating another game that happened to use a sprite, but 1000 times then I know some poor programmer won't be there doing what I am doing at the moment.

我一直在想要制作一个包含10个数字的数组:

I've been wondering about making an array that holds 10 numbers:

int invadersArray[10] = {1,2,3,4,5,6,7,8,9,10};

然后执行一个for循环,将sprite的渲染循环10次,意味着sprite被加载在10次。我在这里的右边线?如果我可以得到一些帮助,如何做这个?

and then doing a for loop which loops the rendering of the sprites 10 times, meaning the sprite gets loaded in 10 times. Am I on the right lines here? If I am could I possibly get some help as HOW to do this?

或者可能保存精灵在内存中,找到,然后循环的内存位置与我的数组?感谢

Or perhaps saving the sprite in memory, locating that and then looping that memory location with my array? Thanks

推荐答案

是的,你过于复杂,不使用数组或容器。任何时候你最终再现一个变量只是一个数字在变量名的末尾增加,是时候使用一个容器。

Yes, you're overcomplicating it and not using arrays or containers where you should be. Any time you end up reproducing a variable just with a number at the end of the variable name increasing, it's time to use a container.

由于你的所有 sf :: Texture s完全相同,你只需要做一次:

Since all of your sf::Textures are exactly the same, you only need to do it once:

// Create a texture
sf::Texture invaderTexture;
// Load image file into that texture
invaderTexture.loadFromFile("images/invaders.png");

然后,如果你想创建,让我们说10个入侵者精灵,你会有一个容器 sf :: Sprite 。这里我给你一个 std :: vector ,但其他容器(或一个简单的旧数组)也将工作:

Then, if you want to create, let's say, 10 invader sprites, you would have a container of sf::Sprite. Here I show you with a std::vector, but other containers (or a plain old array) will work too:

// Create a vector of 10 sprites initialised with the texture above
std::vector<sf::Sprite> invaderSprites(10, sf::Sprite(invaderTexture));

正如你所看到的,它们都使用相同的 invaderTexture code>。这是更好的,因为你不需要一个纹理的拷贝在内存中为每个入侵者。

As you can see, they are all initialised with the same invaderTexture. This is much better since you don't need to have a copy of the texture lying around in memory for each invader.

然后你可以循环 invaderSprites 设置它们的属性:

Then you can loop over invaderSprites to set their properties:

// Loop over the elements of the vector of sprites
for (int i = 0; i < invaderSprites.size(); i++) {
  invaderSprites[i].setPosition(...);
}

这应该可以帮助你开始。

This should help you get started.

对数组执行相同的操作类似于初始化:

To do the same with arrays would look like this for initialisation:

// Create an array of 10 sprites (cannot initialise them with textures here)
sf::Sprite invaderSprites[10];
// Loop over each sprite, setting their textures
for (int i = 0; i < 10; i++) {
  invaderSprites[i].setTexture(invaderTexture);
}

然后,当你需要做某事时,你可以做同样的循环所有的入侵者。

Then you can do the same kind of loop whenever you need to do something to all of the invaders.

这篇关于SFML 2.0有相同的sprite循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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