Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Universal scaling within a “button-class-with-sprites”

in Questions Posts: 28

Recently, Ive been trying to make sure my projects are all a little more universal freindly. I've been removing all hardcoded values and replacing them with their WIDTH and HEIGHT equivalents. Everything has been fairly straight forward except with a sprite button class that I have frequently used in some of my projects. I found the class here on the forums, but I'm getting some undesired results as I try to make the necessary changes for universal scaling, as everything is set up around the original image dimensions. Would anybody mind showing me the proper way to achieve my goal with this class?

https://codea.io/talk/discussion/1298/button-class-with-sprites

Button = class()

function Button:init(btnNorm,btnSel,x,y)
    self.pos = vec2(x,y)
    self.btnNorm = btnNorm
    self.btnSel = btnSel
    self.selected = false
    self.action = nil
    self.w,self.h = spriteSize(self.btnNorm)
end

function Button:draw()
    if self.selected == true then
        sprite(self.btnSel,self.pos.x,self.pos.y,self.w,self.h)
    elseif self.selected == false then
        sprite(self.btnNorm,self.pos.x,self.pos.y,self.w,self.h)
    end
end

function Button:touched(touch)
    if touch.state == BEGAN or touch.state == MOVING then
        if math.abs(self.pos.x - touch.x) <= (self.w/2)
        and math.abs(self.pos.y - touch.y) <= (self.h/2) then
            self.selected = true
        else
            self.selected = false
        end
    elseif touch.state == ENDED then
        if self.selected == true and self.action then
            self.action()
        end
        self.selected = false
    end
end

Comments

  • dave1707dave1707 Mod
    edited March 3 Posts: 5,987

    It looks like everything is based on the size of the Sprite being passed to the init function. You need to scale the Sprite size for the device you're using.

  • Posts: 213

    @Circuit I didn't really read your code, I just want you to know that spriteSize() glitches in xCode and it doesn't seem to work. That might also be a problem with the class

Sign In or Register to comment.