Forum Discussion

BradC's avatar
BradC
Binge Watcher
12 years ago

how to change animation on animated sprite?

Is there a way to change the animation in an animated sprite?

I can change it to a region containing a bitmap, but can't figure out how to change the animation and have it play properly.

It's fairly easy to just remove the sprite and create a new one with a new animation, but that's a hack, and seems a lot less efficient than just changing the animation on an existing sprite.


thanks.

10 Replies

  • What do you mean by sprite. Are you talking about a sprite created with
    The compositor? Or are you talking about a sprite meaning a bitmap
    The compositor can take an array of regions and animate them
    This is how I do it most of the time for things like cursors, most
    Recently a flying dove in my own application. You can also experiment
    With the changeallregions if your adventurous. As
    For a sprite meaning just a bitmap I'm not quite
    Sure what you are trying to accomplish
  • If I am guessing correctly. You can create a bitmap the complete size of all the frames in your animation
    So if your frame size was say 100 * 200 and you have 5 frames of animation you create an empty bitmap
    Of width 100 x 5, 200. Load your resource for each frame and draw object into the empty bitmap offsetting
    The x pos by 100 each time. This would create a contiguous strip for you to animate. Next you create a region
    The size of a single frame 0,0, 100, 200. Then all you have to do is offset the region in the x pos by 100 to animate
    If you set wrap to true it will wrap around. Now if you already have a contiguous bitmap. Then you can skip step one
    And just build a region the size of one frame and offset it to move through the frames
  • It's actually much more straightforward than that. You just need to create an array of regions that make up the frames of the animation, then create a sprite with roCompositor.NewAnimatedSprite(). You'd then call roCompositor.AnimationTick() to automatically advance any animated sprites in the compositor. The duration you pass to AnimationTick() uses the value you set on the individual frame regions via roRegion.SetTime() to determine which frame to advance to. If you don't set a time on the region, then AnimationTick() will always advance to the next frame.
  • BradC's avatar
    BradC
    Binge Watcher
    apologies for not being clear.

    if I've defined 2 animation sets in the sprite map, how do I switch between them in a given animated sprite? Or is this not currently possible?
  • "BradC" wrote:
    apologies for not being clear.

    if I've defined 2 animation sets in the sprite map, how do I switch between them in a given animated sprite? Or is this not currently possible?

    By "2 animation sets" do you mean like one set of a bird flying to the left and another of a bird flying to the right? If that's the case, then I think you'd need two separate sprites that you hide or show depending on which animation you want to display.
  • "TheEndless" wrote:
    It's actually much more straightforward than that .


    Yes I alluded to that in my first post but I was not sure if he was using a sprite
    Meaning a bitmap. You recall that some people use the term sprite meaning
    Bitmap. So I came back with the second response. But you know that I am
    A big fan of the compositor and use animation tick quite often
  • BradC's avatar
    BradC
    Binge Watcher
    "TheEndless" wrote:
    By "2 animation sets" do you mean like one set of a bird flying to the left and another of a bird flying to the right? If that's the case, then I think you'd need two separate sprites that you hide or show depending on which animation you want to display.


    Yep, that's exactly my question... is there a way to switch animations on an animated sprite, or does the sprite have to be destroyed and recreated (or hidden if you have extra CPU and memory available to play with)
  • "BradC" wrote:
    "TheEndless" wrote:
    By "2 animation sets" do you mean like one set of a bird flying to the left and another of a bird flying to the right? If that's the case, then I think you'd need two separate sprites that you hide or show depending on which animation you want to display.


    Yep, that's exactly my question... is there a way to switch animations on an animated sprite, or does the sprite have to be destroyed and recreated (or hidden if you have extra CPU and memory available to play with)

    The sprite has a SetRegion method, that would allow you to reset the region on a non-animated sprite. It may accept an array to do the same with an animated sprite, but I've never tried that.
  • "BradC" wrote:
    Yep, that's exactly my question... is there a way to switch animations on an animated sprite, or does the sprite have to be destroyed and recreated (or hidden if you have extra CPU and memory available to play with)

    There is this mysterious ChangeMatchingRegions() method, about which i asked the other day - it might be related. Or not :roll:
  • BradC's avatar
    BradC
    Binge Watcher
    "TheEndless" wrote:
    The sprite has a SetRegion method, that would allow you to reset the region on a non-animated sprite. It may accept an array to do the same with an animated sprite, but I've never tried that.



    It doesn't seem to unless I did it wrong.