So I am putzing through the code for JungleScene (a program that came with the Corona download) trying to figure out how it works. I am doing OK until I hit the tree[1].dx = .1. I tried my usual solution of looking in the API but I cannot find it! Is it there? I figure it has something to do with delta x and if I comment just one of the dx statements out the whole scrolling scenery does not scroll. I wish Corona had put just a few more comments in these demos for us poor novices. [import]uid: 23256 topic_id: 21964 reply_id: 321964[/import]
Without looking at the code, it is probably a delta X. It’s probably setting the speed, i.e. adding 0.1 pixels per frame to do the scrolling, in other words about 1 pixel every 1/3 second (at 30 frames per second).
There is probably an Runtime:addEventListener(“enterFrame”, someFunction) and in someFunction() I’m guessing you will see something like:
tree[1].x = tree[1].x + tree[1].dx
or some such.
EDIT: Wow good guess on my part… I just went to look at that code and there is a function called “move()” that is run by an enterFrame listener and it contains this line of code:
tree[i].x = tree[i].x - tree[i].dx * tDelta * 0.2
tDelta is the time between frame runs which should be 1/30th of a second (33.33 milliseconds), after multiplying by 0.2, you end up with a value of 6.667. Since tree[1] has dx value of 0.1, then your value is now 0.667, so each frame, tree[1].x will be decremented by 0.6 pixels.
As a programmer, I would like to think I document my code well. I use comments to explain problem areas, and hopefully my variable names are self explanatory. Since you can add anything to a Lua table, it’s tough to have to comment everything. It becomes important to learn to read code and figure out what it is doing.
[import]uid: 19626 topic_id: 21964 reply_id: 87303[/import]
I can see how the .dx is being used but is the .dx being defined somewhere or is the tree[1].dx = .1 defining it? I guess I am just asking can we just create a .thing when needed? [import]uid: 23256 topic_id: 21964 reply_id: 87310[/import]
Yes, you can create .thing when you need it. In this case tree[1].dx = .1 is defining the dx.
Lua tables are two purposed: one is an array. In this case tree[1], tree[2], tree[3] etc. are cells in a Lua table called “tree”. On line 22 there is a line:
local tree = {}
which creates an empty table. If you access the table with numbers, i.e. trees[1], trees[i], then it acts like an array.
If you access a table with words, you are in a key-value mode, similar to an associative array in PHP.
Thus:
local myTable = {}
table[“dx”] = 0.1
and
table.dx = 0.1
are equivalent, and you are free to add whatever key-value pairs you want. Those can values can be strings, numbers, other tables or even functions.
You can do myTable.doSomething = function() do something end and then be able to:
myTable.doSomething()
In your tree example, tree[1] is getting a table added to it by this line:
tree[1] = display.newImage( “Palm-arecaceae.png” )
Display.newImage returns a table and it has all of the display.newImage properties and methods, like, x, y, removeSelf() etc. You of course can add your own goodies to this like dx. [import]uid: 19626 topic_id: 21964 reply_id: 87322[/import]
Cool. Now it makes sense. The table[“dx”] = .1 was familiar, the table.dx was new but once explained is obvious.
Thanks for the help. I am teaching Corona to a high school Programming II class and I am about two days ahead of them in the book I am using (except for the one uber-geek kid that is running me over, smart kids are such a pain). I am having to rewrite some of the book to bring it down to the high school level (and my level) so help like this is greatly appreciated. [import]uid: 23256 topic_id: 21964 reply_id: 87330[/import]