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”

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?

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)

function Button:draw()
    if self.selected == true then
    elseif self.selected == false then

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
            self.selected = false
    elseif touch.state == ENDED then
        if self.selected == true and self.action then
        self.selected = false


  • dave1707dave1707 Mod
    edited March 2017 Posts: 7,604

    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: 237

    @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.