0
\$\begingroup\$

everybody. I need to add animations to my game. And I have a question.

I found AnimatedSprite class on SFML Github which is an inheritance, but in my project, I am using a lot of elements made with strategy pattern, for example movement behavior, jumping behavior, fighting behavior, etc. I just try to avoid inheritance because it makes class inflexible.

But this time in contrast to other classes Animation class has a lot of methods that can be used at runtime. I mean Play(), Stop(), Pause(), and more. If I use this as a strategy I would have to give access to this object.

What do you think? Maybe I just need to use another design pattern?

\$\endgroup\$
0
\$\begingroup\$

You should include the AnimatedSprite class with composition (so not inheritance). By letting the class itself handle the animations, you won't need to give exterior access to the class. For example, following the update pattern, each time the entity updates, it could check if the current state dictates that the entity should be animated, and call the Play(), Pause(), or Stop() methods accordingly. On top of this, you could provide a method in your entity to change the animation, which would let the entity swap the current animation it is drawn with (or just let the entity handle that itself in its update method). In my own game, I have implemented this by having a vector of animations, and a variable to hold the index of the one I want to draw every tick.

Hope this helps! Sorry I can't include code right now as I'm typing from my phone

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.