Howdy, Stranger!

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

Help with tables and arrays?!

I still can't get my head around tables and arrays what's the difference and how do they work and what do they do.Does anyone have any tutorials I know reefwing made one but I don't understand it.

Tagged:

Comments

  • dave1707dave1707 Mod
    edited February 2016 Posts: 7,528

    Do a forum search for tables or arrays and you'll find plenty of examples and explanations.

  • Posts: 75

    Thanks @Ignatz and @dave1707 I have a question though if I have a Sprite in here like this.

    robot = {sprite(...),sprite(...)}

    How can I display this because
    sprite(robot) doesn't work.

  • Posts: 2,020

    sprite(robot[1])

  • Posts: 75

    Thanks so much @yojimbo2000

  • Posts: 75

    I tried it but it doesn't work.

  • dave1707dave1707 Mod
    Posts: 7,528

    @majied Try something like this.

    function setup()
        robot={"Planet Cute:Character Boy",
                "Planet Cute:Character Cat Girl",
                "Planet Cute:Character Horn Girl"}
    end
    
    function draw()
        background(40, 40, 50)
        sprite(robot[1],WIDTH/2,HEIGHT/2)
    end
    
  • Posts: 75

    It works when I use it in a separate project but it doesn't work in my own?

  • Posts: 75

    Oh thanks @dave1707 I fixed it it was an unassigned string .
    For some reason Codea marks red the line below the incorrect line.

  • dave1707dave1707 Mod
    Posts: 7,528

    Without seeing any code, we can't say what's wrong.

  • Posts: 75

    @dave1707 How can I do this so it shows all of the sprites

  • dave1707dave1707 Mod
    Posts: 7,528

    @majied If you have the sprites in a table, then you need to iterate thru the table to show each Sprite. Without knowing what you're doing, I can't tell you more.

  • dave1707dave1707 Mod
    Posts: 7,528

    @majied Here's the above code showing how to go thru the table to display each Sprite.

    function setup()
        robot={"Planet Cute:Character Boy",
                "Planet Cute:Character Cat Girl",
                "Planet Cute:Character Horn Girl"}
    end
    
    function draw()
        background(40, 40, 50)
        for z=1,#robot do
            sprite(robot[z],WIDTH/2,HEIGHT-200*z)
        end
    end
    
  • Posts: 75

    How Can I make it so that when I touch the screen it creates a Sprite where I touched it I can use table.insert but I can't save the X and y positions

  • dave1707dave1707 Mod
    Posts: 7,528

    @majied Do you want a Sprite to show at every x,y position that you touch. Give me more info of what you want to do.

  • Posts: 75

    Yeah exactly

  • dave1707dave1707 Mod
    edited March 2016 Posts: 7,528

    @majied Try this. Tap the screen where you want a Sprite.

    function setup()
        tab={}
    end
    
    function draw()
        background(40, 40, 50)
        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
            table.insert(tab,vec2(t.x,t.y))
        end
    end
    
  • Posts: 75

    It works but i can't display it with the z From the code before

  • Posts: 75

    Can you merge them into one script because i don't know how to do this

  • dave1707dave1707 Mod
    Posts: 7,528

    @majied The value of z was determining which Sprite from the table you were going to display. The other code was just displaying a Sprite at the x,y position you touched. How do you want to control which Sprite from the table you want to display.

  • dave1707dave1707 Mod
    Posts: 7,528

    Here's an example of incrementing the robot table position each time the screen is touched.

    displayMode(FULLSCREEN)
    
    function setup()
        z=1
        tab={}
        robot={"Planet Cute:Character Boy",
                "Planet Cute:Character Cat Girl",
                "Planet Cute:Character Horn Girl",
                "Planet Cute:Character Pink Girl",
                "Planet Cute:Character Princess Girl"}
    end
    
    function draw()
        background(40, 40, 50)
        for a,b in pairs(tab) do
            sprite(robot[b.z],b.x,b.y)
        end
    end
    
    function touched(t)
        if t.state==BEGAN then
            table.insert(tab,vec3(t.x,t.y,z))
            z=z+1
            if z>#robot then
                z=1
            end
        end
    end
    
  • Posts: 75

    It works in a seperate project but not this one. Is there any problems?

    function setup()
        tbs = 0
        tboc= "Hover Cursor Over Bar To Open The Toolbox."
        selectedmaterial = 1
        tab={}
        robot={"Planet Cute:Character Boy",
                "Planet Cute:Character Cat Girl",
                "Planet Cute:Character Horn Girl",
                "Planet Cute:Character Pink Girl",
                "Planet Cute:Character Princess Girl"}
        recources = {"Wood","Metal","Tyre","Mounted Gun","Grenade Launcher","Blade"}
        activeblock =  {"Documents:wood","Documents:Metal","Documents:tyre","Documents:gun","Documents:launcher","Documents:chainsaw"}
        blocksused = 0
        mx = WIDTH/2
        my = HEIGHT/2
        recourtexttransparent = 150
        z = 1
    end
    
    function draw()
        if selectedmaterial > 6 then
            selectedmaterial = 1
        end
        mx = CurrentTouch.x
        my = CurrentTouch.y
        background(255, 255, 255, 255)
        fill(0, 169, 255, 255)
        rect(-10,710,1100,100)
        font("GillSans")
        fill(0, 0, 0, 255)
        text(tboc,WIDTH/2,726)
        fill(0, 0, 0,recourtexttransparent)
        font("HelveticaNeue-CondensedBold")
        text(recources[selectedmaterial],WIDTH/2,100)
        fill(0, 0, 0, 255)
        textSize(100)
        sprite("Documents:Undo",30,30,50,50)
        if tbs == 1 then
            sprite("Cargo Bot:Toolbox",130,600)
            sprite(activeblock[selectedmaterial],130,600)
            sprite("Documents:next",130,480,80,60)
        end
        if CurrentTouch.y > 710 then
            tbs = 1
            tboc = "Shake left to right to close Toolbox."
        end
        if UserAcceleration.x > 1 then
            tbs = 0
            tboc = "Hover Cursor Over Bar To Open The Toolbox."
            selectedmaterial = 1
        end
         for a,b in pairs(tab) do
            sprite(robot[b.z],b.x,b.y)
        end
        sprite("Documents:Cursor",mx,my,20,30)
        if recourtexttransparent > 0 then
            recourtexttransparent = recourtexttransparent - 1
        end
    end
    function touched(t)
        -- Undo
        if CurrentTouch.state == ENDED then
            if CurrentTouch.x < 50 and CurrentTouch.y < 50 then
                if blocksused > 0 then
                    blocksused = blocksused - 1
                    print("Undo")
                    table.remove(robot,j)
                else
                    print("Can not undo")
            end
        end
        -- Next Object
            if CurrentTouch.y > 450 and CurrentTouch.y < 510 and CurrentTouch.x > 90 and CurrentTouch.x < 170 then
                selectedmaterial = selectedmaterial + 1
                recourtexttransparent = 255
            end
            -- Place Object
         if t.state==BEGAN then
            table.insert(tab,vec3(t.x,t.y,z))
            z=z+1
            if z>#robot then
                z=1
            end
        end
    end
    end
    
  • dave1707dave1707 Mod
    Posts: 7,528

    @majied Check your touched function. The end statements aren't matched up with the if statements. Everything is in the ENDED if statement, so the BEGAN if statement will never execute.

  • Posts: 75

    Oh thank you ! :smiley:

Sign In or Register to comment.