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

Author Topic: SFML 2.0 - Repeated Animations  (Read 2064 times)

0 Members and 1 Guest are viewing this topic.

charliesnike

  • Newbie
  • *
  • Posts: 10
    • View Profile
SFML 2.0 - Repeated Animations
« on: November 24, 2014, 01:53:48 pm »
Hi. I am trying to do a laser fire animation as seen in the space invaders game. I have some ideas but I really have no idea how to implement them. The idea I think I have is to have the visibility is the sprite set to off, or have it behind the tank so it looks like it isn't there, then for the sprite to follow the tank around (Left and Right). Once the space button is fired the visibility of the laser is shown, and then it shoots up towards the top of the screen. This is my second day for SFML and the only way I have tried to do the laser shoot is to use the .move but this isn't working as it only moves once and not continuously.  This is what I have at the moment:

int spriteWalkSpeed = 10;
int DownspriteWalkSpeed = 5;
int up=-spriteWalkSpeed, down=DownspriteWalkSpeed, left=-spriteWalkSpeed, right=spriteWalkSpeed;

        tankSprite.setPosition(400,550);
        laserSprite.setPosition(400,550);

while (App.isOpen())
        {
                // Process events
                sf::Event Event;
                while (App.pollEvent(Event))
                {
                        if (Event.type == sf::Event::KeyPressed)
                        {
                                if (Event.key.code == sf::Keyboard::Left)
                                {
                                        tankSprite.move(left,0);
                                }
                                if (Event.key.code == sf::Keyboard::Right)
                                {
                                        tankSprite.move(right,0);
                                }
                                if (Event.key.code == sf::Keyboard::Space)
                                {
                                        laserSprite.move(up, up);
                                        laser.play();
                                }

                        }
           }
« Last Edit: November 24, 2014, 02:11:04 pm by Laurent »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML 2.0 - Repeated Animations
« Reply #1 on: November 24, 2014, 02:16:31 pm »
Your description is a bit confusing... What object are you referring to when you say "sprite"? With animation, do you mean a special "fire" effect or just the laser shot moving? Maybe a simple sketch could illustrate the problem...

In general, I would approach the problem as follows: store an STL container for the laser shots currently in flight. These can be sprites for simplicity, later you'll want to separate graphics and game logic. Then, when the player fires, you add a new shot to the container; when it leaves the screen, you remove it.

If you're not familiar with the STL, take a break and learn it carefully, because it's an absolutely crucial tool in C++ that will accompany you every day.

By the way, please use [code=cpp] [/code] tags for code :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML 2.0 - Repeated Animations
« Reply #2 on: November 25, 2014, 12:39:19 am »
I think "animation" in this context means the motion of the sprite.

.move changes the position of the sprite and you have it set to do that when the key is pressed. If you wish for it to continue to move on its own, you'll need to use .move outside of the event loop. You could use a flag (boolean) to set whether or not it should be moving and trigger it with the key press. Remember to reset it once it's left the screen etc..

That said, I agree with Nexus' solution and suggestions. It would definitely benefit you to learn about STL containers. The default one probably should be std::vector.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything