Jump to content

  • Log In with Google      Sign In   
  • Create Account


Sprite and Animation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 PhilObyte   Members   -  Reputation: 317

Like
0Likes
Like

Posted 12 July 2013 - 01:36 PM

Hello,

 

I have an Animation class that has a Sprite-Array, and of course a Sprite class that defines the actual texture.

I want to have my Entity a fixed Sprite or an Animation, when getting the Sprite and an Animation has been set,

returning the current Sprite.

 

How is this easily possible? Thought of an interface "ISprite" that only has a void GetSprite(); but this looks arwful and

it would be better to have in implicit...can one define an implicit operator in an interface?

 

Thank for your help, Phil.


Edited by PhilObyte, 12 July 2013 - 01:42 PM.


Sponsor:

#2 Waterlimon   Crossbones+   -  Reputation: 2477

Like
1Likes
Like

Posted 12 July 2013 - 03:09 PM

You could make an animator class that updates entity.sprite as needed.

 

Eg.

 

Sprite s;

Entity e(s);

Animator a(s);

 

while (1) do

{

a.updateSprite();

e.draw();

}

 

You would probably store a reference to the sprite in both. You can make a sprite container class and have the enity and animator store an index to the container instead.


o3o


#3 Djfix   Members   -  Reputation: 237

Like
2Likes
Like

Posted 12 July 2013 - 03:24 PM

I don't get what you are asking for exactly, but I am asuming that you need to animate a Sprite.

 

From what I understand from your post, you seem to be the kind of programmer that has found OOP and thinks it is the best and most elegant solution for everything.

 

You don't need all those classes to animate a sprite. You just need an array that holds all the animation frames, and you loop over it once per main loop tick, changing the sprite's image with the one that the array index reaches. At the end of the animation just have an image that shows the sprite being idle.

 

for example, have only a Sprite class like this (pseudocode - also encompasses an animation function that goes over the frames in an array once per loop tick):

class Sprite:
    constructor::set(x,y,image,moving,frames,currentFrame,extraTime) // give it some attributes 
    assign_attributes
    {
      x = 100
      y = 100
      image = image1
      moving = False //this is a boolean that changes state when a movement key is pressed
      frames = [image1,image2,image3,image4] // array of animation frames, taken from a spritesheet
      currentFrame = 0
      extraTime = 0
    }
    function AnimateSprite(int secondsPast)
    {
       if moving == true
       {
          extraTime += secondsPast
          timeBetweenFrames = 1.0/30.0
          while (extraTime > timeBetweenFrames)
          {
             extraTime -= timeBetweenFrames
             currentFrame+=1
             if currentFrame > maxNumberOfFrames:
                currentFrame = 0
           }
           image = frames[currentFrame]
        }
        else
           image = image1
     }

           



Edited by Djfix, 12 July 2013 - 03:26 PM.


#4 warnexus   Prime Members   -  Reputation: 1400

Like
0Likes
Like

Posted 12 July 2013 - 05:35 PM

The idea to implement animation is to have a list of images that acts as an animation strip. You can also think of it as a flip-note.

 

Have a number counter that reaches a threshold. Each threshold is used for each new image to be drawn. Once the threshold has been reached, draw the image. 

 

Once the counter has reached the threshold of the last frame of animation should be, reset the counter to zero.

 

Put the code in the draw method of your object.


Edited by warnexus, 12 July 2013 - 06:08 PM.


#5 PhilObyte   Members   -  Reputation: 317

Like
0Likes
Like

Posted 13 July 2013 - 01:53 AM


From what I understand from your post, you seem to be the kind of programmer that has found OOP and thinks it is the best and most elegant solution for everything.

 

No for the animation itself I of course don't need this, and I already made an animation-class that works fine. Sorry for being unspecificially in my first post: I want my entity have a single sprite when unanimated OR an animation when animated. I could do this by giving it 2 properties, one always null. But this seems to be a rather stupid way because I'ld need to check always. Is there no way by doing it through an interface? I only want my entity to have just one property Sprite, being an Animation that returns the current sprite or a single sprite returning just itself. Anyway, thanks for your long answer. Waterlimon's answer comes near to what I want (I think), but I don't understand it quite good :)

 

Phil



#6 haegarr   Crossbones+   -  Reputation: 4173

Like
1Likes
Like

Posted 13 July 2013 - 02:32 AM

Although not exactly what the OP asks for, but IMHO the better solution would be to separate the existence in the world from appearance determination as follows:

 

There is a list of sprites to render. Here a spite means how to place (means position and orientation) in the world a concrete appearance of an game entity. Currently the list is filled with 2 sprites. There is another list of animations (time dependent, so need to be updated each time step). There is currently 1 animation in the list. In the game loop, call the update method of all listed animations first. This causes the animated sprites to be updated. There is 1 animated sprite in the list of sprites. Hence those is updated, while the other is left as is. Then iterate the sprite list and render each contained sprite.

 

Notice please that the sprites in the said list all show a single appearance. So an animation means to pick an appearance from an animation file and to link it to one of the listed sprites. The update mechanism just sees 1 animation, and the rendering system sees just 2 up-to-date sprites, so each system does as much work as needed without any case distinction.

 

EDIT: Forgotten to mention the key phrase "single responsibility principle" ;)


Edited by haegarr, 13 July 2013 - 02:35 AM.


#7 PhilObyte   Members   -  Reputation: 317

Like
0Likes
Like

Posted 13 July 2013 - 04:38 AM

Thank you! I solved the problem with a static Animator-class. It has a List with animations.

Animations have a property "ISprite Destination;". ISprite forces a class to have a Sprite.

My Entites implement this interface. I think this is a good and not very complicated way.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS