Multitouch squares?

Hello,

Sorry for asking a noobish question.

What I want to do is write a program that draws a square/rectangle between two points you touch.
so if you touch 2 places the item is drawn and if you move your fingers around the square is redrawn to fit where your finger is.

I’m trying to learn by making simple things and already this is a little over my head, would be be easy to make?

Thank you.

Move 2 fingers around the screen.


function setup()
end

function draw()
    background(40, 40, 50)
    fill(255)
    stroke(255)
    strokeWidth(3)
    if p1~=nil and p2~=nil then
        line(p1.x,p1.y,p1.x,p2.y)
        line(p1.x,p2.y,p2.x,p2.y)
        line(p2.x,p2.y,p2.x,p1.y)
        line(p1.x,p1.y,p2.x,p1.y)
    end
end


function touched(t)
    if t.state==BEGAN then
        if p1==nil then
            p1=vec3(t.x,t.y,t.id)
        elseif p2==nil then
            p2=vec3(t.x,t.y,t.id)
        end
    end
    if t.state==MOVING then
        if p1~=nil then
            if t.id==p1.z then
                p1=vec3(t.x,t.y,t.id)
            end
        end
        if p2~=nil then
            if t.id==p2.z then
                p2=vec3(t.x,t.y,t.id)
            end
        end
    end
    if t.state==ENDED then
        if p1~=nil then
            if t.id==p1.z then
                p1=nil
            end
        end
        if p2~=nil then
            if t.id==p2.z then
                p2=nil
            end
        end
    end
end

@SteveNoob - I would learn a little Lua first, that will make things easier.

Lol! You always fire first Dave!
here is mine anyway


-- rectangle

function setup()
    x0,y0,x1,y1 = 0,0,0,0
    ida,idb = nil,nil
    xa,ya,xb,yb = 0,0,0,0
end


function draw()
    background(40, 40, 50)
    noSmooth() 
    noStroke()
    rectMode(CORNERS)
    fill(255, 174, 0, 255)
    rect(x0,y0,x1,y1)
end

function touched(touch)
    local state = touch.state
    local id = touch.id
    if state==BEGAN then 
        if ida == nil then ida = id
        elseif idb == nil then idb = id
        else return -- only 2 touch events accepted
        end
    end 
    if state==ENDED then 
        if id == ida then ida = nil
        elseif id == idb then idb = nil
        end
    end
    if id == ida then 
        xa = touch.x
        ya = touch.y
    end
    if id == idb then 
        xb = touch.x
        yb = touch.y
    end
    local min,max = math.min, math.max
    if ida and idb then
        x0 = min(xa,xb)
        y0 = min(ya,yb)
        x1 = max(xa,xb)
        y1 = max(ya,yb)
    end
end

Thanks a lot guys, wow thats complex but I will try it.

Ignatz, I thought maybe I’d learn easier by starting with very simple things, I guess this was more complex than I thought it would be. :slight_smile:

Thanks Dave and Jmv38 you guys are sooooo fast writing that code… wow!

@Jmv38 I forgot about the math.min and math.max functions. I was using rect at first, but it wasn’t working if I moved my fingers in a circular motion. That’s when I changed it to use the 4 line commands. I like yours better with the single rect command.

@SteveNoob starting with several touches is a bit complex.
Maybe just start drawing things on the screen.
Then use one touch only.
Then use seval touch. With several touch it is difficult because when a touch happen you dont know which one it is. So you have to check its id and remember it for next time is is fired… This is complex. Start with one touch only, you dont have to care about the id.

@SteveNoob - don’t be put off, just start slowly and build up. You will get there.

@Dave1707 thanks! I like mine better too :)) :)) :)) :))
Just kidding! It is interesting to look are the equalities and diffesrences between our codes.

Thank you all, very helpful and kind. :slight_smile:

I thought I’d ask a similar question here again, rather than spamming the codea forums with threads and annoying the mods :slight_smile:

I currently have ‘sprite(“SpaceCute:Health Heart”,CurrentTouch.x, CurrentTouch.y)’
Which works a treat and displays the sprite where I touch.

I’ve been trying to work out how to stamp a new copy of the sprite when I touch rather than just moving the sprite around.

Would there be a simple way to do that?

Edit: I found by moving the background colour command out of the draw loop and into the setup works and my sprites are added… The only problem is that the screen flickers like mad. Hmmm

Just remove the line “background(40, 40, 50)” from the draw() function. That way the whole Screen won’t be cleared anymore.

@SteveNoob If you remove the background line from draw then you should also put backingMode(RETAINED) in the setup function.

One way to imagine how Codea works is to imagine that the instructions are sent to a group of mad painters who paint the picture on a canvas which is then displayed on the screen. To make it more efficient, the painters have more than one canvas so that they can be working on one canvas while another is shown on the screen. If you don’t blank each canvas at the start of the draw loop, then stuff that is shown on the first canvas will not be shown on the second, but will only be shown when that first canvas is reused. There are actually three canvases, so it takes three loops before the stuff on the first canvas is seen again. This leads to the flicker. The backingMode(RETAINED) ensures that each canvas is copied to the next one before the new drawing starts, removing the flicker.

Thank you, that was very helpful.

I’ve run into another problem now with ‘if’…‘then’ statements, I guess they dont work quite the same as what I used many years ago. But as a noob I should expect to have nothing but problems for the first few weeks :wink:

What is your problem with “if then”?
One thing to keep in mind is to always close the statement with “end”. Even if it is just one line:
if a == 0 then b = true end

I tried to do this

if cnt=1 then spr=“Planet Cute:Gem Blue” end

with cnt being a simple numerical variable and spr being a string storing a sprite name.

But I just get a ‘then’ expected near “=” error.

I did originally put the string variable in quotes “str” as thats how strings were defined in almost all of my previous programming experience.

if cnt == 1 then
    spr="Planet Cute:Gem Blue"
end

A single = is an assignment, a double is a test.

(Incidentally, please read the FAQ on how to format code on the forum.)

same timing as Andrew_Stacey

Damn. I’m so sorry to post such a lame question.

Sorry to annoy you experts even more but… I am missing something here.

My code

-- Test 1

-- Use this function to perform your initial setup
function setup()
    print("Sprite Test")
    background(40,40,50)
    backingMode(RETAINED)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 

    -- This sets the line thickness

    -- Do your drawing here
   for cnt=1, 3 do
    if cnt==1 then spr="Planet Cute:Gem Blue" end
        if cnt==2 then spr="Planet Cute:Gem Green" end
            if cnt==3 then spr="Planet Cute:Gem Orange" end
            sprite(spr,CurrentTouch.x,CurrentTouch.y)
    end
   parameter.action("Clear Screen", function() background (40,40,50) end ) 
end

I am trying to use the loop to cycle between 3 sprites but it only ever shows the orange sprite. Have tried different things and even got it to print out the values for cnt and spr as it ran and it looked fine, but I don’t know why its not changing the sprite.

Thank you very much for your help.

What are you doing at each draw cycle?
Check your code carefuly and you’ll see you get exactly what you asked for.
If after 5 minutes of inspection you still cant find it, i’ll write the answer here. :wink: