Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Allowing Sprites to animate themselves. — Gideros Forum

Allowing Sprites to animate themselves.

edited January 2014 in Suggestions & requests
I'm not entirely sure if this is a suggestion, or should rather be a bug report, but I find myself frequently wanting to use MovieClip in a slightly unconventional way.

Instead of creating my Sprite instance as a MovieClip from the beginning, thus requiring the timeline to be established when calling .new(), I would like to be able to create a separate MovieClip from within the Sprite that would then control the Sprite when played.

This seems to work right now in certain cases. I'll create a new MovieClip, give it an object of "self", play the animation, and all is well. In other cases, however, the clip will stop part way through unless I add the MovieClip to the stage hierarchy, thus creating another instance of "self", which I don't want to do.

Perhaps there's a simple work-around that I'm missing?

Comments

  • edited January 2014
    I should add that it seems to work fine (without adding it to the scene hierarchy) so long as there are no other MovieClips running at the same time.
  • Could you post some sample of the code you are having trouble with?
  • Yes I would also want to see an example, to understand what are you up to :)
  • edited January 2014
    Sure thing. Say, for example, I want a particular Sprite to fade in when it is added to the Stage:
    Widget = Core.class(Sprite)
     
    function Widget:init()
        self:addEventListener(Event.ADDED_TO_STAGE, self.onAddedToStage, self)
    end
     
    function Widget:onAddedToStage()
        local clip = MovieClip.new( { { 1, 60, self, { alpha = { 0, 1, "linear" } } } } ))
        clip:play()
    end
    Notice the "self" parameter in my MovieClip, so it can control the alpha parameter of the Sprite that the MovieClip was created in. This allows me to create new instances of Widget as a Sprite ( Widget.new() ) instead of having to make Widget itself a MovieClip and then provide a timeline every time I want a new instance. It would also allow me to animate Widget in any way at any time from within Widget itself.

    However, if any other MovieClip animations are currently playing, the line...
    clip:play()
    ... will only play part of the animation. It gets interrupted and stops part way through. The only way to guarantee that the animation completes is to add it to the Stage hierarchy. However, I can't do this...
    self:addChild(clip)
    ... because that would be adding "self" to "self", which naturally results in a crash. If I just add it to stage, I end up with a duplicate Widget on my screen.

    Does that make sense?
  • talistalis Guru
    edited January 2014
    What about MovieClip:gotoAndPlay(frame) function, in this case you can guarantee it will always start from the beginning of clip.
    Or you can check in onAddedToStage function Event.COMPLETE if dispatched then play if not MovieClip:gotoAndPlay(frame) .

    (
    When a MovieClip object finishes it playing (by reaching its final frame or a frame with stop action),
    it dispatches an Event.COMPLETE event.
    )

    But what will happen in multiple instances have no idea, haven't tried it just came to me:D
  • When there are multiple MovieClips playing, Event.COMPLETE is never triggered in the example I gave above. I have to add it to the stage hierarchy to make that happen, which poses a problem for me in this case.
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @FlimFlamboyant oh I see
    for this purpose I usually use GTween:
    https://github.com/gideros/GTween

    More info:
    http://appcodingeasy.com/Gideros-Mobile/Gideros-GTween-with-easing

    It can actually tween any property that can be passed to set method, so you can write your custom set methods for your classes and tween anything you would want:
    http://www.giderosmobile.com/forum/discussion/980/tween-anything/p1
  • Oh, and I don't have to add it to the stage hierarchy? Sweet; I think that's just the sort of thing I'm looking for. Thanks!
Sign In or Register to comment.