How do i create a parent object? I need two (or more) objects to have the came code, with out having to keep rewriting the same code for each one. Is there a way to do this with Corona SDK? [import]uid: 208120 topic_id: 34730 reply_id: 334730[/import]
In Lua, all objects are saved as reference to memory. That means when you copy an object, you are simply copying its reference memory, not the code itself:
local object1 = { } --may be table "0x234a4b0" in memory
local object2 = object1 --object2 also now references table "0x234a4b0"
To re-create (i.e. actually copy) an object, you have to copy all of it’s key-value pairs:
for k,v in pairs(object1) do object2[k] = v end
Not sure what you are trying to do though by creating a “parent” object… here’s what I think you mean(?)
[code]
local parent = { }
local object1 = { }
local object2 = object1
parent.obj1 = object1 --table 0x34ab9f
parent.obj2 = object2 --table 0x34ab9f
[/code] [import]uid: 36792 topic_id: 34730 reply_id: 138022[/import]
What I want is:
if objectA is programmed to move when clicked and dragged. Then, I want objectB to do the exact same thing without having to write a separate code for objectB.
Is there a way to “Pair” the two objects together?
I tried what you wrote, but it didn’t work. Not sure if I just wrote it wrong or something though. [import]uid: 208120 topic_id: 34730 reply_id: 138175[/import]
@Wooderson, if you’re using physics you can use some kind of joint to move them together. If not, in the code to move object A, you have to move object B with it. The other way is to use an enterFrame listener on the Runtime to check every 1/30th or 1/60th of a second (depending on your frame rate) if B isn’t a set distance from a to move it. [import]uid: 199310 topic_id: 34730 reply_id: 138208[/import]
Hey Rob Miracle,
Do you have a sample code that you could show me? Im still new to coding and corona sdk. [import]uid: 208120 topic_id: 34730 reply_id: 138236[/import]
It’s a tough question to answer because it involves questions about how you structure your code. My advice is aim to become familiar with OOP. A lot of Corona users try to patch things together with spaghetti code, because a lot of them are also new to programming. I wrote this 4-part tutorial specifically on OOP and how to structure your code. People seem to think it’s well written. hope it helps!
Part 1: http://www.ardentkid.com/blog/2012-11-22/class-structure-in-corona
Part 2: http://www.ardentkid.com/blog/2012-11-27/non-static-classes-func-inheritance-oo-lua-24
Part 3: http://www.ardentkid.com/blog/2012-12-05/custom-events-oo-lua-34
Part 4: http://www.ardentkid.com/blog/2012-12-24/inheritance-oo-lua-44 [import]uid: 36792 topic_id: 34730 reply_id: 138240[/import]
Thanks ArdentKid ill give this a read. [import]uid: 208120 topic_id: 34730 reply_id: 138244[/import]
As I understand, you can write something like:
Create a module of your object type
--let your module name like as your file name
module("objectType",package.seeall)
local object = {}
object.newObject = function()
local abc = display.newGroup()
--do everything if you want
--may be add touch to abc and make it move as you touch
return abc -- return it
end
return object
And everytime you need to create an object of this object type:
-- in other code file
local objectType = require "objectType"
local objectA = objectType.newObject()
local objectB = objectType.newObject()
--now you have two object with same code
--every code you write for "abc" group, objectA and objectB have it
It seem like OOP, but more simple. If you want to do more thing, read ArdentKid’s OOP [import]uid: 9190 topic_id: 34730 reply_id: 138250[/import]
In Lua, all objects are saved as reference to memory. That means when you copy an object, you are simply copying its reference memory, not the code itself:
local object1 = { } --may be table "0x234a4b0" in memory
local object2 = object1 --object2 also now references table "0x234a4b0"
To re-create (i.e. actually copy) an object, you have to copy all of it’s key-value pairs:
for k,v in pairs(object1) do object2[k] = v end
Not sure what you are trying to do though by creating a “parent” object… here’s what I think you mean(?)
[code]
local parent = { }
local object1 = { }
local object2 = object1
parent.obj1 = object1 --table 0x34ab9f
parent.obj2 = object2 --table 0x34ab9f
[/code] [import]uid: 36792 topic_id: 34730 reply_id: 138022[/import]
What I want is:
if objectA is programmed to move when clicked and dragged. Then, I want objectB to do the exact same thing without having to write a separate code for objectB.
Is there a way to “Pair” the two objects together?
I tried what you wrote, but it didn’t work. Not sure if I just wrote it wrong or something though. [import]uid: 208120 topic_id: 34730 reply_id: 138175[/import]
@Wooderson, if you’re using physics you can use some kind of joint to move them together. If not, in the code to move object A, you have to move object B with it. The other way is to use an enterFrame listener on the Runtime to check every 1/30th or 1/60th of a second (depending on your frame rate) if B isn’t a set distance from a to move it. [import]uid: 199310 topic_id: 34730 reply_id: 138208[/import]
Hey Rob Miracle,
Do you have a sample code that you could show me? Im still new to coding and corona sdk. [import]uid: 208120 topic_id: 34730 reply_id: 138236[/import]
It’s a tough question to answer because it involves questions about how you structure your code. My advice is aim to become familiar with OOP. A lot of Corona users try to patch things together with spaghetti code, because a lot of them are also new to programming. I wrote this 4-part tutorial specifically on OOP and how to structure your code. People seem to think it’s well written. hope it helps!
Part 1: http://www.ardentkid.com/blog/2012-11-22/class-structure-in-corona
Part 2: http://www.ardentkid.com/blog/2012-11-27/non-static-classes-func-inheritance-oo-lua-24
Part 3: http://www.ardentkid.com/blog/2012-12-05/custom-events-oo-lua-34
Part 4: http://www.ardentkid.com/blog/2012-12-24/inheritance-oo-lua-44 [import]uid: 36792 topic_id: 34730 reply_id: 138240[/import]
Thanks ArdentKid ill give this a read. [import]uid: 208120 topic_id: 34730 reply_id: 138244[/import]
As I understand, you can write something like:
Create a module of your object type
--let your module name like as your file name
module("objectType",package.seeall)
local object = {}
object.newObject = function()
local abc = display.newGroup()
--do everything if you want
--may be add touch to abc and make it move as you touch
return abc -- return it
end
return object
And everytime you need to create an object of this object type:
-- in other code file
local objectType = require "objectType"
local objectA = objectType.newObject()
local objectB = objectType.newObject()
--now you have two object with same code
--every code you write for "abc" group, objectA and objectB have it
It seem like OOP, but more simple. If you want to do more thing, read ArdentKid’s OOP [import]uid: 9190 topic_id: 34730 reply_id: 138250[/import]