Howdy, Stranger!

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

In this Discussion

Can’t get a grip on touch states

in Questions Posts: 57

I can’t seem to catch the ENDED event not matter what I have tried, I even tried an else after getting BEGAN. Here is the current form.

Here is my touch event:

-- touch events
function touched(touch)
    if rightBtn:touched(touch) then
        if touch.state == BEGAN then
            print("right")
            Global.direction = Global.MOVERIGHT
        end 
        if touch.state == ENDED then
            print("stop")
            if Global.direction == Global.MOVERIGHT then
                Global.direction = Global.NOMOVE
            end
        end
    elseif leftBtn:touched(touch) then
        if touch.state == BEGAN then
            print("left")
            Global.direction = Global.MOVELEFT
        end
        if touch.state == ENDED then
            print("stop")
            if Global.direction == Global.MOVELEFT then
                Global.direction = Global.NOMOVE
            end
        end
    elseif fireBtn:touched(touch) then
        if touch.state == BEGAN then
            Global.fire = true
            if gameState == Global.GSTATE_PAUSED then
                gameState = Global.GSTATE_PLAYING
            end
        elseif touch.state == ENDED then
            print("fire false")
            Global.fire = false
        end
    else
        if gameState ~= Global.GSTATE_PLAYING then
            initNewGame()
        end
    end
end
My draw():

`     function draw()
            -- background color 
            background(0, 0, 0)

            -- buttons
            rightBtn:draw()
            leftBtn:draw()
            fireBtn:draw()
        end`

 And finally my Button class:

        `Button = class()

        function Button:init(x, y, image,...)
            -- you can accept and set parameters here
            self.x = x
            self.y = y
            self.w = 32
            self.h = 32
            self.image = image
            self.imageWidth = nil
            self.imageHeight = nil
            local arg = {...}
            self.txt = arg[1];
            self.fnt = arg[2];
            self.fntSize = arg[3];
        end

        function Button:draw()
            if self.imageWidth then
                sprite(self.image, self.x, self.y, self.imageWidth, self.imageHeight)
            else
                sprite(self.image, self.x, self.y)
            end
            if self.txt ~= nil then
                pushStyle()
                fill(90, 71, 71, 255)
                font(self.fnt)
                fontSize(self.fntSize)
                text(self.txt, self.x, self.y)
                popStyle()
            end
        end

        function Button:touched(touch)
            if touch.state == BEGAN then
                if touch.x > self.x - self.w and touch.x < self.x + self.w and
                touch.y > self.y - self.h and touch.y < self.y + self.h then
                    return true
                end
            end
            return false
        end`

Comments

  • dave1707dave1707 Mod
    Posts: 9,109

    In your Button:touched function, you’re only looking for BEGAN. If it’s ENDED then you return false. In the touched function, if the state is ENDED, the statement “ if rightBtn:touched(touch)” is false and you never go into it so you never test for ENDED.

  • Posts: 57

    I am a dumb ass, can you show me some code? Thanks...

  • dave1707dave1707 Mod
    Posts: 9,109

    Try this in Button:touched

        if touch.state == BEGAN or touch.state==ENDED   then
    
  • Posts: 57

    That worked, thanks much! Too much time on this grog head...

Sign In or Register to comment.