# shooting

Hi! Sorry I’ve been asking so many questions :/. In my game I’m making you can shoot in four directions, up, down, left, and right. Right now I only have the code say what direction it is and was wondering how would I handle the bullets. Would i use physics? And if so, it’d be great if you could give me an example. Thanks in advance!

Try using a table to keep track,~table.insert()~ and when they reach their target or out of bounds, remove them~table.remove()~

@compactbear Here’s an example. Tap the screen for the direction and to shoot a bullet.

displayMode(FULLSCREEN)

function setup()
bullets={}
end

function draw()
background(40, 40, 50)
fill(255)
ellipse(WIDTH/2,HEIGHT/2,10)
for a,b in pairs(bullets) do
b.x=b.x+b.z
b.y=b.y+b.w
if b.x<0 or b.x>WIDTH then
table.remove(bullets,a)
end
if b.y<0 or b.y>HEIGHT then
table.remove(bullets,a)
end
ellipse(b.x,b.y,10)
end
end

function touched(t)
if t.state==BEGAN then
dirx=(t.x-WIDTH/2)/50
diry=(t.y-HEIGHT/2)/50
table.insert(bullets,vec4(WIDTH/2,HEIGHT/2,dirx,diry))
end
end

thanks

How would I use this in my current code?
I just want it to be able to shoot up down left or right.

displayMode(FULLSCREEN)

function setup()
bullets = {}
end

function draw()
background(40,40,50)
fill(255)
ellipse(WIDTH / 2, HEIGHT / 2, 10)
for a, b in pairs(bullets) do
b.x = b.x + b.z
b.y = b.y + b.w
if b.x < 0 or b.x > WIDTH then
table.remove(bullets, a)
end
if b.y < 0 or b.y > HEIGHT then
table.remove(bullets,a)
end
ellipse(b.x, b.y, 10)
end
end
function touched(t)
currentx = t.x
currenty = t.y
id = t.id
if t.state == BEGAN then
swipex = currentx
swipey = currenty
swipeid = id
elseif t.state == ENDED and id == swipeid then

swipeid = nil
if swipey - currenty > 75 then
print 'down'
elseif currenty- swipey > 75 then
print "up"
elseif swipex - currentx > 75 then
print "left"
elseif currentx - swipex > 75 then
print "right"
end
end

end

@compactbear Here’s your code with changes. I’m not sure where you wanted the bullets to come from, so I made them to come from where you touched and to go in the direction you swipe (left, right, up, down).

displayMode(FULLSCREEN)

function setup()
bullets = {}
end

function draw()
background(40,40,50)
fill(255)
for a, b in pairs(bullets) do
b.x = b.x + b.z
b.y = b.y + b.w
if b.x < 0 or b.x > WIDTH or b.y < 0 or b.y > HEIGHT then
table.remove(bullets,a)
end
ellipse(b.x,b.y, 10)
end
end

function touched(t)
currentx = t.x
currenty = t.y
id = t.id
if t.state == BEGAN then
swipex = currentx
swipey = currenty
swipeid = id
elseif t.state == ENDED and id == swipeid then
swipeid = nil
dx=0
dy=0
if swipey - currenty > 75 then
dy=-1
elseif currenty- swipey > 75 then
dy=1
elseif swipex - currentx > 75 then
dx=-1
elseif currentx - swipex > 75 then
dx=1
end
if dx~=0 or dy~=0 then
table.insert(bullets,vec4(currentx,currenty,dx,dy))
end
end
end

thanks Dave!

how would i make these move faster?

nevermind