Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Continuous sprites  (Read 3768 times)

0 Members and 3 Guests are viewing this topic.

mark1515

  • Newbie
  • *
  • Posts: 4
    • View Profile
Continuous sprites
« on: November 07, 2012, 12:26:53 am »
Probably it's a dumb problem but I can't solve it. I have an array of four sprites, each one is an arrow, and want them to go across the screen one after the other randomly. This would be an example but with two arrows: -> -> <- -> -> -> <- -> -> -> <- -> .
while(Window.isOpen())
{
   sf::Event event;

   int index = rand() % 4; //

   while(Window.pollEvent(event))
   {
         if(event.type == sf::Event::Closed)
            Window.close();
   }

   Window.clear();

   while(spritesArray[arrow].getPosition().x < windowWidth)
   {
        Window.draw(spritesArray[arrow]);
        spritesArray[arrow].move(speed, 0);
   }

   Window.display();
}
 

I think the answer might be obvious, but I can't see it.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Continuous sprites
« Reply #1 on: November 07, 2012, 01:54:39 am »
Read the tutorials again and learn how things work with SFML.
You have to move the sprite every frame iteration and not all at once in a while loop...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Continuous sprites
« Reply #2 on: November 07, 2012, 06:03:14 am »
But that doesn't explain everything in my opinion, read them though or you won't

Almost every graphical engine works with 2 screens. One that is actually displayed on your window and the other one in the background, not visible. You always work on the background, so if you call draw(), you won't draw it in your window, but in the background. Same applies for clear(). The function display() switches background to foreground, so everything you've just drawn will be visible.
With that in mind you should be able to work it out.

mark1515

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Continuous sprites
« Reply #3 on: November 07, 2012, 01:19:07 pm »
I know how to move the sprite, but just one. I have an array of four and I need to move them randomly, but I can't find a way of doing it.

    int index = rand() % 4;
    Window.draw(spritesArray[index]);
    spritesArray[index].move(speed, 0);
 

I use this to move just one, but this generates a different sprite every time it loops, so it doesn't work either.

 I know it sounds silly, but I can't figure it out.

MrMuffins

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Continuous sprites
« Reply #4 on: November 07, 2012, 09:46:50 pm »
"I use this to move just one, but this generates a different sprite every time"

Is it because your only drawing one thing at a time?
>Window.draw(spritesArray[index]);

Or am I not understanding..

int index = rand() % 4;
for (int i = 0; i < 4; i++) {
    Window.draw(spritesArray[i]); //draw all 4 sprites
}
spritesArray[index].move(speed, 0); //move only a random one
 

If that's in the game loop then every run will only move a random sprite a few pixels anyways. But all four will still draw. ?

mark1515

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Continuous sprites
« Reply #5 on: November 08, 2012, 12:50:39 pm »
I start over. I'm trying to make a simple game, where four arrows (up, right, down, left), move from one side to the screen to the other side, one after the other. It would be like guitar hero, but you have to press the key corresponding to the arrow while these pass through a zone.

I have an array of sprites with the four arrows in it. Now my problem is that I can't move the arrows one after the other. I tried drawing a new one in every loop, but it didn't work. I just need to have a constant wave of arrows moving through the screen, but I'm stucked right there.

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Continuous sprites
« Reply #6 on: November 08, 2012, 01:57:24 pm »
I start over. I'm trying to make a simple game, where four arrows (up, right, down, left), move from one side to the screen to the other side, one after the other. It would be like guitar hero, but you have to press the key corresponding to the arrow while these pass through a zone.

I have an array of sprites with the four arrows in it. Now my problem is that I can't move the arrows one after the other. I tried drawing a new one in every loop, but it didn't work. I just need to have a constant wave of arrows moving through the screen, but I'm stucked right there.
That a lot more clear :) Always first try it with numbers before random numbers so you know what's going on.


Firstly you'll want to create your array of sprites. For example :
sf::Sprite[4] array_sprites = {arrow_left, arrow_up, arrow_right, arrow_down}

For the main loop you'll want to move the sprites by using sf::Sprite.Move(x,y) inside a for-loop so you can iterate through array_sprites:
for (int i = 0, i <= 3, ++i) {
    array_sprites[i].Move(x,y);
}
Then clear your screen, draw everything and display it. End of loop.

But seriously, read the tutorials of 1.6 for how to draw sprites, the system is the same, but you'll have to replace sf::Image with sf::Texture.

mark1515

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Continuous sprites
« Reply #7 on: November 08, 2012, 06:53:37 pm »
Thank you for your quick response  ;) I have the array with the sprites, I know how to move them, but I can't do it separately, one after the other (like a marquee). These are the steps I'm making:

- To generate a random number representing one of the four arrows
- Draw it
- Take the position to separate the next arrow
- Draw the next arrow

Of course this way doesn't work, because when I draw the second arrow I lost the index of the first one and then it disappears from the screen. I think there must be a way to do it automatically but I can see it.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Continuous sprites
« Reply #8 on: November 08, 2012, 11:39:36 pm »
First, establish an array with your arrow images, 0-3.

Second, you will have another array, which will hold your sprites.

Third, you would have something like:

Code: [Select]

for(int i = 0; i < numberOfArrows; i++)
{
     short int arrow = rand % 4;

     arrowSprite[i].SetImage(arrowImages[arrow]);

     //insert code here for setting up their starting positions you can use i to set them a set distance apart
     //x = i * arrowSprite.GetWidth() + distanceBetweenArrows is an example

}

This will assign one of the four arrow images at random to every sprite of the array.

Next, you will have the movement.

Code: [Select]

for(int i = 0; i < numberOfArrows; i++)
{
     arrowSprite[i].Move( xMove, yMove);
}


Then you will clear the screen with screen.Clear();

Then you will draw the sprites

Code: [Select]

for(int i = 0; i < numberOfArrows; i++)
{

}