I NEED HELP (pls)

Yuuup, looking for a good example.

Okay :ok_hand::joy: (No idea why I‘m doing the laugghing smiley)

Hello are you there now?

@Bri_G ARE YOU THERE ??? HELLO???

Are you THEREEEEE


Still playing - back shortly.

Edit : Still fiddling with my simple demo, in the meantime here is a simple demo from the Ignatz site - hope this helps.

Ignatz Snake

Oh thanks!!!
you very muvh

@Bri_G Actually, I wanted to have a button all the time (e.g.), and when I click on it, a new window opens, so to speak, and then I can “in” things myself and you can also close the "window“



I made this code myself. But there is a problem. So the texts and so are in German, but if you click on the “Cotton red” and you move your finger except the if then the green background is gone. Is it possible to remove it?

@Killer.Jo - actually reading this from images is very difficult. A lot of the code seems to be missing. To get the best from posting code you need to post it as preformatted text. Look at the forum editing window bar, there is a settings gear at the right. Tap that and you get a list of options of which the top one is ‘preformatted text’ when you tap that three lines are added to the editing window the top and bottomones are there as delimetters for your code. The centre line reads ‘preformatted text’ and is highlighted.
Select an copy your code from Codea andtap on the highlighted text in the forum edit window and it will paste in your code. Exit edit and press reply and the code is then available to forum members to easily copy and use.

Edit: your formatting seems to have infected my post - both narrow width !!!

Edit 2: Closed forum and reopened and formatting OK now !!!


function draw()
    viewer.retainedBacking = true
    
    sprite(asset.builtin.SpaceCute.Planet, 550, 500)
    
    if CurrentTouch.state == MOVING and CurrentTouch.x >= 450 and CurrentTouch.x <= 600 and CurrentTouch.y >= 450 and CurrentTouch.y <= 600 then  
    sprite(asset.builtin.SpaceCute.Planet, 550, 500)
        fill(255, 14, 0)
    text("Wird zerstört... (grad = 446)", 550, 200)
        fill(138, 255, 0)
        text("Bombe wird plaziert...", 350, 200)
    end 
    
    if CurrentTouch.state == BEGAN and CurrentTouch.x >= 450 and CurrentTouch.x <= 600 and CurrentTouch.y >= 450 and CurrentTouch.y <= 600 then  
    sprite(asset.builtin.SpaceCute.Planet, 550, 500)
        fill(255, 14, 0)
    text("Wird zerstört... (grad = 446)", 550, 200)
        fill(138, 255, 0)
        text("Bombe wird plaziert...", 350, 200)
    end 
    
    
    if CurrentTouch.state == ENDED and CurrentTouch.x >= 450 and CurrentTouch.x <= 600 and CurrentTouch.y >= 450 and CurrentTouch.y <= 600 then  
     sprite(asset.builtin.SpaceCute.Planet, 550, 500)
        fill(58, 255, 0)
    text("NĂ€chster Planet.", 550, 200)
        fill(0, 247, 255)
        text("Bombe plaziert...", 350, 200)
        
    
    end 
    noSmooth()
    noStroke() 
    
    
    sprite(asset.builtin.Blocks.Cotton_Red, 700, 700)
    
    if CurrentTouch.state == ENDED and CurrentTouch.x >= 600 and CurrentTouch.x <= 800 and CurrentTouch.y >= 600
 and CurrentTouch.y <= 800 then
        background(154, 233, 80)
        background(73, 133, 38)
        sprite(asset.builtin.Blocks.Grass3, 400, 350)
    
        if CurrentTouch.state == MOVING then
            background(169, 233, 80)
        end
        
        if CurrentTouch.state == BEGAN then
            background(167, 233, 80)
        end
    end 
    
   if CurrentTouch.state == MOVING and CurrentTouch.x >= 600 and CurrentTouch.x <= 800 and CurrentTouch.y >= 600
 and CurrentTouch.y <= 800 then
        background(154, 233, 80)
        background(73, 133, 38)
        sprite(asset.builtin.Blocks.Grass3, 400, 350)
    
        if CurrentTouch.state == MOVING then
            background(169, 233, 80)
        end
        
        if CurrentTouch.state == BEGAN then
            background(167, 233, 80)
        end
    end 
    
     if CurrentTouch.state == BEGAN and CurrentTouch.x >= 600 and CurrentTouch.x <= 800 and CurrentTouch.y >= 600
 and CurrentTouch.y <= 800 then
        background(154, 233, 80)
        background(73, 133, 38)
        sprite(asset.documents.Planet_2, 400, 350)
end
    
    sprite(asset.builtin.Space_Art.Green_Ship, CurrentTouch.pos.x, CurrentTouch.pos.y, 100,100)

ellipse(500, 500)
end

@killer.Jo - nice to see some code from you. I’ll play around with it and post back shortly.

Ah good!!! I hope you’re not overwhelmed because that might be a lot of work. And by the way: I write all this with translator

@Killer.Jo - played around with your code. I think you misunderstood the basics of Codea. You don’t need to put massive code in the draw() function. You need to capture touch events and change variables which you then use in draw(). Also setting up variables etc in Setup() can simplify and help you understand what you need. Setting up variables with a relevant name is easier to understand than numbers.

Code is here:


-- Killer.jo 

viewer.mode = FULLSCREEN
viewer.retainedBacking = true
function setup()
    --
    -- sprite definitions
    planet = asset.builtin.SpaceCute.Planet
    pw, ph = spriteSize(planet)
    red = asset.builtin.Blocks.Cotton_Red
    grass = asset.builtin.Blocks.Grass3
    ship = asset.builtin.Space_Art.Green_Ship
    prt = ""
    fcol = color(67, 236, 82)
    txt1 = "Wird zerstör"
    txt2 = "Bombe wird plaziert"
    txt3 = "NĂ€chster Planet"
    txt4 = "Bombe plaziert"
    midX, midY = WIDTH//2, HEIGHT//2
    planetPos = vec2(midX, midY)
    msgPos = vec2(midX,HEIGHT-60)
    planetSize = 200
    planetRad = planetSize//2
    startPos = vec2(200,200)
    buttonPos = vec2(planetPos.x+150, planetPos.y+200)
    buttonSize = 100
    shipPos = startPos
end

function draw()
    -- 
    background(0)
    pushMatrix()
        spriteMode(CENTER)
        sprite(planet, midX, midY, pw, ph)
        sprite(ship,shipPos.x, shipPos.y, 100, 100)
        sprite(red, buttonPos.x, buttonPos.y, buttonSize, buttonSize)
        textMode(CENTER)
        fill(fcol)
        font("Baskerville-BoldItalic")
        fontSize(32)
        text(prt,msgPos.x, msgPos.y)
    popMatrix()
end

function touched(t)
    --
    if t.state == BEGAN or t.state == MOVING then
        shipPos = t.pos
        if inCircle(t.pos, planetPos, planetRad) then
            if t.state ~= ENDED then
                fcol = color(255, 0, 45)
                prt = "Over The Planet Drop Bomb !!"
            end
        else
            fcol = color(67, 236, 82)
            prt = "Outside the planet"
        end
    end
end

function inCircle(p, c, r)    
    -- p is the point, c is the center of the circle, and r is the radius
    print(p,c,r)
    local dist = math.sqrt((p.x - c.x)^2 + (p.y - c.y)^2)
    if dist < planetSize then return true else return false end
end

As you can see you can write text dialogue outside the reformatted text.

I wasn’t sure what you was trying to achieve so you may need to modify this code a lot.
I added a inCircle() function to avoid lots of numbers in comparisons. Not, the planet sprite is not a circle as you can see in the values of pw, ph but the code roughly shows how it works.

So in summary, set up variables in Setup(), capture touch events in touched() and modify variables then use draw() to generate your screen

Hope that helps.

Edit: As you grow to understand the structured approach you will introduce more external functions to carry out small features of code, generally repetitive code.

@Bri_G Yes, thank you very much, But actually I meant that when I due sprite “asset.builtin.Blocks.Cotton_Red” then comes a green background. But if you click somewhere again, the green background is gone. And I want to avoid that. Can you give me an improved code? :sweat_smile::sweat_smile::sweat_smile:

@Killer.Jo - OK, try this


viewer.mode = FULLSCREEN
viewer.retainedBacking = true
function setup()
    --
    -- sprite definitions
    planet = readImage(asset.builtin.SpaceCute.Planet)
    pw, ph = spriteSize(planet)
    button = readImage(asset.builtin.UI.Red_Box_Tick)
    grass = readImage(asset.builtin.Blocks.Grass3)
    ship = readImage(asset.builtin.Space_Art.Green_Ship)
    prt = ""
    fcol = color(236, 67, 88)
    tcol = color(0, 233, 255)
    bkgnd = color(67, 236, 82)
    txt1 = "Wird zerstör"
    txt2 = "Bombe wird plaziert"
    txt3 = "NĂ€chster Planet"
    txt4 = "Bombe plaziert"
    midX, midY = WIDTH//2, HEIGHT//2
    planetPos = vec2(midX, midY)
    msgPos = vec2(midX,HEIGHT-60)
    planetSize = 200
    planetRad = planetSize//2
    startPos = vec2(200,200)
    buttonPos = vec2(planetPos.x+150, planetPos.y+200)
    buttonSize = 100
    buttonRad = buttonSize//2
    shipPos = startPos
    zapped = false
end

function draw()
    -- 
    if zapped == false then
        background(0)
    else
        background(bkgnd)
    end
    pushMatrix()
        spriteMode(CENTER)
        if zapped == false then
            sprite(planet, midX, midY, pw, ph)
        end
        sprite(ship,shipPos.x, shipPos.y, 100, 100)
        sprite(button, buttonPos.x, buttonPos.y, buttonSize, buttonSize)
        textMode(CENTER)
        font("Baskerville-BoldItalic")
        fontSize(32)
        text(prt,msgPos.x, msgPos.y)
    popMatrix()
end

function touched(t)
    --
    if t.state == BEGAN or t.state == MOVING then
        touchPos = t.pos
        if inCircle(t.pos, buttonPos, buttonRad) then
            fill(tcol)
            prt = "Kaaaaboooom !!!"
            zapped = true 
        elseif inCircle(t.pos, planetPos, planetRad) then
            shipPos = touchPos
            if t.state ~= ENDED then
                fill(fcol)
                prt = "Over The Planet Drop Bomb !!"
            end
        else
            shipPos = touchPos
            fill(tcol)
            prt = "Outside the planet"
        end
    end
end

function inCircle(p, c, r)    
    -- p is the point, c is the center of the circle, and r is the radius
    print(p,c,r)
    local dist = math.sqrt((p.x - c.x)^2 + (p.y - c.y)^2)
    if dist < planetSize then return true else return false end
end

Edit: Updated the code to make the project do what I think you wanted and took out my own mistakes. Hope this meets your needs


1 Like

Thannnnnnnkssssss!!!

Can you make a simple game with a main menu with several buttons and when you click on one come more buttons and there is a back button that you come back to the main menu. I think at least then, have you FINALLY solved my problem that would be the best moment of my life (in programming) then I will FINALLY write you other things in Codea talk then you have also done so much for me you can then rest from me :sweat_smile::pray: and I know that this is a lot of work I hope that you forgive me

@Killer.Jo - OK, here is what I think you asked for. Now I’m going to get stuck into my own project. When you’ve finished post your project so we can see how well you’ve done.


viewer.mode = FULLSCREEN
function setup()
    --
    midX, midY = WIDTH//2, HEIGHT//2
    buttonMaster = asset.builtin.UI.Blue_Box_Tick
    masterPos = vec2(midX, midY)
    returnPos = masterPos
    buttons = {
                asset.builtin.UI.Green_Box_Tick,
                asset.builtin.UI.Grey_Box_Tick,
                asset.builtin.UI.Red_Box_Tick,
                asset.builtin.UI.Yellow_Box_Tick,
                asset.builtin.UI.Blue_Circle
                }
    butSize = 80
    butRad = butSize//2
    chosen = false
    mode = "start"
    str = "Option not chosen yet"
end

function draw()
    -- 
    background(19, 19, 23)
    if mode == "start" then
        pushMatrix()
            spriteMode(CENTER)
            sprite(buttonMaster, midX,midY, butSize, butSize)
    elseif mode == "options" then      
            for lp = 1, 4 do
                sprite(buttons[lp],lp*160, 800, butSize, butSize)
            end
            ret = #buttons
            fill(67, 236, 71)
            font("Arial-BoldItalicMT")
            fontSize(48)
            text(str, midX, HEIGHT-60)
            sprite(buttons[ret],midX, midY, butSize, butSize)
    
        popMatrix()
    end
end

function touched(t)
    --
    if t.state == BEGAN then
        myTouch = t.pos
        if mode == "start" and inCircle(myTouch, masterPos, butSize//2) then
            mode = "options"
        elseif mode == "options" and inCircle(myTouch, returnPos, butSize) then
            mode = "start"  
        else 
            for lp = 1, 4 do
                if inCircle(myTouch, vec2(lp*160,800), butRad) then
                    chosen = "true"
                    str = "Button pressed is : " .. lp
                end
            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
    local dist = math.sqrt((p.x - c.x)^2 + (p.y - c.y)^2)
    if dist < butRad then return true else return false end
end

Okay, wait a few days and then maybe I’ll be done

How can I make even more buttons and how can I make a different execution instead of a text (I know that I’m a complete beginner, I just don’t understand all thisđŸ„Č)