Miken1 wrote:Can this be explained in an easier way? Having trouble to implement and understand it.
Thanks.
No problem.
The following is going to be a long and boring explanation of the logic given above.
For implementation purpose, you can still refer to my previous post.
Let us consider a not so different example, where you want to have different objects drawn at the same time.
Each object stand on its own, and is different from the others. Therefore, it should have its own attributes (x,y, width, height, etc.).
I will assume that an object is represented by a table. If I need 3 different objects,i'll have to create three tables.
But because all those objects are similar, they can share a certain template. So I can basically write a factory function that returns a new object.
Code: Select all
local function spawnObject(x,y, width, height)
-- create the object
local newObject = {x = x, y = y, width = width, height = height}
-- return it.
return newObject
end
The function above can be used this way:
Code: Select all
local obj1 = spawnObject(10,10, 50, 50) -- A 50x50 object spawned at position 10,10
local obj2 = spawnObject(30, 75, 25, 25) -- A 25x25 object spawned at position 30,75
This is nice, because although
obj1 and
obj2 have the same template, they are different.
Code: Select all
-- move obj1
print(obj1.x) --> 10
obj1.x = obj1.x + 10
print(obj1.x) --> 20
--move obj2
print(obj2.x) --> 30
obj2.x = obj2.x + 10
print(obj2.x) --> 40
At this point, everything sounds nice. But you might encounter a problem. What if you are planning to work with a large number of objects ?
And this is where the fun begins. Basically, you can create another table (let us name it
collectionOfObjects) where you will register all your objects.
Code: Select all
local collectionOfObjects = {}
local function spawnObject(x,y, width, height)
-- same as before
end
-- spawning objects
local obj1 = spawnObject(10,10, 50, 50)
table.insert(collectionOfObjects, obj1) -- register obj1 in collection
local obj2 = spawnObject(30, 75, 25, 25) -- A 25x25 object spawned at position 30,75
table.insert(collectionOfObjects, obj2) -- register obj2 in collection
In case you want to move
all your objects, for instance, this can be done fairly simply, by looping through the entire
collectionOfObjects with
ipairs:
Code: Select all
for i,obj in ipairs(collectionOfObjects) do
obj.x = obj.x + 10
end
See ?
Last minor enhancement, you might find painful to register manually each new object.
Fair enough, this can be done automatically by the factory function.
Code: Select all
local collectionOfObjects = {}
local function spawnObject(x,y, width, height)
-- creates the object
local newObject = {x = x, y = y, width = width, height = height}
--register the object
table.insert(collectionOfObjects,newObject)
-- return it.
return newObject
end
Usage example:
Code: Select all
local obj1 = spawnObject(10,10, 50, 50) -- obj1 is already registered in the collectionOfObjects at position 1
print('Let us check if obj1 == collectionOfObjects[1]', obj1 == collectionOfObjects[1]) --> true
local obj2 = spawnObject(30, 75, 25, 25) -- obj2 is already registered in the collectionOfObjects at position 2
print('Let us check if obj2 == collectionOfObjects[2]', obj2 == collectionOfObjects[2]) --> true
That's the general idea. Later on, you can bind the factory function to trigger a new object when a specific key is pressed.
or whatever. It depends on your needs.
Hope this helps.
Feel free to ask if anything goes wrong.