Button click detection?

Are there any built-in methods for this? Right now what I currently do to detect a sprite-click is I take the sprite’s position, and do if spriteposition:dist(vec2(CurrentTouch.x,CurrentTouch.y) < Whatever number fits the appropriate range. But is there a better way to detect sprite taps?

Nope, there are no clickable objects on a Codea screen, just pixels

@coder1500 we all use the same method as yours.

@Jmv38 That’s not quite right. Very few of us use CurrentTouch, and for a rectangular sprite one wouldn’t use the dist function.

I use the touched function not the currentTouch tag @Jmv38

@LoopSpace agreed. I really meant ’ we use the same principle = comparing a touch position to the rect sprite center and size’, i didnt find useful for his concern to go into details of the various implementations. I know his implementation is not the most efficient, but he’ll learn that by himself when needed.

@Coder1500 Besides your way, there are other ways to determine if a Sprite is touched. You can combine different shapes to try and cover the area of the Sprite. Here’s an example. Touch each Sprite to see the shapes used. If you want to get into even more math, you can use a polygon shape to better outline the Sprite. I don’t show any of that here.

function setup()
    rectMode(CENTER)
    ellipseMode(CENTER)
    print(630+95/2)
end

function draw()
    background(0)

    if not tch then
        sprite("Cargo Bot:Menu Game Button",100,400)
        sprite("Cargo Bot:Play Button",300,400)
        sprite("Small World:Tower",200,600)
    else
        fill(255)
        -- Menu Game Button
        rect(100,400,100,50)
        -- Play Button
        rect(300,385,140,50)
        rect(300,420,90,30)
        -- Tower
        ellipse(200,560,120,60)
        rect(200,630,40,95)
    end
end

function touched(t)
    if t.state==BEGAN or t.state==MOVING then
        tch=false
        -- Menu Game Button
        if t.x>50 and t.x<150 and t.y>375 and t.y<425 then
            tch=true
        end
        -- Play Button
        if t.x>230 and t.x<370 and t.y>360 and t.y<410 then
            tch=true
        end
        if t.x>255 and t.x<345 and t.y>405 and t.y<435 then
            tch=true
        end
        -- Tower
        if (t.x-200)^2/60^2+(t.y-560)^2/30^2 <= 1 then 
            tch=true
        end  
        if t.x>180 and t.x<220 and t.y>582 and t.y<678 then
            tch=true
        end
    end
    if t.state==ENDED then
        tch=false
    end
end

Thanks guys :slight_smile: