Page 1 of 2

creating new objects when needed

Posted: Wed May 07, 2014 4:55 am
by minkzilla
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.

Re: creating new objects when needed

Posted: Wed May 07, 2014 6:15 am
by bartbes
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

Posted: Sat May 10, 2014 5:59 pm
by minkzilla
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.
this doesn't really help me very much, could you be more specific please?

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

Posted: Sat May 10, 2014 6:33 pm
by szmol96
First of all, you have to create a table, like this:

Code: Select all

enemies = { }
I assume you can handle player positions, x and y. Then you would check if the player is at a certain position.

Code: Select all

if player.x == something and player.y == something then
	--do something
end
or

Code: Select all

if player.x > something and player.y > something then
	--do something
end
or

Code: Select all

if player.x > something and player.x < something and player.y > something and player.y < something then
	--do something
end
And then you can replace the "do something"s with the actual code, specifically you insert an object into the enemies table, like this:

Code: Select all

table.insert(enemies, {
			x = somewhere,
			y = somewhere
	})
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.

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
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.

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
I hope this helps. Ask if you have any problems and I will see what i can do about it.

Re: creating new objects when needed

Posted: Sat May 10, 2014 7:58 pm
by minkzilla
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.

Re: creating new objects when needed

Posted: Sun May 11, 2014 7:50 am
by szmol96
Of course it goes away if you left

Code: Select all

o.x = o.x + 10 * dt
o.y = o.y + 10 * dt
as it was.

Re: creating new objects when needed

Posted: Sun May 11, 2014 6:15 pm
by minkzilla
How would i get it to stay than?

Re: creating new objects when needed

Posted: Sun May 11, 2014 6:33 pm
by szmol96
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.

Re: creating new objects when needed

Posted: Sun May 11, 2014 11:31 pm
by minkzilla
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.

Re: creating new objects when needed

Posted: Mon May 12, 2014 4:40 pm
by szmol96
Leaving the area that spawns the enemy doesn't despawn it.