[anim8][resolved] Can't update multiple animations

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Clyybber
Prole
Posts: 15
Joined: Thu Aug 13, 2015 11:46 am

[anim8][resolved] Can't update multiple animations

Post by Clyybber »

EDIT: It did what it should. It was just my fault in terms of thinking.


So im making a small gui library for myself which as of now only has buttons. But i cant get multiple animations to update correctly. If theres one animation/button then it works but if theres 2 they just hang at the starting frame. Im including my code.
gui.lua ( the library )

Code: Select all

local anim8 = require "anim8"
local gui = {}
local pi = math.pi
local button = {}
local buttonmt = { __index = button }
--Helper functions
local function drawImageorAnimation(image,x,y,r,sx,sy,ox,oy,kx,ky,animation)
   if type(animation) == "table" then animation:draw(image,x,y,r,sx,sy,ox,oy,kx,ky)
   else love.graphics.draw(image,x,y,r,sx,sy,ox,oy,kx,ky) end
end


----------------------------------------------------------------------------------------------------------------------------------

function newButtonSystem(center,edge,corner,centerON,edgeON,cornerON) --If theres an animation include in a table with the according image
   local t = {}
    for i=1, 6 do
      t[i] = 0
    end
    t[1] = center
    t[2] = edge
    t[3] = corner
    t[4] = centerON
    t[5] = edgeON
    t[6] = cornerON
    local temp = {}
    for k=1,6 do
      if type(t[k]) == "table" then
         for i, v in pairs(t[k]) do
            if type(v) == "table" then t[k+6] = v
            else temp[k] = v end
         end
      end
   end
   for i,v in pairs(temp) do
      t[i] = v
   end
   return t
end

function newButton(text,x,y,w,h,ox,oy,funct,system)
   local yc = math.ceil((h-18)/9)
   local xc = math.ceil((w-18)/9)
   for i=7, 12 do
      if system[i] then
         system[i] = system[i]:clone()
      end
   end
   return setmetatable({text=text,x=x,y=y,w=w,h=h,ox=ox,oy=oy,yc=yc,xc=xc,hover=false,click=funct,system=system},buttonmt)
end

function button:update(dt)
   for i = 7, 12 do
      if self["system"][i] then
         self["system"][i]:update(dt)
         if self.hover then
            self["system"][i]:resume()
         else
            self["system"][i]:pauseAtStart()
         end
      end
   end
   if checkCoordinBox( love.mouse.getX() , love.mouse.getY() , self.x-self.ox , self.y-self.oy , self.w , self.h ) then self.hover = true else self.hover = false end
end
function button:mousepressed(x,y)
    if checkCoordinBox( x , y , self.x-self.ox , self.y-self.oy , self.w , self.h ) then self.click() end
end

function button:draw()
   love.graphics.push()
   love.graphics.translate(-self.ox,-self.oy)
   if self.hover then
      self:drawActive()
   else
      self:drawDeactive()
   end
   self:drawText()
   love.graphics.pop()
end

function button:drawText()
   love.graphics.printf(self.text,self.x+self.w/2,self.y+self.h/2,self.w/2,"left",0,1,1,font:getWidth(self.text)/2,font:getHeight()/2)
end

function button:drawActive()
   for i=1,self.yc,1 do
      for j=1,self.yc,1 do
         drawImageorAnimation(self['system'][4],self.x+9*j,self.y+9*i,0,sx,sy,ox,oy,kx,ky,self['system'][10])
         drawImageorAnimation(self['system'][5],self.x+9*j+9,self.y,0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][11])
         drawImageorAnimation(self['system'][5],self.x+9*j,self.y+self.h,-0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][11])
      end
      drawImageorAnimation(self['system'][5],self.x,self.y+9*i,0,sx,sy,ox,oy,kx,ky,self['system'][11])
      drawImageorAnimation(self['system'][5],self.x+self.w,self.y+9*i+9,pi,sx,sy,ox,oy,kx,ky,self['system'][11])
      drawImageorAnimation(self['system'][6],self.x,self.y,0,sx,sy,ox,oy,kx,ky,self['system'][12])
      drawImageorAnimation(self['system'][6],self.x+self.w,self.y,0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][12])
      drawImageorAnimation(self['system'][6],self.x,self.y+self.h,-0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][12])
      drawImageorAnimation(self['system'][6],self.x+self.w,self.y+self.h,pi,sx,sy,ox,oy,kx,ky,self['system'][12])
   end
end

