creating new objects when needed
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
creating new objects when needed
I am new to love2d and Lua in general, but so far am loving it! but I have run into a problem that I can not find the answer googling, any help would be greatly appreciated.
So what I am trying to do is create a new enemy (boxes right now) every time you get to a certain point. The problem is I have no idea how to keep making more enemies when ever they are needed, besides just making a ton of rectangles and keep them off screen until needed.
So what I am trying to do is create a new enemy (boxes right now) every time you get to a certain point. The problem is I have no idea how to keep making more enemies when ever they are needed, besides just making a ton of rectangles and keep them off screen until needed.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: creating new objects when needed
It depends on how you create those rectangles. Typically though you've got a table that's basically just a list of rectangles, who are themselves tables. At that point you can simply insert a new entry into the list that represents the new rectangle, and suddenly you've got an extra rectangle.
Re: creating new objects when needed
this doesn't really help me very much, could you be more specific please?bartbes wrote:It depends on how you create those rectangles. Typically though you've got a table that's basically just a list of rectangles, who are themselves tables. At that point you can simply insert a new entry into the list that represents the new rectangle, and suddenly you've got an extra rectangle.
I tried this, but I am obviously doing something wrong becasue the rectangle won't even show up. Also how do you control the movement of every rectangle separately, do you just make variables for all of the rectangles in the same way you make the rectangles?
Re: creating new objects when needed
First of all, you have to create a table, like this:
I assume you can handle player positions, x and y. Then you would check if the player is at a certain position.
or
or
And then you can replace the "do something"s with the actual code, specifically you insert an object into the enemies table, like this:
You have to replace the "somewhere"s with your desired positions. As for controlling every enemy seperately, you can use a for loop. You should use this in love.update. Don't forget to include dt.
And if your enemies do not show up, the main reason may be that you are not drawing them. So, you should use love.draw.
I hope this helps. Ask if you have any problems and I will see what i can do about it.
Code: Select all
enemies = { }
Code: Select all
if player.x == something and player.y == something then
--do something
end
Code: Select all
if player.x > something and player.y > something then
--do something
end
Code: Select all
if player.x > something and player.x < something and player.y > something and player.y < something then
--do something
end
Code: Select all
table.insert(enemies, {
x = somewhere,
y = somewhere
})
Code: Select all
function love.update(dt)
local i, o
for i, o in ipairs(enemies) do
o.x = o.x + 10 * dt
o.y = o.y + 10 * dt
end
end
Code: Select all
function love.draw()
local i, o
for i, o in ipairs(enemies) do
love.graphics.draw(the image of the enemy, o.x , o.y)
end
end
All your code are belong to us.
Re: creating new objects when needed
I am getting the rectangle to spawn. But it goes away when I am not in collision with the area, and likewise it does not move.
thanks for you help btw.
thanks for you help btw.
Re: creating new objects when needed
Of course it goes away if you left as it was.
Code: Select all
o.x = o.x + 10 * dt
o.y = o.y + 10 * dt
All your code are belong to us.
Re: creating new objects when needed
How would i get it to stay than?
Re: creating new objects when needed
Just don't change the the positions. I only wrote that code to show an example how to change the position of your enemies. If you don't want them to move, delete those two lines.
All your code are belong to us.
Re: creating new objects when needed
sorry, realized I worded that the wrong way.
What I wanted to say was how would I get the enemy to stay spawned even after I left the area that initially spawns it.
What I wanted to say was how would I get the enemy to stay spawned even after I left the area that initially spawns it.
Re: creating new objects when needed
Leaving the area that spawns the enemy doesn't despawn it.
All your code are belong to us.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests