Need some help with a weapon

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.
Post Reply
Czrlos
Prole
Posts: 14
Joined: Wed Nov 10, 2010 9:09 pm

Need some help with a weapon

Post by Czrlos »

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?

Code: Select all

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
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Need some help with a weapon

Post by zac352 »

It would appear to me as that you only have one bullet.

You should get middleclass.
Hello, I am not dead.
Czrlos
Prole
Posts: 14
Joined: Wed Nov 10, 2010 9:09 pm

Re: Need some help with a weapon

Post by Czrlos »

Can't I do it all on main.lua?
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Need some help with a weapon

Post by zac352 »

Czrlos wrote:Can't I do it all on main.lua?
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). ;)
Hello, I am not dead.
Czrlos
Prole
Posts: 14
Joined: Wed Nov 10, 2010 9:09 pm

Re: Need some help with a weapon

Post by Czrlos »

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 :D
User avatar
Elvenrix
Prole
Posts: 26
Joined: Sat Aug 28, 2010 6:52 pm

Re: Need some help with a weapon

Post by Elvenrix »

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.
User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Re: Need some help with a weapon

Post by Mud »

OOP in Lua

At a minimum you can use this for basic prototype-based inheritance:

Code: Select all

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
You can use it something like this:

Code: Select all

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.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Need some help with a weapon

Post by zac352 »

This is my class file, I have yet to test it, so feel free to test it yourself.
This file is a part of the Nintendo Rangers Engine. :P

Code: Select all

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;
})
Hello, I am not dead.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Need some help with a weapon

Post by Ryne »

zac352 wrote:This is my class file, I have yet to test it, so feel free to test it yourself.
This file is a part of the Nintendo Rangers Engine. :P

Code: Select all

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.
@rynesaur
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 35 guests