function button:drawDeactive()
   for i=1,self.yc,1 do
      for j=1,self.xc,1 do
         drawImageorAnimation(self['system'][1],self.x+9*j,self.y+9*i,0,sx,sy,ox,oy,kx,ky,self['system'][7])
         drawImageorAnimation(self['system'][2],self.x+9*j+9,self.y,0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][8])
         drawImageorAnimation(self['system'][2],self.x+9*j,self.y+self.h,-0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][8])
      end
      drawImageorAnimation(self['system'][2],self.x,self.y+9*i,0,sx,sy,ox,oy,kx,ky,self['system'][8])
      drawImageorAnimation(self['system'][2],self.x+self.w,self.y+9*i+9,pi,sx,sy,ox,oy,kx,ky,self['system'][8])
      drawImageorAnimation(self['system'][3],self.x,self.y,0,sx,sy,ox,oy,kx,ky,self['system'][9])
      drawImageorAnimation(self['system'][3],self.x+self.w,self.y,0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][9])
      drawImageorAnimation(self['system'][3],self.x,self.y+self.h,-0.5*pi,sx,sy,ox,oy,kx,ky,self['system'][9])
      drawImageorAnimation(self['system'][3],self.x+self.w,self.y+self.h,pi,sx,sy,ox,oy,kx,ky,self['system'][9])
   end
end

return gui
Basically you create a System which includes the style for a button. And after that you make Buttons.
ATM My library assumes that all the images are 9x9(expect the animation spritesheets of course)
Last edited by Clyybber on Wed Apr 27, 2016 7:51 pm, edited 1 time in total.
My glass is half empty and half full that means its FULL :ultrahappy:
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [anim8] Can't update multiple animations

Post by kikito »

I recommend you that in order to maximize the posibilities of getting help in the future you include a .love file displaying the issue that you are having instead of just your library file. That way, if someone wants to give it a try at helping you, they don't have to go through your code, create some buttons, create animations for them, etc. Every extra step that you add for people to help you will lower the amount of people willing to help you. Help others help you!

That said, I can tell you a couple things just by looking at your code. First, there are several "magical numbers": For example self['system'][3] or for i = 7, 12. It is often possible to replace these numbers with a more intention-revealing variable name or string - like using 'center' instead of 1, 'edge' instead of 2, etc. With the numbers alone the code becomes ilegible pretty quickly - for example for someone like me trying to help you. It is also very easy to copy-paste and put the incorrect number somewhere without noticing.

Second, I don't understand what a "system" is in relation to each individual "button", but it seems to me that those "systems" add a lot of complexity to an otherwise simple setup. If I had to investigate a bug in this library, I would definitively start by the use of these "systems". I would also consider removing the "system" abstraction completely and just using buttons.
When I write def I mean function.
User avatar
Clyybber
Prole
Posts: 15
Joined: Thu Aug 13, 2015 11:46 am

Re: [anim8] Can't update multiple animations

Post by Clyybber »

Ok,
Heres my love.
I still havent changed the part with the numbers, because it would require more work.
Attachments
LOVE2D_TESTENVIRONMENT.love
Sry for the name xD
(11.61 KiB) Downloaded 195 times
Last edited by Clyybber on Sun Apr 24, 2016 10:19 am, edited 1 time in total.
My glass is half empty and half full that means its FULL :ultrahappy:
User avatar
Clyybber
Prole
Posts: 15
Joined: Thu Aug 13, 2015 11:46 am

Re: [anim8] Can't update multiple animations

Post by Clyybber »

Aaand what the system part should do is:
You basically create a plan of the button and then you create multple buttons out of it. So you dont have to provide thousands of arguments again and again to all your buttons.
My glass is half empty and half full that means its FULL :ultrahappy:
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [anim8] Can't update multiple animations

Post by kikito »

Clyybber wrote:Heres my love.
I see two buttons, the one at the top does nothing (except on hover) and the one at the bottom exits. What is the expected behavior?
Clyybber wrote:Aaand what the system part should do is:
You basically create a plan of the button and then you create multple buttons out of it. So you dont have to provide thousands of arguments again and again to all your buttons.
If your systems are only involved in the creation of the buttons, then they are overstating their welcome. In particular: once a button is created, it should not retain any reference to the system which created it. Once the button is created, all the button properties should be "in the button", not "some in the button, and some in the system"). The system should "copy the necessary properties to the button" while it is creating it. It would look like this:

Code: Select all

local system = newButtonSystem(x,y,z) -- parameters for creating all buttons the same way

local button = newButton(system, a,b,c) -- parameters for this individual button
What's important is that the system newButton method fills up all the button attributes, leaving no trace of itself in the button. This way, the button update and the button draw functions will only have to "work with the buttons themselves", and won't have to "deal with two different things at the same time (buttons and systems).

If you do this, you will end up with something similar to the Factory Method Pattern. You might want to consider renaming your "systems" to "factories", as it is a more intention-revealing name.
When I write def I mean function.
User avatar
Clyybber
Prole
Posts: 15
Joined: Thu Aug 13, 2015 11:46 am

Re: [anim8] Can't update multiple animations

Post by Clyybber »

Ahh yeah that makes sense. Thx for your help. And the factorial method. Changed to resolved.
My glass is half empty and half full that means its FULL :ultrahappy:
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests