Is there an official way of typing objects/tables in Lua or Corona?
I was thinking of simply adding a "type" attribute to my display objects so I can identify what type of object was tapped. For example, was a player or enemy tapped.
Label: corona android iphone ipad building error inmobi problem display screen View image tableView failed object physics touch button game lua config event listener |
Is there an official way of typing objects/tables in Lua or Corona?
I was thinking of simply adding a "type" attribute to my display objects so I can identify what type of object was tapped. For example, was a player or enemy tapped.
See "Data Types" here; http://blog.anscamobile.com/2011/06/understanding-lua-tables-in-corona-sdk/
The whole tutorial is very useful and wonderfully written.
Peach :)
Thanks! It's an excellent tutorial but doesn't seem to answer my question. I wanted to know if there was some way to do type checking.
I wanted to distinguish my enemy display objects from my friendly display objects. That way, I could perform different actions depending on what the user taps on.
In Flash, I would do this by creating an Enemy class and linking it to a enemy symbol in my library. Likewise, I would create a Player class and link it to the player symbol. Then, if the user clicks on an enemy, I can check it's type with the is
keyword:
1 2 3 | if (clickedObject is Enemy) { ... } |
Ah OK, well you'd add your objects to a table and using the data types (from the link) you could check like this;
1 2 3 | if event.target.type == "bad" then -- do stuff end |
Thanks, that confirms it.
Excellent - best of luck with your project :)