I NEED HELP (pls)

@Bri_G How old are they actually? I would like to know. Of course you don’t have to tell me that, but I would like to know, so @Dave1707 this code will definitely help me 100%! You solved my biggest problem!!! Thank you!!! I’ll get in touch if there’s anything

@Killer.Jo - here is my mods on @dave1707’s code, hoping it gives you what you need:


viewer.mode=FULLSCREEN

function setup()
    -- this function holds initial variables in
    ellipseColor=color(255) -- set initial ellipse color
    butMode = "active"
    butPos = vec2(WIDTH/2,HEIGHT/2)
end

function draw()
    -- this function holds all the graphics command
    background(236, 169, 67)
    fill(ellipseColor)          -- set ellipse color for the button
    if butMode == "active" then
        ellipse(butPos.x, butPos.y,200,200)   -- draw ellipse
    else
        -- do nothing
    end
end

function touched(t)
    -- this function captures and reacts to touches from data held in
    -- a table t, t.state tells you what the touch condition is
    -- BEGAN, ENDED or CHANGED
    if t.state == ENDED then
        if inCircle(t.pos,butPos,200/2) then
            if  butMode == "active" then
                butMode = "false"
            else
                -- if you want the button back after second touch
                -- then remove the -- from the next line
                -- butMode = "active"
            end
        end
    end
end

function inCircle(p, c, r)    
    -- p is the point, c is the center of the circle, and r is the radius
    -- this tests to see if the touch is in the button
    -- returns true if true or else false if not
    local dist = math.sqrt((p.x - c.x)^2 + (p.y - c.y)^2)
    if dist < r then return true else return false end
end

If you meant how old am I, then I am retired so that should give you an idea.

@Bri_G Wow @Bri_G, I didn’t know you retired at 40, :smiley:. Here’s is a better way to check if inside a circle (or ellipse).

    if (x-xc)^2/a^2+(y-yc)^2/b^2 <= 1 then 
        text("point in ellipse",xc,yc)        
    end    

x= touch x point
y= touch y point
xc= ellipse center x
yc= ellipse center y
a= size of a axis ( center to edge )
b= size of b axis ( center to edge )
a and b will be equal for a circle.

This will do any size circle or ellipse.

Do you have a link to Codea V4 documentation. All the links I find give 404 page errors.

@dave1707 - thanks for the routine. Had to dig for the link, think this is it - hope it’s the most up to date.

Codea 4 Docs

More directly try this:

Codea 4 Docs actual texts

And yet another one:

Other docs - textual

p.s. My original recorded link just sent me to a 404 page too. Note @John

@Bri_G @dave1707


function setup()
    bgColor = color(0) -- Background color to start with
    BUTTONclr = color(255, 0, 180)
    test01 = "Testi Mesti"
end

function draw()
    background(bgColor)-- show the background color
    if not buttonPressed then   -- if the button wasnt pressed
        sprite(asset.builtin.UI.Blue_Button_04, 550, 500) -- show button  
        sprite(asset.builtin.UI.Blue_Button_04, 550, 780)
        if CurrentTouch.state == BEGAN then -- check if the button is pressed        
            if CurrentTouch.x >= 550-75 and CurrentTouch.x <= 550+75 and 
            CurrentTouch.y >= 500-19 and CurrentTouch.y <= 500+19 then  
                buttonPressed=true      -- button was pressed, so set the flag
                bgColor=color(255, 0, 0) 
                print("Moin") -- set the new background color
            end   
        end
        
        if CurrentTouch.state == BEGAN then -- check if the button is pressed        
            if CurrentTouch.x >= 550-75 and CurrentTouch.x <= 550+75 and 
            CurrentTouch.y >= 780-19 and CurrentTouch.y <= 780+19 then  
               bgColor=color(232, 255, 0)
                buttonPressed=true
                sprite(asset.builtin.Space_Art.Asteroid_Small, 550, 500)
            end
    end
end end   

I thought I’d make myself a second button until here everything worked, but where I tried to add a text or add a sprite, it didn’t work for whatever reason. Could you help me? And @Bri_G have you ever released a game/app? Because retirement at 40 is not normal (should not sound offensive)

@Killer.Jo @Bri_G didn’t retire at 40, I was just kidding him about that. He’s probably around my age.

Here’s you code with changes. The reason the sprite didn’t show was because once you pressed the button, you never go back into that section of the code to show the sprite. Normally when you draw something, you have to do it constantly or it doesn’t show. The draw() function gets called 60 times every second and that’s what draws stuff to the screen. So whatever you want to show on the screen has to be in draw() or a function called from draw(). You can look over my changes and if you have questions, just ask.

function setup()
    bgColor = color(0) -- Background color to start with
    BUTTONclr = color(255, 0, 180)
    test01 = "Testi Mesti"
    textMsg=""
end

function draw()
    background(bgColor)-- show the background color
    
    if not buttonPressed then   -- if the button wasnt pressed
        textMsg=""  -- clear msg
        
        sprite(asset.builtin.UI.Blue_Button_04, 550, 500) -- show button  
        sprite(asset.builtin.UI.Blue_Button_04, 550, 780)       
        fill(0)
        text("Show Moin",550,500) -- text on button
        text("Show sprite",550,780)
        
        if CurrentTouch.state == BEGAN then -- check if the button is pressed        
            if CurrentTouch.x >= 550-75 and CurrentTouch.x <= 550+75 and 
            CurrentTouch.y >= 500-19 and CurrentTouch.y <= 500+19 then  
                buttonPressed=true      -- button was pressed, so set the flag
                bgColor=color(255, 0, 0) 
                textMsg="Moin"
            end   
        end
        
        if CurrentTouch.state == BEGAN then -- check if the button is pressed        
            if CurrentTouch.x >= 550-75 and CurrentTouch.x <= 550+75 and 
            CurrentTouch.y >= 780-19 and CurrentTouch.y <= 780+19 then  
                bgColor=color(232, 255, 0)
                buttonPressed=true
                drawSprite=true
            end
        end
    end 
    
    if textMsg~="" then     -- any message to display
        fill(255)
        fontSize(40)
        text("Moin",WIDTH/2,HEIGHT/2)
    end
    if drawSprite then
        sprite(asset.builtin.Cargo_Bot.Codea_Logo, 550, 500)
    end
end 

@Killer.Jo - @dave1707 is right, he was making a joke hence the smiling emoji. I did retire early but not that early.

Just a point of clarification - are you learning Codea at school ? or have picked it up yourself and want to learn by yourself and not alongside other students ?

Why I ask is that you keep using CurrentTouch and using it in the draw() function, which is fine. But the reason most programmers use the three setup() draw() and touched() functions is to make the coding simpler and easier to trace errors.

No, I’m not in any school and in my school we don’t do that either, but I get along with CurrentTouch and so much better because I got used to it

@Killer.Jo It’s time to stop using CurrentTouch and get used to the touched(t) function. CurrentTouch is just going to hold you back and not let you write better code.

@dave1707 @Bri_G


function setup()
    Hintergrund = readImage(asset.documents.Snake_World)
    GameNAME = "Jo's Snake!"
    clr = color(31)
    Snake = readImage(asset.builtin.Space_Art.Green_Ship)
    SNAKEcolor = ""
end

function draw()

    if not Snaking then
        sprite(Hintergrund, 550, 450, 1100, 1000)
        
        sprite(asset.builtin.Platformer_Art.Coin, 550, 500)
       end 
        if CurrentTouch.state == MOVING then
           sprite(Snake,  CurrentTouch.pos.x, CurrentTouch.pos.y, 100,100) 
    end
    
    if CurrentTouch.state == ENDED then
           sprite(Snake,  CurrentTouch.pos.x, CurrentTouch.pos.y, 100,100) 
    end
    
    if CurrentTouch.state == MOVING then -- check if the button is pressed        
            if CurrentTouch.x >= 550-75 and CurrentTouch.x <= 550+75 and 
            CurrentTouch.y >= 500-19 and CurrentTouch.y <= 500+19 then
            fill(255, 219, 0) 
            font("Arial-BoldMT")
            fontSize(30)
            text("Du bist auf einen COIN", 550, 800)
        end
    end 
if CurrentTouch.state == ENDED then -- check if the button is pressed        
            if CurrentTouch.x >= 550-75 and CurrentTouch.x <= 550+75 and 
            CurrentTouch.y >= 500-19 and CurrentTouch.y <= 500+19 then  
            text("Du bist auf einen COIN", 550, 800)
        end
end
end

viewer.retainedBacking = true
viewer.mode = FULLSCREEN_NO_BUTTONS

I made a new code. This time Snake ordered on Wish. A few times I had to use CurrentTouch but with Touched(t) I can’t do it so well because my problem was that I actually wanted to make a snake in a “function touched(t)” but that didn’t work because a function can’t draw into a function. But can it be done differently?

@Killer.Jo Heres your code using touched(t).
Anytime you use a sprite or anything else from your Dropbox or Documents folders, we don’t have access to them. So I commented out the hintergrund in my example.


viewer.retainedBacking = true
viewer.mode = FULLSCREEN

function setup()
    --Hintergrund = readImage(asset.documents.Snake_World)
    GameNAME = "Jo's Snake!"
    clr = color(31)
    Snake = readImage(asset.builtin.Space_Art.Green_Ship)
    SNAKEcolor = ""
    tx,ty=0,0    
end

function draw()    
    if not Snaking then
        --sprite(Hintergrund, 550, 450, 1100, 1000)        
        sprite(asset.builtin.Platformer_Art.Coin, 550, 500)
    end
    
    if tState == CHANGED then
        sprite(Snake,  CurrentTouch.pos.x, CurrentTouch.pos.y, 100,100) 
    end
    
    if tState == ENDED then
        --sprite(Snake,  CurrentTouch.pos.x, CurrentTouch.pos.y, 100,100) 
    end
    
    if tState == CHANGED then -- check if the button is pressed        
        if tx >= 550-75 and tx <= 550+75 and 
        ty >= 500-19 and ty <= 500+19 then
            fill(255, 219, 0) 
            font("Arial-BoldMT")
            fontSize(30)
            text("Du bist auf einen COIN", 550, 800)
        end
    end
     
    if tState == ENDED then -- check if the button is pressed        
        if tx >= 550-75 and tx <= 550+75 and 
        ty >= 500-19 and ty <= 500+19 then  
            text("Du bist auf einen COIN", 550, 800)
        end
    end
end

function touched(t)
    tState=t.state
    tx=t.x
    ty=t.y
end

@Killer.Jo - that’s a good start for you and @dave1707 has suggested improvements. A few things to point out:

  1. At the bottom of your code you have placed a command to set the graphics mode for your project -
    viewer.mode = FULL_SCREEN_NO_BUTTONS and viewer.retained = FALSE

The viewer.mode you set means you can’t use the buttons to capture the screen image or video during development or stop the project running, while developing it I would recommend running in FULLSCREEN so the buttons are present giving you those options.

Also I would place those lines at the top of your project so other users see them straight away when testing.

  1. The other option you have specified to stop retaining the background means that you get retained images from each cycle of the screen. Sometimes that’s needed for effect, but usually it makes a mess of the screen image, that option defers to true.

  2. Finally if you are testing things like touch inside an object it’s better to call a function designed for that like the inRect() or inEllipse() functions that have been posted on your thread. It makes things easier to understand and simplifies code when using repeated calls using math calcs.

@Killer.Jo @dave1707 - here is a little demo to show what external functions can do. It displays a random rectangle in size and colour after each touch. If you remark out the background() in the draw() function you will retain the rectangles on the same screen.


