Hi guys
I've been playing with Love and so far I love it. However, I'm struggling with something which is important for whatever project i would try to do. I have no idea how to program things which behave in a certain way and that can spawn frequently. For example, how can i program arrow which spawn on click? I suppose that this is where object oriented programming comes in handy, but I have no idea how to implement the idea of OOP. I'm not sure that I've explained my problem clearly, so if you need more info you can ask.
Thanks
Questions about "spawning things" with love and lua
Re: Questions about "spawning things" with love and lua
Well, it depends on what you're trying to do. Lua isn't a fully object oriented language, so you don't need to formally create classes and stuff. So much of what you can read about OOP might not be necessary for a game.
There are several ways to "spawn things" in Lua. One of the simplest is to just use functions that create tables. Sort of like this:
For a total beginner to programming, this is definately good enough. It is not the fastest and most efficient way of doing it, but it's not something you're going to notice in a relatively simple game.
There are several ways to "spawn things" in Lua. One of the simplest is to just use functions that create tables. Sort of like this:
Code: Select all
function spawnEnemy()
enemy = {}
--do stuff with it, add stuff to the table enemy
return enemy
end
--use like this
enemy1 = spawnEnemy()
enemy2 = spawnEnemy()
--or like this
enemies = {}
for i=1,100 do
table.insert(enemies,spawnEnemy())
end
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
-
- Party member
- Posts: 235
- Joined: Sat Dec 15, 2012 6:54 am
Re: Questions about "spawning things" with love and lua
You could do this with tables. Try something like this...
Short version without lengthy comments:
Lengthy comments version:
https://dl.dropbox.com/u/17269775/arrow.lua
If you'd like more traditional OO programming, I suggest that you start here:
http://www.lua.org/pil/16.html
Short version without lengthy comments:
Code: Select all
--declare arrow table
arrow = { }
--this represents whether an arrow has been marked for removal - this is explained below
arrowRemoved = false
arrowImage = --SET AN IMAGE FOR THE ARROW HERE!
--function for spawning arrows
function NewArrow(x,y,sx,sy,r,rSpeed)
local id = #arrow+1
arrow[id] = {
x = x,
y = y,
sx = sx,
sy = sy,
r = r,
rSpeed = rSpeed,
removed = false
}
return id
end
function UpdateArrows(dt)
--removing arrows marked for removal
if arrowRemoved then
arrowRemoved = false
for i = #arrow, 1, -1 do
if (arrow[i].removed == true) then
table.remove(arrow, i)
end
end
end
--update loop
for i,v in ipairs(arrow) do
if (v.removed == false) then
v.r = v.r + v.rSpeed * dt
end
end
end
--now, let's draw the arrows!
function DrawArrows()
for i,v in ipairs(arrow) do
love.graphics.draw(arrowImage, v.x, v.y, v.r, v.sx, v.sy)
end
end
--function for marking a particular arrow for removal
function RemoveArrow(i)
arrow[i].removed = true
arrowRemoved = true
end
https://dl.dropbox.com/u/17269775/arrow.lua
If you'd like more traditional OO programming, I suggest that you start here:
http://www.lua.org/pil/16.html
Who is online
Users browsing this forum: Bing [Bot] and 2 guests