Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
timed sprite animation — Gideros Forum

timed sprite animation

deezaydeezay Member
edited September 2011 in General questions
I'm fairly new to this stuff, I have just looked at the animation and timers sample. I was wondering how i could combine these two together to create multiple animations that move independently based on different timers or a certain time after the previous sprite has moved? Any help will be much appreciated.

Comments

  • You can add a timer to the Bird class (inside the Bird:init method):
       self.timer = Timer.new(20, 0)
     
       self.timer:addEventListener(Event.TIMER, self.onTimer, self)
       self.timer:start()
    Then, instead of changing the current animation frame inside Bird:onEnterFrame, do it inside the timer event:
    function Bird:onTimer()
       self:removeChild(self.frames[math.floor(self.frame)])
       self.frame = self.frame + 0.1
       if self.frame >= self.nframes + 1 then
          self.frame = 1
       end
       self:addChild(self.frames[math.floor(self.frame)])
    end
    Hope this helps.
  • ok thanks i did notice a change but just to check is this wt u meant -
    Bird = gideros.class(Sprite)
     
     
    function Bird:init(frameList)
     
       self.frames = {}
     
       for i = 1, #frameList do
          self.frames[i] = Bitmap.new(TextureRegion.new(Texture.new(frameList[i])))
       end
     
       self.nframes = #frameList
     
       -- to find the bounding box we add all childs and then remove them
       for i = 1, #self.frames do
          self:addChild(self.frames[i])
       end
     
       local width = self:getWidth()
       local height = self:getHeight()
     
       for i = 1, #self.frames do
          self:removeChild(self.frames[i])
       end
     
     
       for i = 1, #self.frames do
          self.frames[i]:setX(-width/2)
          self.frames[i]:setY(-height/2)
       end
     
     
       self.frame = 1
       self:addChild(self.frames[1])
     
       self:setX(540)
       self:setY(math.random(160) + 40)
     
       self.speedy = math.random(-1000,1000)/1000
     
       self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
     
       self.timer = Timer.new(20, 0)
       function Bird:onTimer()
       self:removeChild(self.frames[math.floor(self.frame)])
       self.frame = self.frame + 0.1
       if self.frame >= self.nframes + 1 then
          self.frame = 1
       end
       self:addChild(self.frames[math.floor(self.frame)])
    end
    self.timer:addEventListener(Event.TIMER, self.onTimer, self)
       self.timer:start()   
    end
     
     
    function Bird:onEnterFrame()
       self:removeChild(self.frames[math.floor(self.frame)])
       self.frame = self.frame + 0.1
       if self.frame >= self.nframes + 1 then
          self.frame = 1
       end
       self:addChild(self.frames[math.floor(self.frame)])
     
       local x = self:getX()
       local y = self:getY()
     
       if (y > 300) then self.speedy = 2 * self.speedy end
       if (y < 10) then self.speedy = -2 * self.speedy end
     
     
       x = x - 3
       y = y + self.speedy
     
       if (x < -10) then 
          x = 540
          y = (math.random(160) + 40)
       end
       print(x, y)
     
       self:setX(x)
       self:setY(y)
     
    end
  • This was what I meant
    Bird = gideros.class(Sprite)
     
     
    function Bird:init(frameList)
     
       self.frames = {}
     
       for i = 1, #frameList do
          self.frames[i] = Bitmap.new(TextureRegion.new(Texture.new(frameList[i])))
       end
     
       self.nframes = #frameList
     
       -- to find the bounding box we add all childs and then remove them
       for i = 1, #self.frames do
          self:addChild(self.frames[i])
       end
     
       local width = self:getWidth()
       local height = self:getHeight()
     
       for i = 1, #self.frames do
          self:removeChild(self.frames[i])
       end
     
     
       for i = 1, #self.frames do
          self.frames[i]:setX(-width/2)
          self.frames[i]:setY(-height/2)
       end
     
     
       self.frame = 1
       self:addChild(self.frames[1])
     
       self:setX(540)
       self:setY(math.random(160) + 40)
     
       self.speedy = math.random(-1000,1000)/1000
     
       self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
     
       self.timer = Timer.new(20, 0)
     
       self.timer:addEventListener(Event.TIMER, self.onTimer, self)
       self.timer:start()   
    end
     
    function Bird:onTimer()
       self:removeChild(self.frames[math.floor(self.frame)])
       self.frame = self.frame + 0.1
       if self.frame >= self.nframes + 1 then
          self.frame = 1
       end
       self:addChild(self.frames[math.floor(self.frame)])
    end
     
    function Bird:onEnterFrame()
       local x = self:getX()
       local y = self:getY()
     
       if (y > 300) then self.speedy = 2 * self.speedy end
       if (y < 10) then self.speedy = -2 * self.speedy end
     
     
       x = x - 3
       y = y + self.speedy
     
       if (x < -10) then
          x = 540
          y = (math.random(160) + 40)
       end
       print(x, y)
     
       self:setX(x)
       self:setY(y)
     
    end
    +1 -1 (+3 / -0 )Share on Facebook
  • oh right haha...thanks for your help mate appreciate it
Sign In or Register to comment.