Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Passing a table of frames to a MovieClip? — Gideros Forum

Passing a table of frames to a MovieClip?

bigtunacanbigtunacan Member
edited April 2016 in General questions
I'm trying to use a MovieClip for the first time. I have a textureatlas that I would like to split into frames and put into a movie clip and then play it in a repeating animation.

The following is what I tried, but it does not work.
        local texture = Texture.new("images/spinner.png")
	local bitmaps = {}
 
	for i=1, 19 do
		bitmaps[#bitmaps] = {i, i, Bitmap.new(TextureRegion.new(texture, 0, (i-1)*108, 108, 108))}
	end
 
	local mc = MovieClip.new({ bitmaps })
	mc:setGotoAction(19, 1)
 
	mc:setAnchorPoint(0.5, 0.5);
	mc:setPosition(conf.width/2, conf.height/2)
 
	self:addChild(mc)

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited April 2016
    You almost got it right, except you cant simply pass i, i as first elements for each sprite, you need to pass start frame and end frame, and that would determine how long will this specific sprite be displayed. Right now it is either too quick or not displaying at all.

    You should do something like that:
    local len = 100
    for i=1, 19 do
        bitmaps[#bitmaps] = {(i-1)*len+1, i*len, Bitmap.new(TextureRegion.new(texture, 0, (i-1)*108, 108, 108))}
    end
    that should provide nice intervals like
    1, 100
    101, 200,
    201, 300
    etc

    you can adjust the speed of animation by changing value of len variable

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • @ar2rsawseen,

    Sorry, I should have been more specific. Right now my code is erroring out on this line
    	local mc = MovieClip.new({ bitmaps })
    I'm getting the message

    bad argument #-1 to '?' (number expected, got nil)
  • ar2rsawseenar2rsawseen Maintainer
    Ah yes, there is no need for second pair of curly brackets, this should be correct:
    local mc = MovieClip.new(bitmaps)
  • hgy29hgy29 Maintainer
    I Also have a doubt about:
    bitmap[#bitmap]=...
    , shouldn't it be
    bitmap[#bitmap+1]=...
    ?
  • Ugh; such a stupid oversight on my part. As hgy29 said; I missed the +1 on my table insert.

    So now it is loading and I just get a still image. What I would like to do is have it play and loop "forever" until I hit some condition (I'm using this as a loading indicator).

    This is what I currently have, but it just displays one image doesn't animate at all. The code resides inside a Scene init function.
    	local bitmaps = {}
     
    	local len = 100
    	for i=1, 19 do
    		bitmaps[#bitmaps+1] = {(i-1)*len+1, i*len, Bitmap.new(TextureRegion.new(texture, 0, (i-1)*108, 108, 108))}
    	end
     
    	local mc = MovieClip.new(bitmaps)
    	mc:setGotoAction(19, 1)
    	mc:play()
     
    	mc:setPosition(conf.width/2, conf.height/2)
     
    	self:addChild(mc)
  • ar2rsawseenar2rsawseen Maintainer
    loop it by calling:
    mc:setGotoAction(19*len, 1)
    and start playing from the start by:
    mc:gotoAndPlay(1)
  • @ar2sawseen,

    Thanks that does work. In this case I don't really understand why it works though. Any chance you could explain what is happening?
  • ar2rsawseenar2rsawseen Maintainer
    edited April 2016 Accepted Answer
    I think both of the methods are self explanatory, but let me elaborate.

    So at first you define a goto action, meaning when it reaches specific frame it jumps to other frame.
    In your case when it reaches last frame it jumps to first frame, thus loopin whole animations.

    You can also define stop actions too. by stopping animation when reaching specific frame.

    or you can clear the frame from any action.

    and gotoAndPlay just goes to provided frame and starts playing your animation, which upon reaching last frame jumps back to first one to start again.
    Of course since it is first frame, you could simply call play and I think it would play from first frame too.

    http://docs.giderosmobile.com/reference/gideros/MovieClip#MovieClip

    Likes: bigtunacan

    +1 -1 (+1 / -0 )Share on Facebook
  • @ar2rsawseen,

    Thanks. That makes sense. I was thinking the play function and the gotoAndPlay function should work the same way if I just wanted to loop from the first frame. I think my confusion was I mistakenly thought this was where I had messed up, but the issue was with where I was misusing setGotoAction.

    Thanks again!
Sign In or Register to comment.