Simple "How-To" question / FRUSTRATED

Folks:

I have been programming in various languages for over 25 years…yet I’m a newbie to Lua…so far I love Corona, love LUA, but some things are driving me mad…after endless hours of trying everything I could think of, I am stuck with the following problem:

I have all of the game logic and board graphics generation in a single function, lets call it ‘function A’ Inside of it, there are multiple other “child” functions. Everything works fine. Inside this function I have an object, called ‘ball’ that simply falls due to gravity (using physics).

What I am trying to do is to check if the ‘ball’ goes outside the screen limits (actually off the screen view) and reset everything.

I have tried placing this logic into Runtime onframe event - that doesn’t work because it doesn’t see ‘ball’ object.
I have tried numerous other things and nothing works.
How can I continuously check the coordinates of the ‘ball’ object? Can the 'ball object have its own ‘onframe’ event? Are there any continuous events that fire so that I can check the state of objects? What events can be assigned to the ‘ball’ so that this off-limits condition can be checked? I would think this would be such a common problem.

Lua is nice, but so far has been very unforgiving…please help…really frustrated.

Thanks,

Oleg
[import]uid: 92927 topic_id: 18389 reply_id: 318389[/import]

Hey Oleg,

The Runtime listener should work fine provided you put it inside the function that is spawning the ball - or if the ball isn’t local. (But that second one isn’t the best option.)

If you post some plug and play code or a downloadable project file I’m sure someone would be happy to check it out and offer further, more specific guidance.

Peach :slight_smile: [import]uid: 52491 topic_id: 18389 reply_id: 70512[/import]

without seeing any of your code I’d suggest you use LUA tables to keep track of your objects. The idea is simple:

  
local objTable = {} -- create a table for your objects  
  
local function createBall ()  
  
 -- create a local table  
 local ball = {}  
  
 -- create the ball object  
 ball.object = display.newRect(10,10,4,4)   
  
 -- optionally (!) add local variables you may need:  
 ball.someHandler = "green"   
 ball.isBouncy = true  
  
 -- insert the new object to your objects table  
 table.insert(objTable, ball)  
  
end  
-- now you can iterate through the objTable  
function checkObjects ()  
   
 for i,val in pairs(objTable) do  
  
 -- remove objects that are out of bound  
 if val.object.y \> 320 or val.object.x \> 480 or val.object.x \< 0 or val.object.y \< 0then   
 val.object:removeSelf();  
 objTable [i] = nil  
 end   
  
 end  
  
end  
  
-- run the enterFrame listener  
Runtime:addEventListener( "enterFrame", checkObjects )  
  

Well, this is one of many approaches :slight_smile:
hope it helps

-finefin [import]uid: 70635 topic_id: 18389 reply_id: 70556[/import]

a simple

[lua]local ball

function inside ()
ball = display.newImage…
end

function checkObjects ()
if ball.x < 0 then…
end

– run the enterFrame listener
Runtime:addEventListener( “enterFrame”, checkObjects )[/lua]

outside and over the function and then assigning the ball object to it should do the trick. No need to work with tables here. [import]uid: 5712 topic_id: 18389 reply_id: 70636[/import]

Guys:

Thanks very much for the suggestions.

I will start from the bottom up:

The last comment from Mike Hart (thanks for reply) I was told NOT to do so as to not have global vars around. Is that still true? Even though you have “local ball”, you have made it global to the entire module? Isn’t that a no-no practice in Lua land?

Canupa, thanks for the suggestion. I really like your idea of keeping stuff in a separate table, but is that essentially a “trick” to bring out the local ball reference to the top? Would you advise this for all the objects?

Peach, thanks for reply as well. If I understand correctly, are you saying I can have a Runtime:addEventListener( “enterFrame”, somefunction ) be LOCAL to my ‘function A’ in the description? How would that look in the code? A bit confused…can u explain?

Again, thanks to all, still trying to find the best (cleanest) way out.

Oleg
[import]uid: 92927 topic_id: 18389 reply_id: 70684[/import]

I know it sounds funny to be reading a WoW Wiki but they actually have a very very nice Lua Wiki setup. I’d recommend reading http://www.wowwiki.com/Lua_variable_scoping

Now in Lua there are a two kinds of scopes (global and local) and each of those have a bunch of other sub types. For Mikes example it is a file local variable (as declared with the “local …”, if you left off the word local it would become a global) not a global.

What you are thinking about is the normal function local variables (private, public functions with variables declared in each and so forth). This is one of 3 ways of doing it in Lua (the other being block local).

So the answer would be no. [import]uid: 46343 topic_id: 18389 reply_id: 70704[/import]

Really, seeing some plug and play code would help me answer the question directed at me.

Mike’s suggestion is solid, so is firefin’s - really you could take your pick.

Peach :slight_smile: [import]uid: 52491 topic_id: 18389 reply_id: 70719[/import]

let me add that my suggestion is especially helpful when dealing with a lot of objects (like particles).

-finefin [import]uid: 70635 topic_id: 18389 reply_id: 70769[/import]

Everyone, thanks once again for replying…

dubcanada thanks very much for that link, that was incredibly helpful !!

I am leaning towards MikeHart’s response since it is simpler (although I really like canupa’s approach for more serious stuff). The only question I have is that currently in my massive ‘function A’ I have a clean-up function that removes all objects that I don’t use. In there, I do set ‘ball = nil’

Does that mean I can’t do that anymore since the ‘local ball’ is now a ‘function local’ and if I do ball=nil I would effectively remove this variable from existence? How do you clear the variables without removing them if I use Mike’s method?

Thanks again,

Oleg
[import]uid: 92927 topic_id: 18389 reply_id: 70921[/import]