Creating new instances
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Creating new instances
Sorry for the title, don't know what this is called
Anyways, so I am playing around with physics and i have this box wich spawns if I press "E" but when i release the button it disappeares but i wan't it to spawn, drop to the ground, spawn a new one while the other one stays on the ground, repeat.
Hope i explained this well enough, thanks.
Anyways, so I am playing around with physics and i have this box wich spawns if I press "E" but when i release the button it disappeares but i wan't it to spawn, drop to the ground, spawn a new one while the other one stays on the ground, repeat.
Hope i explained this well enough, thanks.
Re: Creating new instances
Well, it's quite hard to help you without being able to see your code, could you please upload a .love file?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: Creating new instances
I thought that was not needed but sure.
So here it is and let me explain it again.
When I press "E" the box that is bound to the mouse position gets drawn but it's still at the mouse position wich is my problem because I wan't it to fall to the ground so I can spawn a new one.
Sorry for the inconvenience i just thought my code wasn't necessary.
Code: Select all
obj = {}
obj.height = 50
obj.width = 50
obj.y
obj.x
obj.yvel
obj.xvel
obj.pic = love.graphics.newImage("box.png")
function objectMouse()
obj.x = love.mouse.getX()
obj.y = love.mouse.getY()
if love.keyboard.isDown("e") then
spawn = true
else
spawn = false
end
end
function objectSpawn()
if spawn == true then
love.graphics.draw(obj.pic, obj.x, obj.y)
end
end
function DRAW_OBJECT()
objectSpawn()
end
function UPDATE_OBJECT(dt)
objectMouse()
end
So here it is and let me explain it again.
When I press "E" the box that is bound to the mouse position gets drawn but it's still at the mouse position wich is my problem because I wan't it to fall to the ground so I can spawn a new one.
Sorry for the inconvenience i just thought my code wasn't necessary.
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Creating new instances
Make distincts objects.
Then, when pressing key "E".
Obviously, if you need to display all your objects, move or animate them, you will have to loop through the collection of objects.
Hope this helps.
PS/ Wrote this from scratch, so it might contain some typos. Just get the logic.
Code: Select all
-- A register for all spawned objects
local objects = {}
-- a factory object function
local function spawnObject(x,y, width, height,...)
-- create the object
local newObject = {
x = x, y = y,
width = width, height = height,
-- and whatever else you might want to add
}
--register the object
table.insert(objects,newObject)
-- return it. Unused here, but it might be useful.
return newObject
end
Code: Select all
function love.keypressed(k,u)
if k == 'e' then
spawnObject(...) -- the three dots are just placeholders, fill them with your own values
end
end
Code: Select all
for i, object in ipairs(objects) do
-- do_some_stuff_with object
end
PS/ Wrote this from scratch, so it might contain some typos. Just get the logic.
Re: Creating new instances
Distinct objects, that's it! Thanks alot!
Re: Creating new instances
Can this be explained in an easier way? Having trouble to implement and understand it.
Thanks.
Thanks.
Re: Creating new instances
Check this out:
https://www.youtube.com/watch?v=S2oO2wbsXQs
In that tutorial, the bullets are created in a manner similar to how you want to create your physics objects.
https://www.youtube.com/watch?v=S2oO2wbsXQs
In that tutorial, the bullets are created in a manner similar to how you want to create your physics objects.
Re: Creating new instances
Just found this video before checking here!ac3raven wrote:Check this out:
https://www.youtube.com/watch?v=S2oO2wbsXQs
In that tutorial, the bullets are created in a manner similar to how you want to create your physics objects.
Thanks alot, I really löve this helpful community.
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Creating new instances
No problem.Miken1 wrote:Can this be explained in an easier way? Having trouble to implement and understand it.
Thanks.
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
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
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
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
Code: Select all
for i,obj in ipairs(collectionOfObjects) do
obj.x = obj.x + 10
end
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
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
or whatever. It depends on your needs.
Hope this helps.
Feel free to ask if anything goes wrong.
Re: Creating new instances
This is amazing! thank you soooo much.
Just one last question, why do you use local functions instead of just normal functions?
Just one last question, why do you use local functions instead of just normal functions?
Who is online
Users browsing this forum: No registered users and 4 guests