sceneGroup image referencing

I am creating a game with composer scenes, using the scene template.

I am putting images into the sceneGroup (self.view) group when they are first created.

My question is what is the best way (best practice) to reference a specific image after it has been added to sceneGroup?

For example, if I create a player class with a player image that gets added to sceneGroup, and I wanted to manipulate the image through the class.

ex. size, position, sprite sequence.

Currently the solution I have come up with is to save the image’s position in the group to a variable right after it is added to the group, and reference it by its group number.

local player = {} local player\_mt = { \_\_index = player }   player["new"] = function(group, name)     local newPlayer = {         name = name,                 image = display.newImageRect(group, "img/player.png", 32, 32),                 image = nil,                 gid = group.numChildren     }     return setmetatable( newPlayer, player\_mt ) end function player:test(group, px, py)         group[self["gid"]].x = px         group[self["gid"]].y = py         print(self["name"])         print(group[self["gid"]].x)         print(group[self["gid"]].y) end   return player

Hi @chris216,

Personally, I find it easier to just set/get that reference as a Composer variable, so you can access it from scene to scene if necessary:

http://docs.coronalabs.com/api/library/composer/setVariable.html

http://docs.coronalabs.com/api/library/composer/getVariable.html

As for your code example, I’m not sure why you’re using metatables. Are you experienced with Lua and this type of higher-level implementation? Metatables are not a common thing to deal with so I’m curious why you’re using them.

Take care,

Brent

Okay. I’ll try that. Thank you.

The code example is based on a tutorial for creating external module classes.

https://coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/

I used this method in my last project for external modules that needed to create a new instance of an object. ex. player, collectable, enemy.

I never use metatables for anything else, though.

Is it okay to do it this way? Can I just return a normal table and get the same effect?

Hi @chris216,

Yeah, I would not follow that old tutorial. Here is a (slightly) newer tutorial on external modules:

https://coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/

This method is pretty much the standard way that most developers implement external modules and call functions from within those modules, and it doesn’t bother with metatables and such.

Take care,

Brent

Okay. Thanks again.

Hi @chris216,

Personally, I find it easier to just set/get that reference as a Composer variable, so you can access it from scene to scene if necessary:

http://docs.coronalabs.com/api/library/composer/setVariable.html

http://docs.coronalabs.com/api/library/composer/getVariable.html

As for your code example, I’m not sure why you’re using metatables. Are you experienced with Lua and this type of higher-level implementation? Metatables are not a common thing to deal with so I’m curious why you’re using them.

Take care,

Brent

Okay. I’ll try that. Thank you.

The code example is based on a tutorial for creating external module classes.

https://coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/

I used this method in my last project for external modules that needed to create a new instance of an object. ex. player, collectable, enemy.

I never use metatables for anything else, though.

Is it okay to do it this way? Can I just return a normal table and get the same effect?

Hi @chris216,

Yeah, I would not follow that old tutorial. Here is a (slightly) newer tutorial on external modules:

https://coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/

This method is pretty much the standard way that most developers implement external modules and call functions from within those modules, and it doesn’t bother with metatables and such.

Take care,

Brent

Okay. Thanks again.