Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Setting focus on touched objects

edited August 2014 in Questions Posts: 24

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?

Tagged:

Comments

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

  • Posts: 430

    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.

  • Jmv38Jmv38 Mod
    Posts: 3,295

    @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...

  • IgnatzIgnatz Mod
    edited August 2014 Posts: 5,396

    @Jmv38 - no need to hide the original if you are duplicating it exactly.. ;)

    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.

  • Jmv38Jmv38 Mod
    Posts: 3,295

    @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.

  • dave1707dave1707 Mod
    Posts: 7,605

    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.


    displayMode(FULLSCREEN) function setup() s=0 -- selected object tab={} for z=1,20 do table.insert(tab,vec2(math.random(WIDTH),math.random(HEIGHT))) end end function draw() background(0,0,0) for a,b in pairs(tab) do sprite("Planet Cute:Character Boy",b.x,b.y) end end function touched(t) if t.state==BEGAN then v1=vec2(t.x,t.y) for a,b in pairs(tab) do d=v1:dist(vec2(b.x,b.y)) if d<50 then table.insert(tab,tab[a]) -- move selected to end of table table.remove(tab,a) -- remove original from table s=#tab end end end if t.state==MOVING and s>0 then tab[s].x=t.x tab[s].y=t.y end if t.state==ENDED then s=0 end end
  • Jmv38Jmv38 Mod
    Posts: 3,295

    @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.

  • dave1707dave1707 Mod
    Posts: 7,605

    @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?

  • Jmv38Jmv38 Mod
    Posts: 3,295

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

    myObject:draw()
        if self.visible then
    ...
        end
    end
    
  • 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?

  • dave1707dave1707 Mod
    edited August 2014 Posts: 7,605

    @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.

  • Jmv38Jmv38 Mod
    Posts: 3,295

    @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.

  • Posts: 216

    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

  • IgnatzIgnatz Mod
    Posts: 5,396

    @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)
        end
        --some code
    end
    

    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 :) thanks all!

Sign In or Register to comment.