2017-04-23 79 views
0

我一直在做一個遊戲,但由於某種原因,「子彈」即時嘗試產卵只是通常!?lua love2d,不是在子彈中產卵

這是我的「主」

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 

我的「球員」,我試圖把它

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 

和我的「子彈」,我嘗試繪製和產卵它

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 

事情我試過 - ive將子彈更改爲實心方塊而不是調用png - 複製並粘貼從現有(工作)遊戲的子彈類製作

這些東西都沒有用處。 任何幫助都很有用,謝謝!

+0

提供的代碼只包含幾個函數定義..請提供一個最小的可執行示例。 – Piglet

+0

我並不太熱衷於Lua內部,但在'bullet.spawn'中,你添加了一個帶'x'和'y'成員設置的表格,但在'bullet.draw'中循環遍歷'bullet'的每個成員,並且嘗試使用這些'x'和'y'變量進行繪製。糾正我,如果我錯了,但不會該循環還包括函數'bullet.spawn'和'bullet.draw'? – Thelmund

回答

1

的問題似乎是,在player_shoot你是通過dt而不是加入它乘以playerShootTimer

playerShootTimer = playerShootTimer + dt 

我假設playerShootTimer從零開始。然後你有,如果它變得大於playerShootTimerLim和空間被按下,產生子彈。如果您想讓玩家多次拍攝,您還需要在產生子彈後將playerShootTimer重置爲零。

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