Giving objects/tables a custom type

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 :)

views:1317 update:2011/12/30 9:10:41
corona forums © 2003-2011