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