Using "this" in a function rather than a variable name. Also help with game logic

I am not sure how to approach this with Corona, I am trying to make circles fall from the top of the screen and then let users click on them to make them disappear.

The way I would roughly do it in a browser game would be to loop through a block of code that spawns a circle then give it a class name, so every circle has the same class name, then I would create a touch listener that would interact with that class, then use “this” to only allow it to effect the circle that was touched.

If this possible with corona? Can I use “this” instead of a variable name? 

Also is object:setFillColor() a Graphics 2.0 thing only now? I made some stuff a while back and remember it as being a non graphics 2.0 thing but I could be wrong.

@sebastian,

There is not ‘this’ in Lua, but there is a variable referred to as ‘self’.

Please examine this code to see how it can be used.

-- 1. Explicit 'self' local function ex1() -- a. Create Table local aTable = {} aTable.myName = "bob" -- b. Attach a function with explicit 'self' aTable.doit = function( self ) print(self.myName) end -- Call the function and explicitly pass table reference aTable.doit( aTable ) -- Call the function and automatically pass table reference aTable:doit( ) end -- 2. Implicit 'self' local function ex2() -- a. Create Table local aTable = {} aTable.myName = "bill" -- b. Attach a method with implicit 'self' function aTable:doit() print(self.myName) end -- Call the function and explicitly pass table reference aTable.doit( aTable ) -- Call the function and automatically pass table reference aTable:doit( ) end -- 3. Variation on #1, but use 'me' instead of 'self' local function ex3() -- a. Create Table local aTable = {} aTable.myName = "buddy" -- b. Attach a function with explicit 'self' aTable.doit = function( me ) print(me.myName) end -- Call the function and explicitly pass table reference aTable.doit( aTable ) -- Call the function and automatically pass table reference aTable:doit( ) end

Now run the examples:

ex1() ex2() ex3()

You will see this in the console:

bob bob bill bill buddy buddy

Pay particular attention to the use of dot versus colon notation in my code samples.

Forums Etiquette Suggestion.  

One question per post or you’ll end up getting a thread with only some answers.  

That said here is the answer to your second question:

obj:setFillColor( r, g, b, a ) 

has been around since the start.  The difference is:

  • Graphics 1.0 - r/g/b/a used values between 0 and 255
  • Graphics 2.0 - r/g/b/a uses values between 0 and 1.0

@sebastian,

There is not ‘this’ in Lua, but there is a variable referred to as ‘self’.

Please examine this code to see how it can be used.

-- 1. Explicit 'self' local function ex1() -- a. Create Table local aTable = {} aTable.myName = "bob" -- b. Attach a function with explicit 'self' aTable.doit = function( self ) print(self.myName) end -- Call the function and explicitly pass table reference aTable.doit( aTable ) -- Call the function and automatically pass table reference aTable:doit( ) end -- 2. Implicit 'self' local function ex2() -- a. Create Table local aTable = {} aTable.myName = "bill" -- b. Attach a method with implicit 'self' function aTable:doit() print(self.myName) end -- Call the function and explicitly pass table reference aTable.doit( aTable ) -- Call the function and automatically pass table reference aTable:doit( ) end -- 3. Variation on #1, but use 'me' instead of 'self' local function ex3() -- a. Create Table local aTable = {} aTable.myName = "buddy" -- b. Attach a function with explicit 'self' aTable.doit = function( me ) print(me.myName) end -- Call the function and explicitly pass table reference aTable.doit( aTable ) -- Call the function and automatically pass table reference aTable:doit( ) end

Now run the examples:

ex1() ex2() ex3()

You will see this in the console:

bob bob bill bill buddy buddy

Pay particular attention to the use of dot versus colon notation in my code samples.

Forums Etiquette Suggestion.  

One question per post or you’ll end up getting a thread with only some answers.  

That said here is the answer to your second question:

obj:setFillColor( r, g, b, a ) 

has been around since the start.  The difference is:

  • Graphics 1.0 - r/g/b/a used values between 0 and 255
  • Graphics 2.0 - r/g/b/a uses values between 0 and 1.0