Help on if -- then and tables

I’m trying to understand better the tables and the “if” – “then”

but all the codes I see they are too advanced for me.

I see a lot of this:---------------------------------

if self.j>1 then
        if (gemsTable[self.i][self.j-1]).isMarkedToDestroy == false then

            if (gemsTable[self.i][self.j-1]).gemType == self.gemType then

               markToDestroy( gemsTable[self.i][self.j-1] )
            end    
        end
    end

or this -------------------------------

for i = 1, 8, 1 do
        for j = 1, 8, 1 do
            
            if gemsTable[i][j].isMarkedToDestroy then

                isGemTouchEnabled = false
                transition.to( gemsTable[i][j], { time=300, alpha=0.2, xScale=2, yScale = 2, onComplete=enableGemTouch } )

                – update score
                score = score + 10
                scoreText.text = string.format( “SCORE: %6.0f”, score )
                scoreText:setReferencePoint(display.TopLeftReferencePoint)
                scoreText.x = 60
                
            end
        end
    end

But I don’t get it. All I see is a complete game code, and it’s already too much.

Is there any where I can study this “if” and “tables” but in an extremely beginners way?

like the simplest possible way, just 1 image 1 table 1 if

Please let me know where can I study this.

Victor

There are some video tutorials on this page:

http://www.coronalabs.com/resources/videos/

Scroll down to the Lua Bootcamp videos. There’s one about tables and the last one (Control) covers if/else – but it wouldn’t hurt if you’re a beginner to just do all the Lua videos.

Thanks. I already saw all the videos there, they are great!

in fact I’m planning to buy the videos you offer there for the frog.

I just need to finish something with my app, so I have time to watch the videos and learn.

meanwhile I have this code:


– main.lua

display.setStatusBar( display.HiddenStatusBar )

local bg = display.newImage (“bg.png”)
local cat = display.newImage (“cat.png”,80,10)
    cat:scale  (.5, .5)

local people = { “Jay”, “Wanda”, “Tom”, “Chelsea”}
local age = { “49”, “?”, “18”, “16”}

print ( people[2] )


Very simple, I “print” people [2]

but where do I see it? in real life? not just in the terminal?

All I see when I run the code, is a background and the cat.

– where do I see the results of the table?

– how do I use the table in real life examples?

I want to be able to onRelease a button, have a function that will display

random images , instead of “Jay”, “Wanda” and so on…

I want 10 images, so the function will choose one random image and show it.

Thanks for your help.

And I will but those videos I like it a lot.

Victor

print ( people[2] ) is only going to display the result on the terminal. You want to read up on display.newText() which puts text on device screen.  http://docs.coronalabs.com/api/library/display/newText.html

What Kerem said. 

And instead of the names of my family, I could have used the names of image files:

[lua]local pics = { “picture1.png”, “pictureB.png”, “pictureXX.png”, “picture.png” }[/lua]

