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?
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
Scoots wrote: ↑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?
Hi and welcome to the forums!
When you create the bullets (and player), you can't store their own angles as a variable inside their tables as well, because...?
debug = true
-- player starting location and image
player = {
x = love.graphics.getWidth()/2,
y = love.graphics.getHeight()/2,
img = nil,
angle = math.pi -- Store this in player table instead, no globals to mess with you.
}
-- 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
player.angle = player.angle - (dt * 5 * math.pi/2)
end
if love.keyboard.isDown('d') then
player.angle = player.angle + (dt * 5 * math.pi/2)
end
if love.keyboard.isDown('w') then
player.x = player.x + 200 * (math.cos(player.angle) * dt)
player.y = player.y + 200 * (math.sin(player.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,
angle = player.angle -- Unique variable for each bullet, because you need it.
}
table.insert(bullets, newBullet)
end
for i, bullet in ipairs(bullets) do
bullet.x = bullet.x + ( math.cos(bullet.angle) * 250 * dt)
bullet.y = bullet.y + ( math.sin(bullet.angle) * 250 * dt)
end
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, player.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
love.graphics.draw(bullet.img, bullet.x, bullet.y, bullet.angle + (math.pi/2))
end
end
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.