Hey,
How might I go about making bullets?
I've made a bullet class, but I can't figure out how I go about using them, as in, editing them once they're made.
I have:
[code]
Bullet = {}
Bullet.new = function(x, y)
local self = {}
local x = x or love.mouse.getX()
local y = y or love.mouse.getY()
self.getX = function() return x end
self.getY = function() return y end
self.setX = function(val) x = val end
self.setY = function(val) Y = val end
return self
end
[/code]
Obviously I can do a
[code]
if fire then
bullet = Bullet.new(x, y)
end
[/code]
But how can I make more than 1 and be able to modify its position?
I tried something like
[code]
bullets = {}
for i = 0, 20 do
bullets[i] = Bullet.new()
end
for i = 0, 20 do
bullets[i].setX(500)
end
[/code]
but that didn't seem to work :/
Any help?
Thanks
"love.mouse.love.mouse.getX()"
what are you
what
That's not the idiomatic way to handle classes in Lua. Look up metatables.
surely that doesn't work?
edit: automerge etc.
In fact, your code doesn't even make sense. Try this:
[code]
Bullet_mt = {}
Bullet_mt.getX = function(self)
return self.x
end
Bullet_mt.getY = function(self)
return self.y
end
Bullet_mt.setX = function(self, x)
self.x = x
end
Bullet_mt.setY = function(self, y)
self.y = y
end
function NewBullet(x, y)
local self = {}
setmetatable(self, Bullet_mt)
self.x = x or love.mouse.getX()
self.y = y or love.mouse.getY()
return self
end
[/code]
Then you can use this syntactic sugar to invoke the functions:
[code]
local bullet = NewBullet(0, 0)
-- x:y(z) is the same as y(x, z)
bullet:setX(100)
print bullet:getX()
[/code]
[QUOTE=BlkDucky;34163417]"love.mouse.love.mouse.getX()"
what are you
what[/QUOTE]
Sorry, I started writing it out manually, then copy pasted it in forgetting to remove the first love.mouse. Its not like that in the code.
[QUOTE=ROBO_DONUT;34163498]In fact, your code doesn't even make sense. Try this:
[code]
Bullet_mt = {}
Bullet_mt.getX = function(self)
return self.x
end
Bullet_mt.getY = function(self)
return self.y
end
Bullet_mt.setX = function(self, x)
self.x = x
end
Bullet_mt.setY = function(self, y)
self.y = y
end
function NewBullet(x, y)
local self = {}
setmetatable(self, Bullet_mt)
self.x = x or love.mouse.getX()
self.y = y or love.mouse.getY()
return self
end
[/code]
Then you can use this syntactic sugar to invoke the functions:
[code]
local bullet = NewBullet(0, 0)
-- x:y(z) is the same as y(x, z)
bullet:setX(100)
print bullet:getX()
[/code][/QUOTE]
Ok, thanks. But I still don't get how I'd go about using multiple bullets.
[QUOTE=thetree;34163602]Ok, thanks. But I still don't get how I'd go about using multiple bullets.[/QUOTE]
You're using them correctly, but it didn't work before because the class itself was broken.
[QUOTE=ROBO_DONUT;34163684]You're using them correctly, but it didn't work before because the class itself was broken.[/QUOTE]
Excellent, thank you very much, everything works now :)
Sorry, you need to Log In to post a reply to this thread.