It might be easier if you tell us what variable bullet is and tell us what the draw function looks like.
It almost looks like you only draw the bullet right there in shootBullets (as I see no saving of position data). And since frame's are built from the ground up (I mean: it clears every frame) you have to draw the bullet EVERY frame you want it in. You might try something like this (assuming the variable bullet is the image of the bullet):
function shootBullets()
xDis = mouseX - x
yDis = mouseY - y
rot = math.atan2(yDis, xDis) + math.pi
rotation = math.atan2(yDis, xDis) * (180 / math.pi) - 90
posX = posX - (math.cos(rot) * 1.2)
posY = posY - (math.sin(rot) * 1.2)
table.insert(bullets, {x = posX, y = posY})
end
function draw()
--the rest of the draw function
for i, v in ipairs(bullets) do
love.graphics.draw(bullet, v.x, v.y)
end
--anything else you want here
end
i had a similar problem of things flashing, i was playing and learning how to use things and i wanted to add a word to the screen where i clicked and a different word where i right clicked, it all worked fin but i had the graphics.draw() in the mouse click function meaning it drew it there but when LOVE did the Draw() function again it wasnt in there to get redrawn and stay on screen.
so basically your mouse click function needs to tell the bullet where it needs to be but the bullet should actually be drawn in the draw() function or it wont stay on the screen.
I had basically the same thing you posted above bartbes, but it doesn't work. It does not define a bullet as an individual. Every click, a bullet will spawn on top of the previous one and double in speed. But it makes zero sense to me.... well it does, but no matter what I do I end up with many of the same images on the screen using the same 3 variables that should be changed when they are inserted into the table.
I need a way to spawn a new bullet every click instead of editing the positions and applying it all of the bullet images. The table will apply the exact same thing to every bullet that is inserted into the table. I guess my question is: Is there any way to define a new bullet?
And no, setting the x/y to a different number besides posX while inserting the bullet does not solve this, when drawing that bullet the values will still be shared.
Wait? You're saying that with my code every bullets uses the same position? In that case you might want to try my code again, as it shouldn't (under any circumstance). My guess is you aren't using the x and y variables stored in the table, but the variables set in shootBullets. As for adding a new one, that is exactly what table.insert does.
-- Global bullets table
gBullets = {}
---------------
-- Bullet object
---------------
bullet = {}
bullet.__index = bullet
function bullet:new( )
-- Make a new table({}) and set it's metatable to bullet.
local newBullet = setmetatable( {}, bullet )
newBullet.x = 0
newBullet.y = love.graphics.getHeight( ) / 2
newBullet.speed = 80 -- Pixels per second.
-- Insert to global bullets table
table.insert( gBullets, newBullet )
return newBullet;
end
function bullet:destroy( )
-- Loop through bullets, and remove the one equal to self.
for i, v in ipairs( gBullets ) do
if v == self then
table.remove( gBullets, i )
break; -- Stop the loop
end
end
end
function bullet:updatePosition( dt )
-- Update the position based on the speed
self.x = self.x + (self.speed*dt) -- Direction is right..
-- If it's outside the screen, remove it
if self.x < 0 or self.y < 0 or
self.x > love.graphics.getWidth( ) or self.y > love.graphics.getHeight( ) then
self:destroy( )
end
end
function bullet:draw( )
love.graphics.setColor( 255, 255, 255, 255 )
love.graphics.point( self.x, self.y )
end
---------------
-- Love hooks
---------------
function load( )
end
function update( dt )
for i, v in ipairs( gBullets ) do
v:updatePosition( dt )
end
end
function draw( )
for i, v in ipairs( gBullets ) do
v:draw( )
end
end
function mousepressed( )
-- Make a new bullet when the mouse is pressed
bullet:new( )
end