Hi there people, I have a little problem. I am writing this code and the way that it's written, when I press space to shoot, it spawns the bullet and it doesn't shoot until I release the button. Can someone me how to make it shoot automaticaly as long as I have the button pressed?
function love.load()
love.graphics.setMode(500, 480, false, true, 2)
love.graphics.setBackgroundColor( 0, 0, 0)
love.graphics.setCaption("Proper")
bullet = {
image = love.graphics.newImage("BolaBola.png"),
x = 0,
y = 0,
}
back = love.graphics.newImage("background.png")
music = love.audio.newSource("Musicaaa.wav")
love.audio.play(music)
love.mouse.setVisible( false )
player = {
x = 230,
y = 400,
image = love.graphics.newImage("Blueshippp.png")
}
background1 = {
x = 0,
y = 0,
image = love.graphics.newImage("background.png")
}
background2 = {
x = 0,
y = -600,
image = love.graphics.newImage("background.png")
}
end
function love.draw()
love.graphics.draw(background2.image, background2.x, background2.y)
love.graphics.draw(background1.image, background1.x, background1.y)
love.graphics.draw(player.image, player.x, player.y)
if shoot == true then
love.graphics.draw(bullet.image, bullet.x, bullet.y)
end
end
function love.update()
background1.y = background1.y + 2
background2.y = background2.y + 2
if background1.y == 600 then
background1.y = -600
end
if background2.y == 600 then
background2.y = -600
end
if love.keyboard.isDown("left") then
player.x = player.x - 5
end
if player.x > 300 then
player.x = player.x - 0
end
if love.keyboard.isDown("right") then
player.x = player.x + 5
end
if love.keyboard.isDown(" ") then
shoot = true
bullet.x = player.x + 10
bullet.y = player.y
end
bullet.y = bullet.y - 20
end
You're going to probably want to have more than one bullet in the entire level... You're going to have to spawn multiple objects.
You should probably get middleclass, or write your own library (like I did).
Thanks for the advice. I don't really know what a library is, as I'm new to Lua( and programming in general) but I guess that exploring librarys will be my next task
What zac mean is that you need some OOP to make it, or at least, to make it easier.
And since lua don't have build in classes and stuff, you should use a library.
local Object = {}
function Object:new (o)
o = o or {} -- create object if user does not provide one
setmetatable(o, self)
self.__index = self
return o
end
Point = Object:new { x = 0, y = 0 } -- new class
function Point:show() -- add method to class
print(self.x, self.y)
end
p = Point:new() -- create instance
p:show()
p2 = Point:new { x = 4, y = 5 } -- custom instance
p2:show()
Vector = Point:new { x = 0, y = 0, len = 1 } -- subclass
function Vector:show() -- override inherited method
print(self.x, self.y, self.len)
end
v = Vector:new() -- new instance
v:show()
v2 = Vector:new { x = .7, y = .3, len = 1 } -- custom instance
v2:show()
You may have noticed that we said Vector = Point:new() to create a new class, when we just as easily could have said Vector = p:new(). Every object can serve as a prototype for another object.
Not a bad amount of functionality for 6 lines of code. For a few more lines of code, you can get initializers and the ability to call superclass methods.
class={}
setmetatable(class,{
__call=function(name,...)
local obj=newproxy(true)
getmetatable(obj).__index={}
getmetatable(obj).__newindex=function(t,k,v)
getmetatable(obj).__index[k]=v
if k:sub(1,2)=="__" then
getmetatable(obj)[k]=v
end
end
for i in values{...} do --inherit stuff
for k,v in pairs(getmetatable(i).__index) do
obj[k]=v
end
end
obj.class=name --classnames!
obj.inherits={...} --inheritance!
obj.type=function(self)
return self.class
end
obj.typeOf=function(self,class)
if self.class==class then
return true;
end
for _,v in pairs(self.inherits) do
if v:typeOf(class) then
return true;
end
end
return false;
end
obj.__call=function(self,...)
local n=newproxy(self)
if n.new then
n:new(...) --Is it really that hard to give a callback?
end
return n
end
return obj; --So many times have I forgotten this little piece of code..
end;
})
class={}
setmetatable(class,{
__call=function(name,...)
local obj=newproxy(true)
getmetatable(obj).__index={}
getmetatable(obj).__newindex=function(t,k,v)
getmetatable(obj).__index[k]=v
if k:sub(1,2)=="__" then
getmetatable(obj)[k]=v
end
end
for i in values{...} do --inherit stuff
for k,v in pairs(getmetatable(i).__index) do
obj[k]=v
end
end
obj.class=name --classnames!
obj.inherits={...} --inheritance!
obj.type=function(self)
return self.class
end
obj.typeOf=function(self,class)
if self.class==class then
return true;
end
for _,v in pairs(self.inherits) do
if v:typeOf(class) then
return true;
end
end
return false;
end
obj.__call=function(self,...)
local n=newproxy(self)
if n.new then
n:new(...) --Is it really that hard to give a callback?
end
return n
end
return obj; --So many times have I forgotten this little piece of code..
end;
})
Is your enter key broken?
Also, Czrlos, if you still need help with weapons(assuming you haven't figured it out yet), it's quite easy (and I didn't use middle-class). Technocat helped me a while back, so I could show you my code and help you understand it, if you need it.