Bullet's won't move...[RESOLVED]

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
WolfNinja2
Party member
Posts: 150
Joined: Wed Oct 24, 2012 10:10 pm

Bullet's won't move...[RESOLVED]

Post by WolfNinja2 »

They do spawn, but then they don't want to move, here's my code.
I'M A BIG NEWBIE NEWB!
I forgot to add direction in the table xD
Thanks Guys!

Code: Select all

bullet = {}

function bulletspawn(x,y,direction)
	table.insert(bullet,{x = x,y = y, w = 4, h = 2,speed = 120}) -- REALLY BRO, NO DIRECTION IN THERE? DO YOU EVEN LIFT BRUH?
end

function bulletshoot(key)
	if key == "d" then
	bulletspawn(player.x + (player.w/2),player.y + (player.h/2),"right")
	elseif key == "a" then
	bulletspawn(player.x + (player.w/2),player.y + (player.h/2),"left")
	end
end

function bulletupdate(dt)
	for i,v in ipairs(bullet) do
   if v.direction == "right" then
      v.x = v.x + v.speed * dt
   end
   if v.direction == "left" then
     v.x = v.x - v.speed * dt
		end
	end
end

function bulletdraw()
	for i,v in ipairs(bullet) do
		g.setColor(255,255,255,255)
		g.rectangle("fill",v.x,v.y ,v.w,v.h)
		end
end



function bulletremove()
	if v.x < 0 or
	v.x < g.getWidth() == 20 then
	table.remove(bullet)
	end
end
Hope you all can help!

-Wolf
Last edited by WolfNinja2 on Mon Nov 12, 2012 11:03 am, edited 1 time in total.
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Bullet's won't move...

Post by Qcode »

When you insert into the table bullet you have direction in the function arguments, but you don't actually put that into the table you're inserting. It should be this:

Code: Select all

function bulletspawn(x,y,directionarg)
   table.insert(bullet,{x = x,y = y, w = 4, h = 2,speed = 120, direction = directionarg})
end
(I changed it to directionarg because I don't think direction = direction would work)

Anyway it wasn't working because v.direction equaled nil rather than left or right.
User avatar
paritybit
Citizen
Posts: 53
Joined: Thu Sep 06, 2012 3:52 am

Re: Bullet's won't move...

Post by paritybit »

Qcode wrote: (I changed it to directionarg because I don't think direction = direction would work)
"direction = direction" would work, but more clarity never hurts.
User avatar
WolfNinja2
Party member
Posts: 150
Joined: Wed Oct 24, 2012 10:10 pm

Thanks!

Post by WolfNinja2 »

All I needed to do was have direction = direction in the table. Newbie mistake :)
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests