I have this problem with the bullets, so after the bullets are shot, every time I move my player the bullets move too/ How can I make it so when the bullets are shot, they aren't controlled by the movement of the player.
Another thing is how can I make it so the bullets are shot from the gun of the player(The gun is in the image) I believe this has something to do with the offset that am not familiar with. Am attaching the base image, you can make it so it rotates instead of changing images if you wish. (Other than that, you can multi-comment the "Direction player facing" if statement)
Am just a beginner, and am making this game to learn more and for fun. Any help is appreciated.
Here is the code:
plrImage = love.graphics.newImage("shooterUp.png")
plr = {
xPos = love.graphics.getWidth()/2 - 50,
yPos = love.graphics.getHeight()/2 - 50,
spd = 300,
dir = "Up",
}
bullets = {}
function love.update(dt)
bullet = {
width = 0,
height = 0,
xPos = plr.xPos + 40,
yPos = plr.yPos + 10,
spd = 500
}
function love.keypressed(key)
if key == "space" then
table.insert(bullets, bullet)
end
end
--Movement & Collision
if love.keyboard.isDown("w") and plr.yPos > 0 then
plr.yPos = plr.yPos - plr.spd * dt
plr.dir = "Up"
elseif love.keyboard.isDown("s") and plr.yPos + 50 < love.graphics.getHeight() then
plr.yPos = plr.yPos + plr.spd * dt
plr.dir = "Down"
end
if love.keyboard.isDown("a") and plr.xPos > 0 then
plr.xPos = plr.xPos - plr.spd * dt
plr.dir = "Left"
elseif love.keyboard.isDown("d") and plr.xPos + 50 < love.graphics.getWidth() then
plr.xPos = plr.xPos + plr.spd * dt
plr.dir = "Right"
end
--Direction player facing
if plr.dir == "Up" then
plrImage = love.graphics.newImage("shooterUp.png")
elseif plr.dir == "Down" then
plrImage = love.graphics.newImage("shooterDown.png")
elseif plr.dir == "Right" then
plrImage = love.graphics.newImage("shooterRight.png")
elseif plr.dir == "Left" then
plrImage = love.graphics.newImage("shooterLeft.png")
end
--Bullet Update
for i, v in ipairs(bullets) do
if plr.dir == "Up" then
v.yPos = v.yPos - v.spd * dt
v.width = 1
v.height = 10
elseif plr.dir == "Down" then
v.yPos = v.yPos + v.spd * dt
v.width = 1
v.height = 10
elseif plr.dir == "Right" then
v.xPos = v.xPos + v.spd * dt
v.width = 10
v.height = 1
elseif plr.dir == "Left" then
v.xPos = v.xPos - v.spd * dt
v.width = 10
v.height = 1
end
end
--Bullet Out of bounds check
for i, v in ipairs(bullets) do
if v.yPos < 0 or v.yPos + v.height > love.graphics.getHeight() then
table.remove(bullets, i)
elseif v.xPos < 0 or v.xPos + v.width > love.graphics.getWidth() then
table.remove(bullets, i)
end
end
drawBullet()
end
function drawBullet()
function love.draw()
for i, v in ipairs(bullets) do
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", v.xPos, v.yPos, v.width, v .height)
end
love.graphics.draw(plrImage, plr.xPos, plr.yPos)
end
end
function love.draw()
love.graphics.draw(plrImage, plr.xPos, plr.yPos)
end
TopDownShooter bullet problem?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Sir_Silver
- Party member
- Posts: 286
- Joined: Mon Aug 22, 2016 2:25 pm
- Contact:
Re: TopDownShooter bullet problem?
Code: Select all
--Bullet Update
for i, v in ipairs(bullets) do
if plr.dir == "Up" then
v.yPos = v.yPos - v.spd * dt
v.width = 1
v.height = 10
elseif plr.dir == "Down" then
v.yPos = v.yPos + v.spd * dt
v.width = 1
v.height = 10
elseif plr.dir == "Right" then
v.xPos = v.xPos + v.spd * dt
v.width = 10
v.height = 1
elseif plr.dir == "Left" then
v.xPos = v.xPos - v.spd * dt
v.width = 10
v.height = 1
end
end
Code: Select all
plrImage = love.graphics.newImage("shooterUp.png")
plr = {
xPos = love.graphics.getWidth()/2 - 50,
yPos = love.graphics.getHeight()/2 - 50,
spd = 300,
dir = "Up"
}
bullets = {}
function love.update(dt)
bullet = {
width = 0,
height = 0,
xPos = plr.xPos + 40,
yPos = plr.yPos + 10,
spd = 500,
dir = plr.dir --Stores the current direction of the player on the bullet object.
}
function love.keypressed(key)
if key == "space" then
table.insert(bullets, bullet)
end
end
--Movement & Collision
if love.keyboard.isDown("w") and plr.yPos > 0 then
plr.yPos = plr.yPos - plr.spd * dt
plr.dir = "Up"
elseif love.keyboard.isDown("s") and plr.yPos + 50 < love.graphics.getHeight() then
plr.yPos = plr.yPos + plr.spd * dt
plr.dir = "Down"
end
if love.keyboard.isDown("a") and plr.xPos > 0 then
plr.xPos = plr.xPos - plr.spd * dt
plr.dir = "Left"
elseif love.keyboard.isDown("d") and plr.xPos + 50 < love.graphics.getWidth() then
plr.xPos = plr.xPos + plr.spd * dt
plr.dir = "Right"
end
--Direction player facing
if plr.dir == "Up" then
plrImage = love.graphics.newImage("shooterUp.png")
elseif plr.dir == "Down" then
plrImage = love.graphics.newImage("shooterDown.png")
elseif plr.dir == "Right" then
plrImage = love.graphics.newImage("shooterRight.png")
elseif plr.dir == "Left" then
plrImage = love.graphics.newImage("shooterLeft.png")
end
--Bullet Update
for i, v in ipairs(bullets) do --This now updates the bullet's position based on the direction stored on the bullet, not the player's direction.
if v.dir == "Up" then
v.yPos = v.yPos - v.spd * dt
v.width = 1
v.height = 10
elseif v.dir == "Down" then
v.yPos = v.yPos + v.spd * dt
v.width = 1
v.height = 10
elseif v.dir == "Right" then
v.xPos = v.xPos + v.spd * dt
v.width = 10
v.height = 1
elseif v.dir == "Left" then
v.xPos = v.xPos - v.spd * dt
v.width = 10
v.height = 1
end
end
--Bullet Out of bounds check
for i, v in ipairs(bullets) do
if v.yPos < 0 or v.yPos + v.height > love.graphics.getHeight() then
table.remove(bullets, i)
elseif v.xPos < 0 or v.xPos + v.width > love.graphics.getWidth() then
table.remove(bullets, i)
end
end
drawBullet()
end
function drawBullet()
function love.draw()
for i, v in ipairs(bullets) do
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", v.xPos, v.yPos, v.width, v .height)
end
love.graphics.draw(plrImage, plr.xPos, plr.yPos)
end
end
function love.draw()
love.graphics.draw(plrImage, plr.xPos, plr.yPos)
end
Who is online
Users browsing this forum: Bing [Bot] and 1 guest