Then using math.random(#pics) I can randomly grab one and use it inside display.newImage:

[lua]

local idx = math.random(#pics) – #pics equals number of elements in the table

local chosenPic = pics[idx] – pull out a random image file name

local obj = display.newImage(chosenPic) – and display it to the screen

[/lua]

That should get you going.

And as far as the course goes, a better one is coming out in the next couple weeks (not exactly sure of the time-frame). It’s published by Infinite Skills and it ended up better than I thought it would. :slight_smile:

 Jay

Thanks Jay. Should I wait for the new course? or buy both?

As far as the code I put this

local bg = display.newImage (“bg.png”)
local cat = display.newImage (“cat.png”,80,10)
    cat:scale  (.5, .5)
    
local pics = {“sun.png”, “cat.png”, “tree.png”}

local i = math.random(#pics)
local chosenPic = pics[i]
local obj = display.newImage(chosenPic)

And nothing happens!

what am I doing wrong?

Victor

what am I doing wrong?

 Have no idea. Are the sun.png. etc., pics in the project? Or are they down in a folder? If so, then you need to include that folder name in there some where.

 Jay

I try this

local bg = display.newImage (“bg.png”)
local cat = display.newImage (“cat.png”,80,10)
    cat:scale  (.5, .5)
   
local pics = {“sun.png”, “cat.png”, “tree.png”, “bat.png”, “bee.png”, “ladyBug.png”, “ant.png”}
local i = math.random(#pics)
local chosenPic = pics[i]
local obj = display.newImage(chosenPic)

for i = 1, #pics do
    local i = math.random(#pics)
    local chosenPic = pics[i]
    local obj = display.newImage(chosenPic)
end

And it works!

I put the 3 lines you told me, inside a for i = #pics do ------------- end

and it works I can see a random image everytime I run the code – GREAT!

now:

I see the sun, and then the cat, on top of the sun, then the tree on top of the ant…

How do I show one image at a time?

thanks

Victor

How do I show one image at a time?

You are showing one image one at a time. Just because your eyes don’t send the messages to your brain fast enough for you to distinguish that isn’t the fault of the code.

So you need to ask a different question.

 Jay

PS - Not trying to be difficult, your question just doesn’t mean anything that I can interperet.

PPS - Why are all three images being shown? What in your code makes that happen? If you don’t want to show three images, then don’t do the thing that causes three images to show.

Thanks for taking the time to make me think in a different way.

I just don’t know a lot, in fact, I’m very new at all this. I don’t ask the “correct” questions

because I don’t know how to ask, or what to ask. – I’m learning.

And thanks to people like you, that takes the time, to teach me something

that’s how I know a few things.

I kept thinking, and I think the loop runs 4 times, so the code shows me 1 picture, then 1 more, and another and the last one

but it goes so fast, that I just see the last one, and all the other ones underneath.

I think that’s what’s happening.

So – and forgive me if I’m not asking the correct question –

How do I make a code that will pick just one random image from a table with a lot of images, and display just one

new one every time?

I hope you understand what I’m trying to do.

Thanks

Victor

I’m afraid that though this is the ‘newbie’ forum, far too many people are expecting help with newbie programming questions, when this is really for newbie ‘corona sdk’ questions…

you are asking how to play bach’s 5th concerto before you even know the letters on the keys…you need to learn lua and basic programming before you can help yourself…

Hi sean84

Thanks for the advice.

Would you please  tell me:

1.- Where can I go to learn “the letters on the keys…”?

2.- If this is “only” for “‘corona sdk’ questions…”, could you give me an example of what kind of questions I can ask?

3.- “far too many people are expecting help with newbie programming questions,” Where do I learn basic programming?

Thank you for your time and advice.

I’m learning and I don’t know where to start or where to look for.

Victor

Google is really useful. Look, I type LUA and hit enter. It spits out a gazillion entries. The first one however points me to : 

The LUA Programming Language - http://www.lua.org/ … That’s your answer to comment #1.

You need to to search more and see if you can develop some skills. Internet really is amazing when it comes to picking up technical skills. You need to know what to look for thats it.

You might want to consider registering for a programming 101 type course at your local community college or something. 

Thanks I’m going to study that.

Just by any chance, and I’m sorry for asking.

Do you know what’s this error mean.

The application wrapper must end in .app /var/folders/kh is invalid

I got this in Application loader on Xcode, trying to summit my app

Any ideas?

Vctor

Sorry. No I don’t know that error. 

thanks

There are some video tutorials on this page:

http://www.coronalabs.com/resources/videos/

Scroll down to the Lua Bootcamp videos. There’s one about tables and the last one (Control) covers if/else – but it wouldn’t hurt if you’re a beginner to just do all the Lua videos.

Thanks. I already saw all the videos there, they are great!

in fact I’m planning to buy the videos you offer there for the frog.

I just need to finish something with my app, so I have time to watch the videos and learn.

meanwhile I have this code:


– main.lua

display.setStatusBar( display.HiddenStatusBar )

local bg = display.newImage (“bg.png”)
local cat = display.newImage (“cat.png”,80,10)
    cat:scale  (.5, .5)

local people = { “Jay”, “Wanda”, “Tom”, “Chelsea”}
local age = { “49”, “?”, “18”, “16”}

print ( people[2] )


Very simple, I “print” people [2]

but where do I see it? in real life? not just in the terminal?

All I see when I run the code, is a background and the cat.

– where do I see the results of the table?

– how do I use the table in real life examples?

I want to be able to onRelease a button, have a function that will display

random images , instead of “Jay”, “Wanda” and so on…

I want 10 images, so the function will choose one random image and show it.

Thanks for your help.

And I will but those videos I like it a lot.

Victor

print ( people[2] ) is only going to display the result on the terminal. You want to read up on display.newText() which puts text on device screen.  http://docs.coronalabs.com/api/library/display/newText.html

What Kerem said. 

And instead of the names of my family, I could have used the names of image files:

[lua]local pics = { “picture1.png”, “pictureB.png”, “pictureXX.png”, “picture.png” }[/lua]

Then using math.random(#pics) I can randomly grab one and use it inside display.newImage:

[lua]

local idx = math.random(#pics) – #pics equals number of elements in the table

local chosenPic = pics[idx] – pull out a random image file name

local obj = display.newImage(chosenPic) – and display it to the screen

[/lua]

That should get you going.

And as far as the course goes, a better one is coming out in the next couple weeks (not exactly sure of the time-frame). It’s published by Infinite Skills and it ended up better than I thought it would. :slight_smile:

 Jay