Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Animations - Gideros Forum


totebototebo Member
edited March 2018 in Code snippets
I'm working on a Lua extension to TexturePack which will make frame-by-frame animations easier.

I was wondering if anyone else has already done something like this, and can share ideas?

My approach is to store all TextureRegions, create one Bitmap and then use Bitmap:setTextureRegion() when needed. Then I manually step each animation sprite.
My Gideros games: www.totebo.com


  • MovieClip is great for timeline animations, but not ideal for frame-by-frame bitmap animations, because it uses a new sprite for each frame. Just changing the Bitmap TextureRegion feels cleaner, and my guess is that it's more efficient (although I haven't tested it).
    My Gideros games: www.totebo.com
  • antixantix Member
    Frame swapping will be faster IMHO. My book (should it ever be completed) will contain a frame based animation class.

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • By frame swapping you mean the setTextureRegion, or something else?
    My Gideros games: www.totebo.com
  • Apollo14Apollo14 Member
    edited March 2018
    guys, is it okay if I post tiny offtopic, considering we're discussing animations :)

    In my recent project I'm using this code to change color of a ball. Is it a right practice to work with one MovieClip instead of setting one sprite invisible, the other one visible, or removing one from stage, then adding the other, etc.?
    frame1 = Bitmap.new(myTexturePack1:getTextureRegion("ballBlue.png"))
    frame2 = Bitmap.new(myTexturePack1:getTextureRegion("ballGreen.png"))
    frame3 = Bitmap.new(myTexturePack1:getTextureRegion("ballRed.png"))
    myClip = MovieClip.new{
    stage:addEventListener(Event.MOUSE_DOWN, function()
    myClip:gotoAndStop(myClip:getFrame()%3+1) --switch to next color
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • @totebo I've used that method since day one, had no problems with it and it works for me. :)
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
  • antixantix Member
    I mean using setTextureRegion()
  • totebototebo Member
    edited March 2018
    Yeah, I didn't realise I could do that and had somewhat of an epiphany when I used it the first time. Feels so much cleaner.

    @SinisterSoft do you have a class for this, or how do you step frame by frame animations?
    My Gideros games: www.totebo.com
  • I don't create classes that much - I just put the command in when I want the animation to occur - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.

    Likes: totebo, Apollo14

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    +1 -1 (+2 / -0 ) Share on Facebook
  • for frame rate they are also death by a thousand cuts.
    Best quote ever.

    Likes: SinisterSoft

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 ) Share on Facebook
  • @totebo Would you mind to share some code snippets for Animation frame-by-frame Class?
    Coming soon
  • This is what it looks like currently. Hopefully it's useful in some way! You need to manually step it with the update() function.
    local remove = table.insert
    AnimatedBitmap = Core.class( Sprite )
    function AnimatedBitmap:init( texture_regions, frames_per_texture )
    	-- Store parameters
    	self.texture_regions = texture_regions
    	self.frames_per_texture = frames_per_texture
    	-- Create bitmap
    	local texture_region = texture_regions[1]
    	self.bitmap = Bitmap.new( texture_region )
    	self:addChild( self.bitmap )
    	-- Start off on frame 1 and texture 1
    	self.current_texture_frame = 1
    	self.current_frame = 1
    function AnimatedBitmap:setTextureRegions( texture_regions )
    	--print("AnimatedBitmap:setTextureRegions( texture_regions )", texture_regions, #texture_regions )
    	self.texture_regions = texture_regions
    	-- Reset frame count if new textures has less frames
    	if self.current_texture_frame > #texture_regions then
    		self.current_texture_frame = 1
    		self.current_frame = 1
    	-- Set next frame immediately
    	self.bitmap:setTextureRegion( texture_regions[self.current_texture_frame] )
    function AnimatedBitmap:update()
    	-- Set new texture
    	if self.current_frame == self.frames_per_texture then
    		local texture_regions = self.texture_regions
    		local current_texture_frame = self.current_texture_frame
    		-- Loop current texture frame if displayed the last one on the previous update
    		if current_texture_frame > #texture_regions then
    			current_texture_frame = 1
    			self.current_texture_frame = current_texture_frame
    		-- Set texture
    		local texture_region = texture_regions[current_texture_frame]
    		self.bitmap:setTextureRegion( texture_region )
    		-- Prepare to show the next texture frame
    		self.current_texture_frame += 1
    		-- Reset current frame to start countdown to next frame
    		self.current_frame = 1
    	self.current_frame += 1
    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 ) Share on Facebook
  • Thank @totebo! It helps me a lot!

    Likes: totebo

    Coming soon
    +1 -1 (+1 / -0 ) Share on Facebook
Sign In or Register to comment.