Value from one variable to another
Posted: Wed Apr 19, 2017 11:42 pm
So right now I'm making an astroid-y game, however I've ran into an issue and it's driving me crazy. The ship rotates around and the value of the rotation is stored in the variable angle. Now the bullets shot out of the ship also need to come out at the same angle, and I got that to work. However, the bullets continue to use the changing angle variable. So the bullets are changing angle when the ship turns. How can I make it so the bullet maintains it's own angle when it's created. I found local variables in the lua by googling around. I tried making a local variable which stores the current value of the angle in both the update and draw functions but that didn't work. Is there anything I'm missing?
Code: Select all
debug = true
angle = math.pi
currentAngle = 0
-- player starting location and image
player = {x = love.graphics.getWidth()/2, y = love.graphics.getHeight()/2, img = nil}
-- table that holds bullet objects
bullets = {}
function love.load(arg)
--loading images for the game
player.img = love.graphics.newImage('assests/player.png')
bulletImg = love.graphics.newImage('assests/bullet.png')
backgroundImg = love.graphics.newImage('assests/starBackground.png')
end
function love.update(dt)
if love.keyboard.isDown('escape') then
love.event.push('quit')
end
if love.keyboard.isDown('a') then
angle = angle - (dt * 5 * math.pi/2)
end
if love.keyboard.isDown('d') then
angle = angle + (dt * 5 * math.pi/2)
end
if love.keyboard.isDown('w') then
player.x = player.x + 200 * (math.cos(angle) * dt)
player.y = player.y + 200 * (math.sin(angle) * dt)
end
if love.keyboard.isDown('space') then
bulletExitX = player.x
currentBullet = bulletExitX
bulletExitY = player.y
newBullet = {img = bulletImg, x = player.x, y = player.y, speed = 250}
table.insert(bullets, newBullet)
end
for i, bullet in ipairs(bullets) do
local angle1 = angle
bullet.x = bullet.x + ( math.cos(angle1) * 250 * dt)
bullet.y = bullet.y + ( math.sin(angle1) * 250 * dt)
end
function love.draw(dt)
-- background for the screen
love.graphics.draw(backgroundImg, love.graphics.getHeight(), love.graphics.getWidth() )
-- draw the player image with it's origin at the center of the image
love.graphics.draw(player.img, player.x, player.y, angle + (math.pi/2), 1, 1, player.img:getWidth()/2, player.img:getHeight()/2)
-- for each bullet in bullets draw them
for i, bullet in ipairs(bullets) do
local angle1 = angle
love.graphics.draw(bullet.img, bullet.x, bullet.y, angle1 + (math.pi/2))
end
end
end