Setting focus on touched objects

I have several objects on the screen that I have set to drag around. They drag fine. However, I would like the object that I touch to “get focus” so that it is on top of the other objects that are draggable. In corona there is a set focus function. Does something like that exist in codea?

I should mention that I am trying to move a working game from Corona to Codea :slight_smile:

No. If the objects are 2D, then the “on topness” is determined by the drawing order. So the simplest way is to ensure that the currently touched object is drawn last. Exactly how to do that will depend on how you are drawing your objects.

@swiftjuan in practice the most reliable way to get the object on top is to duplicate it, put the copy on top and hide the original. When you lose focus, copy the changed values to the original, delete the copy and show up the original.
You could also of course move the object on top, but this will mean a lot of side effects to solve, and in the end more work. My 2 cents advice. Ho, and i think too this is how it was done in cargo-bot…

@Jmv38 - no need to hide the original if you are duplicating it exactly… :wink:

All you need to do is keep a note of which object is “on top”, draw them all normally, then at the very end, draw the top object again (as Jmv38 suggests). This minimises code changes.

@ignatz good to see you share this view.
i suggested to hide the original, because he wants to get the focus to modify it. So, unless you echo immediately all the changes (size, position), hiding it emporarily may be simpler.

Here’s an example of drawing the selected item on top without the need to draw extra objects or hide objects. In the touched routine, when I find the object being selected, I insert it at the end of the table and remove it from where it originally was in the table. That way the selected object is always the last item in the table and drawn last to keep it on top of every other object. Just select a sprite and move it on top of other sprites.


function setup()
    s=0  -- selected object
    for z=1,20 do

function draw()
    for a,b in pairs(tab) do
        sprite("Planet Cute:Character Boy",b.x,b.y)

function touched(t)
    if t.state==BEGAN then
        for a,b in pairs(tab) do
            if d<50 then
                table.insert(tab,tab[a])    -- move selected to end of table
                table.remove(tab,a) -- remove original from table
    if t.state==MOVING and s>0 then
    if t.state==ENDED then

@Dave1707 the problem is that, when you start doing some really long code, you generally need the object to go back to its layer after you’ve edited it. And there are different types of ‘layers’ to keep correct (draw, touch, events, parent/child, etc…) This is why i didnt advise your solution.

@Jmv38 Without knowing exactly how the code is going to work, it’s hard to come up with the perfect solution. I thought I would just show another way of doing it. That worked for me in some programs.

How do you hide an object?

usually in your object you define a visible property. Then in the draw

    if self.visible then

@swiftjuan I deleted all of you duplicate posts. If you get an XML error box, check the forum to see if it was posted before trying again.

This is where I get confused. There is no mention of visibility in the documentation that I can find. How is one to know that this property is available. Also, how would you apply that to the object?

@swiftjuan as i wrote you define this property (yourself). What is exactly your programming background? I you are a beginner, I am not sure that my answers are appropriate for you, because they assume you are familiar with object programming. Dave’s example is more clear.

Just to extend a little on what @Jmv38 is saying, you would create a class for the object, and set a variable for that class called visibility (self.visibility). You would then make instances of that class (instance = classname) and call those instances in the draw function (instance:draw). Each of those instances would have their own version of the self.visibility variable, so you can change that variable based on weather it’s touched (instance.visibility = false). Hope this helps

@swiftjuan - I think you are actually asking how you change the properties of an object to make it visible or not, like most other languages.

Codea doesn’t work like that.

Instead, think of Codea as a movie animation (or gif) that shows a series of pictures at 60 frames per second. If you have been drawing a circle in the middle, and you want to hide it, then just don’t draw it in the next frame. You do this by putting an if statement in the draw function that decides whether to draw the circle or not, based on a variable, eg

function draw()
    --some code
    if ShowCircle==true then
        sprite(C, x, y)
    --some code

Now you can turn the circle on and off by setting ShowCircle to true or false. You can move the circle around by changing the values of x and y.

Sweet I get it now :slight_smile: thanks all!