Need help with buttons!...

Hello everyone, i’m a newbie to Codea and I have question regarding changing image when you press buttons…

Everything here is working except that when I press button (BEGAN state), it must change sprite, but it change it only when I push and start moving my finger. If I change sprite on ENDED state, it happens so fast that I can barely see it.

How can I make it to work normally, so I can see it changing? I’m tried every options, even tried with “wait” function…no effect.

Thanks everyone in advance!

PButton = class()

function PButton:init(name, x, y, flip)
    -- you can accept and set parameters here
    self.name = name
    self.x = x
    self.y = y
    self.flip = flip
    self.image = {"Project:btn_img", "Project:btn_img_2"}
    self.i = 1
    
    
    -- Polygonal button
    lu = vec2(-48, 16)
    ld = vec2(-48, -16)
    ru = vec2(48, 16)
    rd = vec2(48, -16)
    
    self.bBody = physics.body(POLYGON, lu, ld, rd, ru)    
    self.bBody.localCenter = vec2(x,y)
    self.bBody.x = x
    self.bBody.y = y
    self.bBody.sensor = true
    self.bBody.type = STATIC
    
    self.selBody = physics.body(CIRCLE, 6)
    self.selBody.type = STATIC
    self.selBody.sensor = true
    
    self.push = false
end


function PButton:draw()
    -- Codea does not automatically call this method
    
    sprite(self.image[self.i], self.x, self.y)
    
end


function PButton:touched(touch)
    -- Codea does not automatically call this method
    self.selBody.x = touch.x
    self.selBody.y = touch.y
    
    if touch.state == BEGAN or touch.state == MOVING and self.selBody:testOverlap(self.bBody) then
        self.push = false
        self.i = 2
        else
        self.i = 1
    end
    
    if touch.state == ENDED and self.selBody:testOverlap(self.bBody) then
        print("Collided!!!")
        self.push = true
        self.i = 1
    end
end 

@Compozitor Here’s a simple example of changing the image when a Button is pressed.

function setup()
    rectMode(CENTER)
end

function draw()
    background(40, 40, 50)
    fill(255)
    rect(WIDTH/2,HEIGHT/2,100,100)
    if flip then
        sprite("Planet Cute:Character Boy",WIDTH/2,HEIGHT/2)
    else
        sprite("Planet Cute:Character Cat Girl",WIDTH/2,HEIGHT/2)
    end
end

function touched(t)
    if t.state==BEGAN then
        if t.x>WIDTH/2-50 and t.x<WIDTH/2+50 and
                t.y>HEIGHT/2-50 and t.y<HEIGHT/2+50 then        
            flip=true
        end
    end
    if t.state==ENDED then
        flip=false
    end
end

Thank you very much! The problrm was, as I think in changing sprite in one tick… Thank you!