Topdown shooter bug

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
ydrojd
Prole
Posts: 1
Joined: Sat Apr 01, 2017 11:26 am

Topdown shooter bug

Post by ydrojd »

Hey, i am trying to make a topdown shooter but there is a bug in my code. Everytime i press space to shoot the bullet travels faster and it resets. I hope someone can help me with this.

Code: Select all

function love.load()
  player = {
    x = love.graphics.getWidth()/2,
    y = love.graphics.getHeight()/2,
    walkspeed = 300,
    walkspeedx = 0,
    walkspeedy = 0,
    keybinds = {
      up = "w",
      down = "s",
      left = "a",
      right = "d",
      shoot = "space"
      },
    dia = 17
    }
  
  bullet = {
    x = 0,
    y = 0,
    dia = 5,
    speed = 20,
    speedx = 0,
    speedy = 0,
  }
  
  bullets = {}
end

function love.update(dt)
  movement(dt)
  shoot(dt)
  
end

function love.draw(dt)
  love.graphics.setColor(0,0,255)
  love.graphics.circle("fill", player.x, player.y, player.dia)
  love.graphics.setColor(255,0,0)
  for i, v in pairs(bullets) do
    love.graphics.circle("fill",v.x,v.y,v.dia)
  end
end

function shoot(dt)
  if love.keyboard.isDown(player.keybinds.shoot) then
    mx,my = love.mouse.getPosition()
    bullet.speedx = (mx - player.x)/ math.sqrt((mx - player.x) * (mx - player.x) + (my - player.y) * (my - player.y))
    bullet.speedy = (my - player.y)/ math.sqrt((mx - player.x) * (mx - player.x) + (my - player.y)  * (my - player.y))
    bullet.x = player.x
    bullet.y = player.y
    table.insert(bullets, bullet)
  end
  
  for i, v in pairs(bullets) do
    v.x = v.x + v.speedx * v.speed * dt
    v.y = v.y + v.speedy * v.speed * dt
    
  end
end

function movement(dt)
  if love.keyboard.isDown(player.keybinds.left) then
    player.walkspeedx = 0 - player.walkspeed
  elseif love.keyboard.isDown(player.keybinds.right) then
    player.walkspeedx = player.walkspeed
  else
    player.walkspeedx = 0
  end
  
  if love.keyboard.isDown(player.keybinds.up) then
    player.walkspeedy = 0 - player.walkspeed
  elseif love.keyboard.isDown(player.keybinds.down) then
    player.walkspeedy = player.walkspeed
  else 
    player.walkspeedy = 0
  end
  
  player.x = player.x + player.walkspeedx * dt
  player.y = player.y + player.walkspeedy * dt
end
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Topdown shooter bug

Post by Davidobot »

The problem is that you're using the player.bullet table as a pointer (default in Lua) so when you change values of the player.bullet table, it changes the values of all the inserted tables.
To fix this, create a new table, assign the appropriate values to that, and then insert that into bullets.

EDIT: this should have been posted in Support and Development, not General.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests