I NEED HELP (pls)

@Killer.Jo - picking up on your theme, you tend to use repetitive code to see effects. Posted this demo to illustrate math.random() but also shows how the external touched function can give better control than CurrentTouch.


function setup()
    --
    
    Mystery = math.random(1, 10)
    BGcolor = color(math.random(255), math.random(255), math.random(255))
end

function draw()
    --
    background(BGcolor)
    fill(0)
    font("AmericanTypewriter-Bold")
    fontSize(48)
    text("The Mystery : "..Mystery, WIDTH//2,HEIGHT-80) 
    sprite(pics[Mystery],WIDTH//2,HEIGHT//2)
end

function touched(touch)
    -- 
    if touch.state == ENDED then
        Mystery = math.random(1, 10)
        BGcolor = color(math.random(255), math.random(255), math.random(255))
    end
end

pics = {
        asset.builtin.Blocks.Redstone,
        asset.builtin.Blocks.Stone_Coal_Alt,
        asset.builtin.Blocks.Brick_Grey,
        asset.builtin.Blocks.Cotton_Green,
        asset.builtin.Blocks.Fence_Stone,
        asset.builtin.Planet_Cute.Gem_Blue,
        asset.builtin.Planet_Cute.Enemy_Bug,
        asset.builtin.Planet_Cute.Gem_Orange,
        asset.builtin.Planet_Cute.Character_Boy,
        asset.builtin.Planet_Cute.Character_Cat_Girl
        }

@Bri_G Ok, I’ll play around with the code. But I still have a question: There was a problem with my previous code. (The code with the “start function”) because there were bugs (e.g. if you went back to the menu and you clicked somewhere you came back (where you just were)) and I don’t want that, I try to try as long as I try.

@Killer.Jo OK - I have a problem with your explanation. When I run your code it fires up with a black screen and an x button. When the button is clicked you go to another screen with several sprites on it which respond to touches. The following screens return you to the screen with buttons on. Which screen is your main menu ?

@Bri_G The main menu is the screen when you start the app and click on the “x”

@Killer.Jo - so is that the black screen wit X or the blue screen with sprites. Also when you tap on the icons to get your option screens which screen do you want to return to ?

Just need to find tune it now

@Bri_G So, when you start the app and click the X, you get to the main menu. If you click any button and then click on the X, you should get to the main menu. But bugs have occurred with my code (e.g. if you return to the main menu and then click somewhere, you get back to where you were just

Edit: Edit: Is very probably ultra difficult but could you also do that when you return to the main menu and you click again e.g. “Start” that you keep your stand? (The things you did before starting the screen should be kept) if they understand that, that would be really super duper cool :sunglasses:

@Killer.Jo Maybe you should take a piece of paper and draw a flow chart showing what screens you want to go to when you tap certain sprites. That would make it easier for you and us if we know what screen we need to go to and from where.

@dave1707

I hope you can read it, attention: In the end you should also get 5 coins

I put it in my code like this:


function setup()
    Coins = 0
end

function draw()
    background(255)
    text(Coins, 550, 750)
    
      if CurrentTouch.state == ENDED then
          Coin=true
        end
    
        
        end

If Coin then
Coins = Coins + 5
end

@Bri_G @dave1707

Yes! I just managed to do my first Air Code project! I wanted to try it last week but it didn’t work out but now it worked!!! Here is the code:


function setup()
end
function draw()
    background(255)
    
    fill(200)
    text("krise", 550, 500)
    
    fill(90)
    text("ABER FETT DIGGI", 550, 300)
    
    if CurrentTouch.state == ENDED then
        background(88)
        fill(1)
        fontSize(50)
        text("klicke lieber, um raus zu kommen", 550, 500)
        
    end end

@Killer.Jo @dave1707 - thanks for input and the flow chart, I’ll finish the code soon.

@Killer.Jo - which Aircode are you using - the one in a browser ?

@Bri_G So I did it on my parents’ iMac in the browser (of course they also allowed me)

@Killer.Jo - played around a bit, not sure if this is what you had in mind but seems to work OK, may need a little fine tuning. Added a few extras.

I left an aid in to show where the touch is picked up on the screen. To see it you have to set LocRect to True. Hope that helps

viewer.mode = FULLSCREEN
function setup()
    --

    -- Coins
    Coins = 0
    option = ""
    menuOpt = ""
    
    menu = vec2(800,180)
    skiner = vec2(800,300)
    farmer = vec2(550,700)
    boxer = vec2(200,200)
    character = vec2(550,500)
    chester = vec2(550,790)
    chestClosed = vec2(150,500)
    butnX = vec2(900,680)
    
    option = "Start11"
    locRect = false
end

function draw()
    -- 
    aspect = HEIGHT/WIDTH
    if aspect > 1 then
        background(0, 233)
        pushStyle()
            font("Georgia-BoldItalic")
            fontSize(32)
            fill(255, 14, 0)
            textMode(CENTER)
            text("Please Run In Landscape Mode", WIDTH/2,HEIGHT/2)
        popStyle()
    else
        if option == "Start11" then
            background(0)
            sprite(pics[13], butnX.x, butnX.y)
            pushStyle()
                fill(128,0,0)
                font("Georgia-BoldItalic")
                fontSize(64)
                textMode(CENTER)
                text("Farmer Game", WIDTH/2, HEIGHT/2)
                fill(255,0,0)
                text("Farmer Game", WIDTH/2+4, HEIGHT/2+4)  
                fontSize(32)
                fill(128,0,0)
                text("by - Killer Jo",WIDTH/2, HEIGHT/2-64)
            popStyle()
        elseif option == "menu" then
            background(colr[1])  
            font("ArialRoundedMTBold")
            text("Start", 800, 170)
            fontSize(20)
            fill(255, 27, 0)
            sprite(pics[2], skiner.x, skiner.y)
            if locRect then testRect(skiner.x,skiner.y,120) end
            text("Box", 800, 200)
            if locRect then testRect(800,180,80) end
            sprite(pics[3], farmer.x, farmer.y)
            text("Farm (Island)", 550, 640)
            if locRect then testRect(farmer.x,farmer.y,80) end
            sprite(pics[4], boxer.x, boxer.y)
            text("Your 2. Box", 200, 116)
            if locRect then testRect(boxer.x,boxer.y,92) end
            sprite(pics[5],  character.x, character.y)
            if locRect then testRect(character.x,character.y-16,100) end
            sprite(asset.builtin.Planet_Cute.Chest_Closed, chestClosed.x, chestClosed.y)
            if locRect then testRect(150,474,128) end
        elseif option == "skiner" then
            background(colr[2])
            sprite(pics[13], butnX.x, butnX.y)
        elseif option == "farmer" then
            background(colr[3])
            sprite(pics[13], butnX.x, butnX.y)
        elseif option == "boxer" then
            background(colr[4])
            sprite(pics[13], butnX.x, butnX.y)
        elseif option == "chestClosed" then
            background(colr[5])  
            sprite(pics[8], butnX.x, butnX.y)
        elseif option == "character" then
            background(colr[6]) 
            sprite(pics[13], butnX.x, butnX.y)
        end
    end
end
    
function touched(t)
    --
    if t.state == ENDED then
        if t.pos.x >= butnX.x-75 and t.pos.x <= butnX.x+75 then
            if t.pos.y >= butnX.y-19 and t.pos.y <= butnX.y+19 then
                option = "menu"
            end
        elseif t.pos.x >= 800-75 and t.pos.x <= 800+75 then
            if t.pos.y >= 150-19 and t.pos.y <= 150+19 then 
                option = "skiner"
                sprite(pics[13], butnX.x, butnX.y)
            elseif t.pos.y >= 300-19 and t.pos.y <= 300+19 then
                option = "skiner"
                sprite(pics[13], butnX.x, butnX.y)
            end
        elseif t.pos.x >= 550-75 and t.pos.x <= 550+75 then
            if t.pos.y >= 700-46 and t.pos.y <= 700+46 then   
                option = "farmer"
                sprite(pics[13], butnX.x, butnX.y)
            end
        elseif t.pos.x >= 200-75 and t.pos.x <= 200+75 then 
            if t.pos.y >= 200-19 and t.pos.y <= 200+19 then   
                option = "boxer"
                sprite(pics[13], butnX.x, butnX.y)
            end
        elseif t.pos.x >= 150-75 and t.pos.x <= 150+75 then 
            if t.pos.y >= 500-19 and t.pos.y <= 500+19 then   
                option = "chestClosed"
                sprite(pics[13], butnX.x, butnX.y)
            end
        elseif t.pos.x >= 570-75 and t.pos.x <= 570+75 then 
            if t.pos.y >= 480-50 and t.pos.y <= 480+50 then 
                option = "character"
                sprite(pics[13], butnX.x, butnX.y)
            end
        elseif option == "skiner" or option == "farmer" or option == "boxer" or option == "character" and t.pos > vec2(0,0) then
            option = "menu"
            sprite(pics[13], butnX.x, butnX.y)
        end
    end
    -- print(t.pos)
    -- print("Option : "..option)
end


menu = vec2(800,150)     -- ski text
skiner = vec2(800,300)      -- ski images
farmer = vec2(550,700)      -- farmer
boxer = vec2(200,200)       -- box icon
character = vec2(550,500)   -- character icon
chester = vec2(550,600)     -- start icon
chestClosed = vec2(150,500) -- closed chest image

pics = {
        asset.builtin.UI.Green_Button_00,
        asset.builtin.SpaceCute.Beetle_Ship,
        asset.builtin.Platformer_Art.Block_Grass,
        asset.builtin.Small_World.Watch_Tower,
        asset.builtin.Planet_Cute.Character_Pink_Girl,
        asset.builtin.Planet_Cute.Chest_Closed,
        asset.builtin.Blocks.Glass,
        asset.builtin.UI.Blue_Box_Cross,
        asset.builtin.Blocks.Dirt_Snow,
        asset.builtin.Blocks.Dirt,
        asset.builtin.Planet_Cute.Character_Boy,
        asset.builtin.Blocks.Glass,
        asset.builtin.UI.Blue_Button12
        }

colr = {
        color(0, 229, 255),         -- menu
        color(229, 97, 229, 247),   -- jetski
        color(85, 130, 61),         -- farmer
        color(138, 83, 79),
        color(169, 149, 73),
        color(255, 245, 0)
        }

@Bri_G Thanks for the code. But I don’t quite understand the code.

@Killer.Jo - basically the setup initialises the variables you need for your project. Other data is provided at the end of the code in tables, like your sprite addresses. You don’t need to use readInage() if they are in Codes asset lists.

The touched function traps the touch() and sets up the state’s for different views in the draw() function. The draw() first tests the orientation and fires up a message if it is in portrait mode, otherwise it fires up a splash screen with a button to progress.

The page variable holds the name of the visible page you have selected from splash to menu. The icons on the menu page select other pages each of which is provided with an exit button back to the menu page.

Both the touch() and the draw() functions need nested structures to select and display your screens.

To expand it you will have to add content in the draw() function and more touch traps to provide your movement around the project.

@Bri_G Ok! That helped me a lot

I made a click game!


function setup()
    click = 0
    
    
end

function draw()
    background(34)
    
    
    sprite(asset.builtin.UI.Blue_Panel, 200, 300)
    
    if click > 99 then
        background(128)
        fill(74, 255, 0)
        text("Erfolg 1!", 550, 700)
       sprite(asset.builtin.UI.Blue_Panel, 200, 300) 
    end
    
    if click > 249 then
        background(255)
        fill(74, 255, 0)
        text("Erfolg 2!", 550, 700)
       sprite(asset.builtin.UI.Blue_Panel, 200, 300) 
    end
    
    
    
    if click > 249 then
        background(229, 255, 0)
        fill(74, 255, 0)
        text("Erfolg 2!", 550, 700)
       sprite(asset.builtin.UI.Blue_Panel, 200, 300) 
    end
    
    if click > 499 then
        background(255, 0, 0)
        fill(74, 255, 0)
        text("Erfolg 3!", 550, 700)
       sprite(asset.builtin.UI.Blue_Panel, 200, 300) 
    end
    
    text(click, 550, 500)
end

function touched(t)
    
    if t.state == ENDED then
    click = click + 1
    
end
end

@dave1707 @Bri_G

Now I know why I shouldn’t use CurrentTouch but touched(t)… I had a problem. Although my problem was that when I do this “option tactics”, then somehow it didn’t work out. And just now I tried and managed with Touched(t)!!! (So this text was written again with a translator because I can’t speak English so well. So if that sounds strange)

Edit: Here a code:


-- The "option strategy" no longer works

-- With Touched(t) 
-- NO currentTouch

function setup()
    Test = "Menu"
end

function draw()
    
    background(25)
    text("Click.", 25, 500)
    
   function touched(t) 
    if t.state == BEGAN then
            option = "Starting"
        end
        
        
    end
    
    if option == "Starting" then
        background(128)
        text("This is the Start", 550, 500)
        
        print(Test)
    end
end

@Killer.Jo The touched function shouldn’t be put in the draw function, but as a function by itself. Anytime you touch the screen, the touched function will execute whatever code is inside it.


function setup()
    fill(255)
    Test = "Menu"
end

function draw()    
    background(25)
    text("Click.", WIDTH/2, 500)       
    if option == "Starting" then
        background(128)
        text("This is the Start", 550, 500)        
        print(Test)
    end
end
    
function touched(t) 
    if t.state == BEGAN then
        option = "Starting"
    end    
end

@dave1707 @Bri_G


local ops = ""

function setup()
    Test = "Menu"
    
    viewer.mode = FULLSCREEN
    
    showKeyboard()
    
    words = ""
    
    ops = "Name"
end

function keyboard(key)
    
    if key == RETURN then
        
        ops = "Starting"
        
    elseif key == BACKSPACE then
        words = string.sub(words, 1, -2)
    else
        words = words .. key
    end 
end

function touched(t)
    
    if t.state == BEGAN then
        
        if t.x >= 550-75 and t.x <= 550+75 and
        t.y >= 700-19 and t.y <= 700+19 then
            
            ops = "Starting"
            ops = "Name"
           
        elseif ops == "Starting" then
            ops =  "e"
            if t.state == BEGAN then
        if t.x >= 900-75 and t.x <= 900+75 and
        t.y >= 150-19 and t.y <= 150+19 then
            ops = "Start"
        elseif t.x >= 200-75 and t.x <= 200+75 and
        t.y >= 525-19 and t.y <= 525+19 then
            ops = "Banner"
        elseif t.x >= 525-75 and t.x <= 525+75 and
        t.y >= 700-19 and t.y <= 700+19 then
            ops = "Island"
        elseif t.x >= 900-75 and t.x <= 900+75 and
        t.y >= 750-19 and t.y <= 750+19 then
            ops = "Starting"
         end   
            
        end end
        
    end
    
end

function draw()
    
    background(128)
    
    text("Wähle einen Namen aus!", 550, 300)
    
    fill(255, 14, 0)
    text(words, 550, 500)
    
    sprite(asset.builtin.SpaceCute.Background, 550, 700, 100)
    text("Weiter", 550, 700)
    
    music(asset.documents.Mein_Song_7)
    
    if ops == "Starting" then
        
        background(128)
        
        fill(0, 171, 255)    
        sprite(asset.documents.Start_buttonKJ, 900, 150, 100)
        
        sprite(asset.documents.Banner00, 200, 525, 100)
        
        text("Symbol", 200, 525)
        
        sprite(asset.documents.Islamd, 525, 700, 100)
        
        text("Island", 525, 700)
        
        sprite(asset.documents.Monster, 525, 200, 100)
        
    elseif ops == "Start" then
        
        background(128)
        
    elseif ops == "Island" then
        
        background(77, 143, 56)
        
    elseif ops == "Banner" then
        
        background(47, 47, 26)
        
    end 
    
    
    sprite(asset.builtin.UI.Blue_Box_Cross, 900, 750)
    
    

        
 

end




If you have entered your name and click on certain positions, you will come back. And my 2 mistake is that the “back button” doesn’t work