0
votes

Ive been working on a game, but for some reason the "bullet" im trying to spawn in just wont!?

This is my 'Main'

require "scripts.player"
require "scripts.bullet"

function love.load()
bulletShoot = love.graphics.newImage("pics/bullet.png")
playerPic = love.graphics.newImage("pics/player.png")
background = love.graphics.newImage("pics/background.jpg")
player_load()
bullet.load()
end

function love.update(dt)
player_update(dt)
bullet.update(dt)
end

function love.draw()
love.graphics.draw(background, 0, 0)
bullet.draw()
player_draw()
end

My 'Player' where I try to call it

function player_shoot(dt)
playerShootTimer = playerShootTimer * dt
if(playerShootTimer > playerShootTimerLim) then
    if love.keyboard.isDown("space")then
        bullet.spawn(playerX + (playerWidth / 2) - (bullet.width / 2), playerY)
    end
  end
end

function player_update(dt)
player_move(dt)
player_boundary()
player_shoot(dt)
end

and my 'Bullet' where I try to draw and spawn it

function bullet.spawn(x,y)
table.insert(bullet, {x = x, y = y})
end

function bullet.draw()
for i,v in ipairs(bullet) do
    love.graphics.draw(bulletShoot, v.x, v.y, bullet.width, bullet.height)
end
end

things ive tried - ive changed the bullet to a filled square instead of calling the png - ive copied and pasted the bullet class from an existing(working) game ive made

none of these things have been useful. Any help is useful, thank you!

1
the provided code only contains a few function definitions.. please provide a minimal executable example. - Piglet
I'm not too keen on Lua internals, but in bullet.spawn you add a table with x and y members set, but in bullet.draw you loop through every member of bullet and attempt to draw using those x and y variables. Correct me if I'm wrong, but wouldn't that loop also include the functions bullet.spawn and bullet.draw? - Thelmund

1 Answers

1
votes

The issue appears to be that in player_shoot you are multiplying playerShootTimer by dt instead of adding it.

playerShootTimer = playerShootTimer + dt

I'm assuming playerShootTimer starts at zero. Then you have if it becomes greater than playerShootTimerLim and space is pressed, spawn a bullet. You will also need to reset the playerShootTimer back to zero after spawning a bullet if you want to allow the player to shoot multiple times.

if love.keyboard.isDown("space")then
    bullet.spawn(playerX + (playerWidth / 2) - (bullet.width / 2), playerY)
    playerShootTimer = 0
end