viewer.retainedBacking = true
viewer.mode = FULLSCREEN
function setup()
    --
    rc,gc,bc = 255, 128, 64
    rs = 128
    stc = color(67, 236, 80)
    pos = vec2(WIDTH//2,HEIGHT//2)
end

function draw()
    -- 
    background(0)
    fill(rc,gc,bc)
    rectMode(CENTER)
    stroke(stc)
    strokeWidth(8)
    rect(pos.x, pos.y, rs, rs)
end

function touched(t)
    --
    if t.state == ENDED then
        pos = t.pos
        setRectSize()
        setRectColor()
    end
end

function setRectSize()
    --
    rs = math.floor(math.random(255))
end

function setRectColor()
    --
    stc = color(rc,gc,bc)
    rc = math.floor(math.random(255))
    gc = math.floor(math.random(255))
    bc = math.floor(math.random(255))
end

Edit: @dave1707 - what’s the best way of demonstration the effect of viewer.retainedBackground options ?

@Bri_G Heres some code that uses backing mode.


viewer.mode = FULLSCREEN
viewer.retainedBacking = true

function setup()
    fill(255)
    xv,yv,x,y=5.1,5.2,WIDTH/2,HEIGHT/2
    col=color(255)
end

function draw()
    x=x+xv
    y=y+yv
    if x<10 or x>WIDTH-10 then
        xv=xv*-1
        col=color(math.random(255),math.random(255),math.random(255))
    end
    if y<10 or y>HEIGHT-10 then
        yv=yv*-1
        col=color(math.random(255),math.random(255),math.random(255))
    end
    fill(col)
    ellipse(x,y,10)
end

@dave1707 - thanks for that, the animation makes the demo more impressive than just tapping to make rectangles in my demo. Neat !!!

@Bri_G Can you show me an example with incircle?

@Bri_G I’ll play around with your code. But you had a good idea with the code

@dave1707 Your code looks kind of spectacular

@Killer.Jo - I posted an example of it when you asked me to post code selecting from several circular buttons. That was my inCircle() function. @dave1707 - posted another good function, which is slightly better as it deals with ellipses and, special case, circles. If you need any explanation after reading the code just post a query here.


function setup()
    buttonTXT = "Keywan"
    backGround = color(27)
    randomTXT = "MoinMoin"
end

function draw()
    if not buttonPressed then
        sprite(asset.builtin.UI.Blue_Button_04, 550, 500)
        fill(255, 14, 0)
        font("Arial-BoldItalicMT")
        fontSize(25)
        text(buttonTXT, 550, 500)
        
    end
        if CurrentTouch.state == BEGAN then        
            if CurrentTouch.x >= 550-75 and CurrentTouch.x <= 550+75 and 
            CurrentTouch.y >= 500-19 and CurrentTouch.y <= 500+19 then  
                buttonPressed=true  
        end end
        
        if buttonPressed then
            background(128)
            
        sprite(asset.builtin.Blocks.Brick_Red, 350, 350)
        
             if CurrentTouch.state == BEGAN then        
            if CurrentTouch.x >= 350-75 and CurrentTouch.x <= 350+75 and 
            CurrentTouch.y >= 350-19 and CurrentTouch.y <= 350+19 then  
                Test=true  
        
            end end 
    if Test then
            fill(108, 255, 0)
            font("Arial-ItalicMT")
            fontSize(30)
            background(255)
            text(randomTXT, 550, 700)
            sprite(asset.builtin.Blocks.Error, 550, 500)
            
            fill(205, 205, 57)
            font("ArialRoundedMTBold")
            text("Prank", 550, 500)
             if CurrentTouch.state == BEGAN then        
            if CurrentTouch.x >= 550-75 and CurrentTouch.x <= 550+75 and 
            CurrentTouch.y >= 500-19 and CurrentTouch.y <= 500+19 then  
                Random=true  
        end
        end
        
        end
    end 
   end 
            

I think it’s super cool what you can do with code. (If you know how) that’s why I played around with touches / (buttons)