Yes. The sprite.image attribute controls which image will be shown.

So the issue would be how to access each picture into the .GIF

I modified the cocos' s example test/test_animation.py first to explore the
GIF and then to change the picture at each SPACE key release.

Attached as test_animation_2.py, put in the test/ directory so the gif can
be found.





On Mon, Nov 20, 2017 at 6:52 PM, FreshManGaming <[email protected]>
wrote:

> Hi all,
>
> I'm trying to change an animated GIF in real time to another animated GIF,
> e.g. from an idle state to a running state depending on a key-press.
>
> So far I have managed to load in the idle animated GIF but I am struggling
> to replace the idle GIF to the running GIF,
>
> I have looked into using Pyglet's Animation class however whenever I use 
> from_image_sequence
> the program just crashes.
>
> Are there any ways to change a sprite represented by a GIF in real time
> with another GIF temporarily depending on a key-press?
>
> Thank you.
>
> --
> You received this message because you are subscribed to the Google Groups
> "cocos2d discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/cocos-discuss.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"cocos2d discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/cocos-discuss.
For more options, visit https://groups.google.com/d/optout.
from __future__ import division, print_function, unicode_literals

# This code is so you can run the samples without installing the package
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
#

testinfo = "s, t 0.77, s, q"
tags = "animation"

import cocos
from cocos.director import director
from cocos.sprite import Sprite
import pyglet

class TestLayer(cocos.layer.Layer):
    def __init__(self):
        self.is_event_handler = True
        super( TestLayer, self ).__init__()

        x,y = director.get_window_size()

        poses = pyglet.resource.animation('dinosaur.gif')
        self.poses = poses
##        # this used to invetigate the animation
##        print(poses)
##        #print(dir(poses)) # frames look promising
##        for e in poses.frames:
##            print("type:", type(e), "value:", e)
##            #print("animation frame dir:", dir(e)) # -> image and duration 
looks promising
##            tx_region = e.image
##            duration = e.duration
##            print(tx_region, duration)
##        print("poses.frames indexable? poses.frames[0]=", poses.frames[0]) # 
-> yes

        # ok, let initialize the sprite at one animation frame and later
        # change frame in a key event
        # I will store anim info in the layer for brevity, a real use case 
should
        # store in (the / each) sprite, maybe subclassing sprite
        self.i_frame = 0
        self.sprite = Sprite(self.poses.frames[self.i_frame].image)
        self.sprite.position = x//2, y//2
        self.add( self.sprite  )

    def on_key_release(self, key, modifier):
        if key == pyglet.window.key.SPACE:
            self.i_frame = (self.i_frame + 1) % len(self.poses.frames)
            self.sprite.image = self.poses.frames[self.i_frame].image


def main():
    print("\nPress Space key to do one animation step")
    director.init()
    test_layer = TestLayer ()
    main_scene = cocos.scene.Scene (test_layer)
    director.run (main_scene)

if __name__ == '__main__':
    main()

Reply via email to