Can't make a bullet

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
User avatar
Abaddon
Prole
Posts: 4
Joined: Tue Apr 03, 2012 7:29 am

Can't make a bullet

Post by Abaddon » Tue Apr 03, 2012 9:16 pm

Well, I'm working in a little proyect, a remake of Space Impact+, right now I have the lives, the score and the ship, but when it comes to bullets I find myself in troubles.
I have 2 problems:
>I can't make it to fire the bullet
>I don't know how to make multiple instances of the bullet

For the fist problem I tried with this code:

Code: Select all

function love.load()
        bullet={
		img=love.graphics.newImage("imgs/bullet.png"),
		exist=false,
		x=20,
		y=20,
		defxy=function(self)
			self.x=charx+40
			self.y=chary+12
		end
	}
end
function love.draw()
        if bullet:exist then
		love.graphics.draw(bullet:img,bullet:x,bullet:y)
	end
end
function love.update(dt)
	if love.keyboard.isDown("z") then
		bullet:exist=true
	end
	if bullet:exist then
		if rspwn == 0 then
			bullet:defxy()
			rspwn=1
		end
		bullet:x=bullet:x+50
		if bullet:x > 384 then
			bullet:exist=false
		end
	end
end
(It has other functions, included in the .love file) I think it should work, but when I try to make it run, it gives me a problem in the part of "if bullet:exist then" I don't really know why, it looks like there wasn't any variables.

On the second problem I don't have any idea on how to make it, Any help with that?

Thanks ^w^ (Sorry for my dab english)
Attachments
Space.love
(2.54 KiB) Downloaded 43 times

User avatar
baconhawka7x
Party member
Posts: 484
Joined: Mon Nov 21, 2011 7:05 am
Contact:

Re: Can't make a bullet

Post by baconhawka7x » Tue Apr 03, 2012 9:30 pm

I'm pretty sure, but correct me if I'm wrong. It should be bullet.exist not bullet:exist.

User avatar
baconhawka7x
Party member
Posts: 484
Joined: Mon Nov 21, 2011 7:05 am
Contact:

Re: Can't make a bullet

Post by baconhawka7x » Tue Apr 03, 2012 9:31 pm

And that's not the best way to make bullets;p

User avatar
Abaddon
Prole
Posts: 4
Joined: Tue Apr 03, 2012 7:29 am

Re: Can't make a bullet

Post by Abaddon » Tue Apr 03, 2012 9:32 pm

Yeah I know it's not the best way :/ But I can't think of anything else, any idea?

User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Can't make a bullet

Post by trubblegum » Tue Apr 03, 2012 10:06 pm

A simple way to make bullets would be something like :

Code: Select all

function bullet(x, y) return {x = x, y = y} end -- a bullet constructor

bullets = {} -- a table to hold bullets

table.insert(bullets, bullet(player.x, player.y)) -- a new bullet in the bullets table

for i, b in ipairs(bullets) do -- iterating over bullets
  -- b is the current bullet. do stuff to it
end
Not a true instance, but will do the job if you put it all where it belongs.

A search for "bullets" in this forum will give some useful results.

User avatar
Abaddon
Prole
Posts: 4
Joined: Tue Apr 03, 2012 7:29 am

Re: Can't make a bullet

Post by Abaddon » Tue Apr 03, 2012 10:13 pm

Thanks for that, I will try to adapt it, and will search more just to see what I find ;) thanks

User avatar
Abaddon
Prole
Posts: 4
Joined: Tue Apr 03, 2012 7:29 am

Re: Can't make a bullet

Post by Abaddon » Tue Apr 03, 2012 11:16 pm

Okay, I got it to work, but now I have another question:
>How can I delimit the bullets fired every time I press the button?

I'm trying with something like this:

Code: Select all

	if love.keyboard.isDown("z") then
		if nblts < 0.3 then
			table.insert(bullets,bullet(char.x+40, char.y+12))
			nblts=nblts+1*dt
		end
	end
And it does work, but I dont know how to make nblts=0 after I have released the "z" key to make another shoot, Any help please?
Attachments
Space.love
(2.57 KiB) Downloaded 39 times

User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Can't make a bullet

Post by trubblegum » Wed Apr 04, 2012 5:07 am

I would do :

Code: Select all

actions = {}
actions.shoot = {key = 'z', repeat = 0.25, dt = 0, func = function() table.insert(bullets, bullet()) end}

love.update = function(dt)
  for i, action in pairs(actions) do
    action.dt = action.dt + dt
    if love.keyboard.isDown(action.key) and action.dt >= action.repeat then
      action.dt = 0
      action.func()
    end
  end
end

User avatar
baconhawka7x
Party member
Posts: 484
Joined: Mon Nov 21, 2011 7:05 am
Contact:

Re: Can't make a bullet

Post by baconhawka7x » Thu Apr 05, 2012 1:00 am

Abaddon wrote:And it does work, but I dont know how to make nblts=0 after I have released the "z" key to make another shoot, Any help please?
As with any programming, there are multiple ways to do one thing. For example, in my game "Z0mb3h Ga3m", to make the bullets, I had the player shoot a bullet every time they pushed a key...

Code: Select all

function love.keypressed(key)
if key == keyshoot then
    insert shooting stuff here
end
end
instead of doing it every frame that they hold it down..

Code: Select all

function love.update(dt)
   if love.keyboard.isDown(keyshoot) then
       insert shooting stuff here 
   end
end
I'm not saying either one is more efficiant. I'm just pointing out that there are a lot of ways to go about this.
One way to reset nblts is...

Code: Select all

function love.keyreleased(key)
if key == "z" then
   nblts = 0
end
end
so if they release the key "z" nblts will = 0.

Also, a slightly less productive way to do this is..

Code: Select all

   if love.keyboard.isDown("z") then
      if nblts < 0.3 then
         table.insert(bullets,bullet(char.x+40, char.y+12))
         nblts=nblts+1*dt
      end
   else
       nblts = 0
   end
But I would go with the first way, because the second way could expel adding on other features to guns.
But I haven't tested either of them lol;p

User avatar
Kasperelo
Party member
Posts: 343
Joined: Fri Apr 13, 2012 1:47 pm
Location: The Milky Way

Re: Can't make a bullet

Post by Kasperelo » Sun Apr 22, 2012 1:14 pm

wat

Post Reply

Who is online

Users browsing this forum: Sky_Render, steVeRoll and 10 guests