Button not recognizing different number of presses

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
cashmere
Prole
Posts: 5
Joined: Thu Dec 29, 2022 4:45 am

Button not recognizing different number of presses

Post by cashmere »

The following is the function I am using to create the button:

Code: Select all

function gui.addIconButton2TouchSupport(px,py,onpress,onpress2,col,icon,sw,sh) --pos, size, function to excec when button is clicked, color, icon as love image, optional w/h
    local ico=icon or''
    local btncol=col or{1,1,1}
    local btn={
        p={x=px,y=py},
        s={w=icon:getWidth()or sw,h=icon:getHeight()or sh},
        pressed=onpress,
        pressed2=onpress2,
        onmouse=function(self,x,y,btn,istouch,touches)
            if(x>self.p.x and x<self.p.x+self.s.w
            and y>self.p.y and y<self.p.y+self.s.h)then
                if(touches==1)then
                    self:pressed()
                elseif(touches==2)then
                    self:pressed2()
                end
            end
        end,
        update=function(self)
            return
        end,
        draw=function(self)
            love.graphics.setColor(btncol)
            love.graphics.draw(icon,self.p.x,self.p.y)
            love.graphics.setColor(1,1,1,1)
        end
    }
    
    return btn --returns button object, add to guiobj/world table
end
And the implementation in the code:

Code: Select all

gui.addIconButton2TouchSupport(love.graphics.getWidth()-cross:getWidth()*4,love.graphics.getHeight()-cross:getHeight(), -- transform/translate
            function(self,x,y,btn,istouch,touches)
                if(not imgTransformTranslate.iterate and currentImg.img~=nil)then
                    imgTransformTranslate.iterate=true
                else
                    imgTransformTranslate.iterate=false
                end
            end,
            function(self,x,y,btn,istouch,touches)
                currentImg.rads=0
                currentImg.scaleX,currentImg.scaleY=1,1
                currentImg.x,currentImg.y=0,0
            end,
            {224/255,203/255,224/255,1},love.graphics.newImage('graphics/ui/larger.png'))
I've used this same style of implementation before, for buttons without the touches parameter and it worked as I wanted it. Is there anything I'm missing with the touches thing? It's possible I've just been missing something obvious but I hope that's not it. Thanks!
RNavega
Party member
Posts: 355
Joined: Sun Aug 16, 2020 1:28 pm

Re: Button not recognizing different number of presses

Post by RNavega »

Provided that that onmouse() function from the button is actually being called (it can be tested with adding a simple print("onmouse()") debug line inside that function), you're not passing any parameters to the pressed() or pressed2() functions when calling them. That is, except the first parameter 'self' holding a reference to the calling table, since you're using the colon syntax with self:pressed() or self:pressed2().

This means that Lua will assign nil to all other parameters, including 'touches'. It will fail that IF test inside onmouse() as it only does something if 'touches' is 1 or 2, but not nil.
cashmere
Prole
Posts: 5
Joined: Thu Dec 29, 2022 4:45 am

Re: Button not recognizing different number of presses

Post by cashmere »

Thanks so much! It was just that I wasn't passing arguments from mousepressed lol. Def something I should've checked but whatever. Appreciate your help!
Post Reply

Who is online

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