How can I create an animated Sprite from multiple images?

96 views
Skip to first unread message

nederhoed

unread,
Nov 15, 2009, 10:46:40 AM11/15/09
to cocos2d discuss
Hello all, I'm very happy with Cocos2d and am on my way to create a
simple first game with it. Yeah!

But... I have a bird with wings that flap: 4 rotating images. A Sprite
object accepts only 1 image...

How can I create an animated Sprite? My first idea is to subclass
Sprite and swap the visible image every second or so.
Is this the way to go?
I will post my AnimatedSrite code using this option when it's done.

Thanks for you help! Robert

claudio canepa

unread,
Nov 15, 2009, 6:40:40 PM11/15/09
to cocos-...@googlegroups.com

--

 


Yes, preloading the images and then and then assign the Sprite .image member as needed would be fine.

--
claudio

nederhoed

unread,
Nov 16, 2009, 2:31:05 AM11/16/09
to cocos2d discuss
Thanks Claudio, that sounds better than what I did: collecting Sprits
internally.

tonight I will update the code with your suggestion: loading internal
Image instances rather than Sprite instances.

My code from yesterday:

class AnimatedSprite(Sprite):
def __init__(self, images, *args, **kwargs):
super(AnimatedSprite, self).__init__(images[0], *args,
**kwargs)
self._sprites = [Sprite(i) for i in images]
# Loop
self._active_sprite = 0
self.schedule_interval(self.update, 0.2)

def draw(self):
"""Draw the currently active Sprite """
self._sprites[self._active_sprite].position = self.position
self._sprites[self._active_sprite].scale = self.scale
self._sprites[self._active_sprite].draw()

def update(self, dt):
self._active_sprite = (self._active_sprite + 1) % len
(self._sprites)


It's open source:
http://bazaar.launchpad.net/~nederhoed/monkeyjungle/trunk/annotate/head%3A/animate.py


Thanks, keep up the good work! Robert-Reinder

On Nov 16, 12:40 am, claudio canepa <ccanep...@gmail.com> wrote:

nederhoed

unread,
Nov 16, 2009, 5:17:01 PM11/16/09
to cocos2d discuss
Okay, updated the code, FYI:
http://bazaar.launchpad.net/~nederhoed/monkeyjungle/trunk/annotate/head%3A/animate.py

class AnimatedSprite(Sprite):
"""Sprite behaviour, while continuously looping through a series
of images.

TODO: dynamic animation logic, maybe something with state
transitions.
"""
def __init__(self, image_filenames, *args, **kwargs):
"""Constructor """
super(AnimatedSprite, self).__init__(image_filenames[0],
*args, **kwargs)
# Load images from file
self._images = []
for filename in image_filenames:
pic = pyglet.image.load(filename)
pic.anchor_x = pic.width // 2
pic.anchor_y = pic.height // 2
self._images.append(pic)
# Loop helpers
self._active_sprite = 0
self.schedule_interval(self.update, 0.2) # 5 fps

def update(self, dt):
"""Schedule this function to enable the change of images """
self._active_sprite = (self._active_sprite + 1) % len
(self._images)
self.image = self._images[self._active_sprite]

Easy! :-) Cheers, Robert-Reinder

devon

unread,
Nov 17, 2009, 12:50:29 PM11/17/09
to cocos2d discuss
look at http://www.pyglet.org/doc/api/pyglet.image.Animation-class.html
and pyglet sprite http://www.pyglet.org/doc/api/pyglet.sprite.Sprite-class.html
(which is what cocos sprite wraps) you can actually already pass a
list of images to the sprites constructor as an Animation object
instead of a single image. It is useful to subclass sprite though to
store a couple different Animations so you can switch states by
reassigning the self.image to the new animation but the Animation
class pretty much already does what you did in your class.

Devon

On Nov 16, 5:17 pm, nederhoed <r.r.nederh...@gmail.com> wrote:
> Okay, updated the code, FYI:http://bazaar.launchpad.net/~nederhoed/monkeyjungle/trunk/annotate/he...

nederhoed

unread,
Nov 18, 2009, 5:22:06 AM11/18/09
to cocos2d discuss
Hi Devon,

thanks for the guiding. Very valuable! I will change my code with your
hints and post it here next weekend.

@claudio: I'm willing to add examples to the Cocos2d programming
guide. Are you interested? I could write a short tutorial on
Animation.

Greetings from a cloudy The Hague, Robert-Reinder

claudio canepa

unread,
Nov 18, 2009, 10:38:51 AM11/18/09
to cocos-...@googlegroups.com

--

 

hi nederhoed !
Personally I would be interested.
If you are talking about an official addition, you must wait response from the devs ( I'm not )
Anyway, navigating from the cocos-discuss homepage
you can create pages and upload files.

--
claudio 
Reply all
Reply to author
Forward
0 new messages