Need to understand this sample corona

I am 16 and started coding now only (inspiration = steve jobs) so i came to corona for help and i learn lil bit of codes like local background…function onTouch…etc but then i came to know that corona already have some samples game so i open many but found a app with little coding whos name is chains and here is the coding:-

local centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight

local physics = require(“physics”)
physics.start()

physics.setScale( 60 )

display.setStatusBar( display.HiddenStatusBar )

– The final “true” parameter overrides Corona’s auto-scaling of large images
local background = display.newImage( “grille_bkg.png”, centerX, centerY, true )

local ground = display.newImage( “ground.png”, centerX, 450, true )
physics.addBody( ground, “static”, { friction=0.5 } )

local beam1 = display.newImage( “beam.png” )
beam1.x = 20; beam1.y = 350; beam1.rotation = -40
physics.addBody( beam1, “static”, { friction=0.5 } )

local beam2 = display.newImage( “beam.png” )
beam2.x = 410; beam2.y = 340; beam2.rotation = 20
physics.addBody( beam2, “static”, { friction=0.5 } )

local beam3 = display.newImage( “beam_long.png” )
beam3.x = 280; beam3.y = 50
physics.addBody( beam3, “static”, { friction=0.5 } )

local myJoints = {}

for i = 1,5 do
    local link = {}
    for j = 1,17 do
        link[j] = display.newImage( “link.png” )
        link[j].x = 121 + (i*34)
        link[j].y = 55 + (j*17)
        physics.addBody( link[j], { density=2.0, friction=0, bounce=0 } )
        
        – Create joints between links
        if (j > 1) then
            prevLink = link[j-1] – each link is joined with the one above it
        else
            prevLink = beam3 – top link is joined to overhanging beam
        end
        myJoints[#myJoints + 1] = physics.newJoint( “pivot”, prevLink, link[j], 121 + (i*34), 46 + (j*17) )

    end
end

local randomBall = function()
    ball = display.newImage( “super_ball.png” )
    ball.x = 10 + math.random( 60 ); ball.y = -20
    physics.addBody( ball, { density=2.9, friction=0.5, bounce=0.7, radius=24 } )
end

– Call the above function 12 times
timer.performWithDelay( 1500, randomBall, 12 )

this is the sample code aND I want to understand it therefore i read it so somehow i understood till

local beam3 = display.newImage( “beam_long.png” )
beam3.x = 280; beam3.y = 50
physics.addBody( beam3, “static”, { friction=0.5 } )

but after that nothing so please anyone help me to understand and dont put silly links below…tell me please!!! :slight_smile:

First, you can stop posting this “I am 16 and started coding now only (inspiration = steve jobs)” in each of your posts…  That bit of social engineering isn’t going to get your better answers.

Second, it sounds like you’re looking for a mentor, so go post in the jobs forum and be straight up about it.  However, don’t expect mentorship to be free. 

Third, there are a lot of folks here (not just the staff who are also awesome) who want to help with serious questions, but you have to meet us half way.  Take the time to post a clear and concise question about a specific topic, and please format any code posts.  Remember, time is money and those of us who answer, could be spending time answering making money.  

formatyourcode.jpg

PS - About links and such.  Learning isn’t free, so if you think you can avoid reading and following up on links, you are wasting your time.  

Here is what you should do if you want to start understanding that bit of code.

  1. Read it line by line.

  2. Go to this page and find every piece of syntax line by line and read about what it means: https://docs.coronalabs.com/daily/api/

Ex: Line 1…

local centerX = display.contentCenterX

Search for contentCenterX on the page I linked above.

That will lead to this page: https://docs.coronalabs.com/daily/api/library/display/contentCenterX.html

Read it and understand it. 

Move on to next line.

Thanx well i dont know before how to put codes and please don’t say to self search please if i am in confusion then help me!..don’t act like you can not do anything!!!

If you don’t know how to write code, that is completely understandable, but you need to get a mentor or start on something simpler, like learning Lua.  

You simply want to run before you’ve even mastered the basics.  I have seen this before and it always ends in frustration.  I don’t want you to be frustrated.  I’m always happy to see new people interested in game development.  Unfortunately, today (new) people see this as something simple and easy, when it is not.  At least, not if you’ve never done any programming or game development at all.

So, It isn’t that I don’t want to help, but rather that you ask very broad questions and don’t realize that to answer them is a massive amount of work. I (and anyone else) has to:

  • know your current level of understanding,
  • put ourselves in your shoes,
  • answer your question while also explaining all of the other fundamental things that build up to the answer.

In closing, I would suggest you first focus entirely on learning Lua (as boring as you may find that).  Then, start making simple toy examples playing with display objects and physics.  Don’t even think about making a game till you are solid on Lua, have some basic experience with Corona, and have become accustomed to digging through the API docs and Guides which have a lot of info.

In the future when you ask a concise question on a very specific topic I will be happy to answer it.

To continue with Ed’s thought. We want you to be successful. And we have years of experience doing that. It doesn’t happen over night. It takes time to learn how to read a documentation page and understand what it’s saying. It takes learning how to properly post code (Ed provided an example of that above). While Corona is one of the easiest ways to build games and apps, it still takes work and learning at your pace. You have to crawl before you walk. You have to walk before you run. That’s true with programming. It’s a learning journey.

So start with simple baby steps. A great place to start is our getting started guide:

https://docs.coronalabs.com/guide/programming/index.html

This breaks down using Corona into baby steps and helps you understand various parts of the code.

Rob

YES, i want to be successful and you said that so it feels happy to me… Thanx for that!

I can explain this:

local randomBall = function() ball = display.newImage( "super\_ball.png" ) ball.x = 10 + math.random( 60 ); ball.y = -20 physics.addBody( ball, { density=2.9, friction=0.5, bounce=0.7, radius=24 } ) end -- Call the above function 12 times timer.performWithDelay( 1500, randomBall, 12 )

randomBall() create a ball image at an x location equal to 10 + a randomly chosen number between 0 and 60 and a y coordinate of -20 (positioning it off screen), physics.addBody adds a body to the ball with the following options:

Giving it a density, friction, bounce, and radius(being used because the object will be round)

timer.performWithDelay will call randomBall every 1500 milliseconds for 12 times.

As for this:

local myJoints = {} for i = 1,5 do local link = {} for j = 1,17 do link[j] = display.newImage( "link.png" ) link[j].x = 121 + (i\*34) link[j].y = 55 + (j\*17) physics.addBody( link[j], { density=2.0, friction=0, bounce=0 } ) -- Create joints between links if (j \> 1) then prevLink = link[j-1] -- each link is joined with the one above it else prevLink = beam3 -- top link is joined to overhanging beam end myJoints[#myJoints + 1] = physics.newJoint( "pivot", prevLink, link[j], 121 + (i\*34), 46 + (j\*17) ) end end

I have not started using these yet but you could read about it here:

https://docs.coronalabs.com/api/type/Joint/index.html

The last block of code is going to create 5 “chains”. Each chain is made up of 17 links where each link in the chain comes from an image file named link.png.

The first for loop creates the 5 chains. The inner “j” loop (j is the index variable) is responsible for creating each link in the chain. After the image is loaded and we’ve created a display object it’s positioned in it’s x, y location and a physics body is added.

The next 5 lines are determining what  each link is connected to. The first link is connected to the graphic at the top (the wood cross beam). This is the else clause of the if-then-else statement. If j is greater than 1 then you want to connect this link to the previous link and set a variable prevLink to hold the object that’s the previous “link”.

Finally create a physics.newJoint() that connects the two display objects using a method the physics engine knows how to treat like a link.

Rob

First, you can stop posting this “I am 16 and started coding now only (inspiration = steve jobs)” in each of your posts…  That bit of social engineering isn’t going to get your better answers.

Second, it sounds like you’re looking for a mentor, so go post in the jobs forum and be straight up about it.  However, don’t expect mentorship to be free. 

Third, there are a lot of folks here (not just the staff who are also awesome) who want to help with serious questions, but you have to meet us half way.  Take the time to post a clear and concise question about a specific topic, and please format any code posts.  Remember, time is money and those of us who answer, could be spending time answering making money.  

formatyourcode.jpg

PS - About links and such.  Learning isn’t free, so if you think you can avoid reading and following up on links, you are wasting your time.  

Here is what you should do if you want to start understanding that bit of code.

  1. Read it line by line.

  2. Go to this page and find every piece of syntax line by line and read about what it means: https://docs.coronalabs.com/daily/api/

Ex: Line 1…

local centerX = display.contentCenterX

Search for contentCenterX on the page I linked above.

That will lead to this page: https://docs.coronalabs.com/daily/api/library/display/contentCenterX.html

Read it and understand it. 

Move on to next line.

Thanx well i dont know before how to put codes and please don’t say to self search please if i am in confusion then help me!..don’t act like you can not do anything!!!

If you don’t know how to write code, that is completely understandable, but you need to get a mentor or start on something simpler, like learning Lua.  

You simply want to run before you’ve even mastered the basics.  I have seen this before and it always ends in frustration.  I don’t want you to be frustrated.  I’m always happy to see new people interested in game development.  Unfortunately, today (new) people see this as something simple and easy, when it is not.  At least, not if you’ve never done any programming or game development at all.

So, It isn’t that I don’t want to help, but rather that you ask very broad questions and don’t realize that to answer them is a massive amount of work. I (and anyone else) has to:

  • know your current level of understanding,
  • put ourselves in your shoes,
  • answer your question while also explaining all of the other fundamental things that build up to the answer.

In closing, I would suggest you first focus entirely on learning Lua (as boring as you may find that).  Then, start making simple toy examples playing with display objects and physics.  Don’t even think about making a game till you are solid on Lua, have some basic experience with Corona, and have become accustomed to digging through the API docs and Guides which have a lot of info.

In the future when you ask a concise question on a very specific topic I will be happy to answer it.

To continue with Ed’s thought. We want you to be successful. And we have years of experience doing that. It doesn’t happen over night. It takes time to learn how to read a documentation page and understand what it’s saying. It takes learning how to properly post code (Ed provided an example of that above). While Corona is one of the easiest ways to build games and apps, it still takes work and learning at your pace. You have to crawl before you walk. You have to walk before you run. That’s true with programming. It’s a learning journey.

So start with simple baby steps. A great place to start is our getting started guide:

https://docs.coronalabs.com/guide/programming/index.html

This breaks down using Corona into baby steps and helps you understand various parts of the code.

Rob

YES, i want to be successful and you said that so it feels happy to me… Thanx for that!

I can explain this:

local randomBall = function() ball = display.newImage( "super\_ball.png" ) ball.x = 10 + math.random( 60 ); ball.y = -20 physics.addBody( ball, { density=2.9, friction=0.5, bounce=0.7, radius=24 } ) end -- Call the above function 12 times timer.performWithDelay( 1500, randomBall, 12 )

randomBall() create a ball image at an x location equal to 10 + a randomly chosen number between 0 and 60 and a y coordinate of -20 (positioning it off screen), physics.addBody adds a body to the ball with the following options:

Giving it a density, friction, bounce, and radius(being used because the object will be round)

timer.performWithDelay will call randomBall every 1500 milliseconds for 12 times.

As for this:

local myJoints = {} for i = 1,5 do local link = {} for j = 1,17 do link[j] = display.newImage( "link.png" ) link[j].x = 121 + (i\*34) link[j].y = 55 + (j\*17) physics.addBody( link[j], { density=2.0, friction=0, bounce=0 } ) -- Create joints between links if (j \> 1) then prevLink = link[j-1] -- each link is joined with the one above it else prevLink = beam3 -- top link is joined to overhanging beam end myJoints[#myJoints + 1] = physics.newJoint( "pivot", prevLink, link[j], 121 + (i\*34), 46 + (j\*17) ) end end

I have not started using these yet but you could read about it here:

https://docs.coronalabs.com/api/type/Joint/index.html

The last block of code is going to create 5 “chains”. Each chain is made up of 17 links where each link in the chain comes from an image file named link.png.

The first for loop creates the 5 chains. The inner “j” loop (j is the index variable) is responsible for creating each link in the chain. After the image is loaded and we’ve created a display object it’s positioned in it’s x, y location and a physics body is added.

The next 5 lines are determining what  each link is connected to. The first link is connected to the graphic at the top (the wood cross beam). This is the else clause of the if-then-else statement. If j is greater than 1 then you want to connect this link to the previous link and set a variable prevLink to hold the object that’s the previous “link”.

Finally create a physics.newJoint() that connects the two display objects using a method the physics engine knows how to treat like a link.

Rob