How to best colourise an object when touched

Button type things in most applications look best when they change colour or tone (and make a sound like a click - thats the easy bit I think) when they are touched, particularly while they are touched - to make it look like they really have been depressed as a real button. My question is how best to do this in Codea. If I use a rectangle or ellipse how can apply a colour change only to the touched shape. If I use a sprite am I best make a copy of the sprite in two different colour schemes.

If your using a sprite maybe tint() will save the use of a second sprite

^ Ditto ^

Thanks, that maybe of help.

I really don’t get it. Why aren’t commands such as fill() and tint() applied to specific things? Why do they have to be applied universally?

To be specific try

pushstyle()
tint()
fill()
rect()
popstyle()

Using the values you prefer

@mattbastin it’s a state-driven environment. You can wrap these things up into objects, if you like. For example, here is a rectangle class that maintains its style and transform as a state:

Rectangle = class()

function Rectangle:init( x, y, w, h )
    self.pos = vec2(x,y)
    self.size = vec2(w,h)

    self.fill = color( 255, 255, 255 )
    self.stroke = color( 0, 0, 0, 0 )
    self.strokeWidth = 0
end

function Rectangle:draw()
    pushStyle()

    stroke( self.stroke )
    strokeWidth( self.strokeWidth )
    fill( self.fill )
    rect( self.pos.x, self.pos.y, self.size.x, self.size.y )

    popStyle()
end

You could use it like this:

function setup()
    rect1 = Rectangle( 0, 0, 100, 100 )
    rect1.fill = color( 255, 0, 0 )
    rect1.stroke = color( 255 )
    rect1.strokeWidth = 5

    rect2 = Rectangle( 100, 100, 100, 100 )
    rect2.fill = color( 0, 0, 255 )
end

function draw()
    background(128)

    rect1:draw()
    rect2:draw()
end

Thanks very much. I tried the code and with a bit of tweaking with the colours got it to work as I expected ( I mean from reading your code). I feel like I have a lt to learn.