creating new objects when needed

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
minkzilla
Prole
Posts: 7
Joined: Sun Apr 27, 2014 4:08 pm

creating new objects when needed

Post by minkzilla » Wed May 07, 2014 4:55 am

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.

User avatar
bartbes
Sex machine
Posts: 4944
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: creating new objects when needed

Post by bartbes » Wed May 07, 2014 6:15 am

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.

minkzilla
Prole
Posts: 7
Joined: Sun Apr 27, 2014 4:08 pm

Re: creating new objects when needed

Post by minkzilla » Sat May 10, 2014 5:59 pm

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?

User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

Re: creating new objects when needed

Post by szmol96 » Sat May 10, 2014 6:33 pm

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.
All your code are belong to us.

minkzilla
Prole
Posts: 7
Joined: Sun Apr 27, 2014 4:08 pm

Re: creating new objects when needed

Post by minkzilla » Sat May 10, 2014 7:58 pm

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.

User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

Re: creating new objects when needed

Post by szmol96 » Sun May 11, 2014 7:50 am

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.
All your code are belong to us.

minkzilla
Prole
Posts: 7
Joined: Sun Apr 27, 2014 4:08 pm

Re: creating new objects when needed

Post by minkzilla » Sun May 11, 2014 6:15 pm

How would i get it to stay than?

User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

Re: creating new objects when needed

Post by szmol96 » Sun May 11, 2014 6:33 pm

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.

minkzilla
Prole
Posts: 7
Joined: Sun Apr 27, 2014 4:08 pm

Re: creating new objects when needed

Post by minkzilla » Sun May 11, 2014 11:31 pm

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.

User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

Re: creating new objects when needed

Post by szmol96 » Mon May 12, 2014 4:40 pm

Leaving the area that spawns the enemy doesn't despawn it.
All your code are belong to us.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests