问题
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;
}
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.
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};
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
回答1:
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.
Since all of your sf::Texture
s 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");
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));
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.
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.
回答2:
Since sf::Sprite
keeps a const pointer to the texture you are using, you can reuse the same texture without issue. You are making a lot of textures for no reason when a single one will do. Unfortunately the same doesn't apply to moving the sprites since those are separate objects.
来源:https://stackoverflow.com/questions/15259771/sfml-2-0-having-the-same-sprite-looped