Page 1 of 1

Rads^9000 "Safety box" spawn, and alignment bug

Posted: Sun Oct 26, 2014 12:58 am
by SteweeBee
Dear ladies and gentlemen of the forums

I'm making a small 2d game, I started with Lua/LÖVE 2 days ago and it isn't NEAR done, but when its finished, it'll be a simple 2d shooter revolving around the player... The screen follows the player, and you can fire bullets aimed at the mouse, with left click, to fend off hordes of so far completely unimplemented enemies of unknown number or strength... The catch is, you have to stay within your "safety box" to protect yourself from the intense radiation around you, leaving it is an instant game over... I'm trying to implement coding at the bottom to make the "safety box" keep moving after the player has stopped, until the player is aligned in the center again. The problem is, the player is spawning OUTSIDE the "safety box", even though I have the spawn locations set to the same spot AND the alignment is COMPLETELY based around the center of both boxes... I have around 130 lines of code (just inside the main.lua file, I have others, but I know the problem is somewhere in main.lua) It'll be messy, and I'm sure there's loads of newbie mistakes clogging it up in there, but I hope you can help, the main suspect code is at the VERY bottom, I'm sure that code would be considered MONSTROUS and over complicated to anyone with more coding experience, but I tried.

Code:

Code: Select all

require("player")
require("camera")
require("coll")
function love.load()
  set()
  love.window.setTitle("Rad^9000: version 0.01")
  box = love.graphics.newImage("Box.png")
 end
function love.update(dt)

  --Variables
  scenx = x - love.window.getWidth()/2
  sceny = y - love.window.getHeight()/2
  pcenx = x + 12.5
  pceny = y + 12.5
  bcenx = x2 + 25
  bceny = y2 + 25

  --Scrolling Camera
   camera:setPosition(cenx, ceny)

   --Collision detection: 
   --actually checks if you are NOT in the box
   if aColl(x, y, w, h, x2, y2, w2, h2) and
    aColl((x + w), y, w, h, x2, y2, w2, h2) and
    aColl(x, (y + h), w, h, x2, y2, w2, h2) then
   else
    love.event.quit()
  end

  --Movement
    if love.keyboard.isDown("w") then
      y = y - (speed * dt)
   end
    if love.keyboard.isDown("a") then
      x = x - (speed * dt)
   end
    if love.keyboard.isDown("s") then
      y = y + (speed * dt)
   end
    if love.keyboard.isDown("d") then
      x = x + (speed * dt)
   end
end

function love.draw()

  --Camera details, Player, and Box setup
    camera:set()
    love.graphics.setColor(255,255,255) 
    love.graphics.draw(box, (x2 + (w2/2)), (y2 + (h2/2)), 0)
    love.graphics.draw(Player, (x + (w/2)), (y + (h/2)), 0)
    camera:unset()
end

function love.keyreleased(k)

  --ATTEMPT at making the box continue moving
  --Until its aligned with the player again
  --Once I get the code working Im going to port it
  --to another file and use "require"
  --Suspected code malfunction somewhere in there
  if key == "w" then
    if pcenx > bcenx then
      x2 = x2 + ((speed - 50) * dt)
    end
      
    if pcenx < bcenx then
      x2 = x2 - ((speed - 50) * dt)
    end

    if pceny > bceny then
      y2 = y2 + ((speed - 50) * dt)
    end

    if pceny < bceny then
      y2 = y2 - ((speed - 50) * dt)
    end
  end

  if key == "s" then
    if pcenx > bcenx then
      x2 = x2 + ((speed - 50) * dt)
    end
      
    if pcenx < bcenx then
      x2 = x2 - ((speed - 50) * dt)
    end

    if pceny > bceny then
      y2 = y2 + ((speed - 50) * dt)
    end

    if pceny < bceny then
      y2 = y2 - ((speed - 50) * dt)
    end
  end

  if key == "a" then
    if pcenx > bcenx then
      x2 = x2 + ((speed - 50) * dt)
    end
      
    if pcenx < bcenx then
      x2 = x2 - ((speed - 50) * dt)
    end

    if pceny > bceny then
      y2 = y2 + ((speed - 50) * dt)
    end

    if pceny < bceny then
      y2 = y2 - ((speed - 50) * dt)
    end
  end

  if key == "d" then
    if pcenx > bcenx then
      x2 = x2 + ((speed - 50) * dt)
    end
      
    if pcenx < bcenx then
      x2 = x2 - ((speed - 50) * dt)
    end

    if pceny > bceny then
      y2 = y2 + ((speed - 50) * dt)
    end

    if pceny < bceny then
      y2 = y2 - ((speed - 50) * dt)
    end
  end
end

Re: Rads^9000 "Safety box" spawn, and alignment bug

Posted: Sun Oct 26, 2014 8:05 am
by DaedalusYoung
The love.keyreleased function is called only once, so trying to change the position of an object over time is not going to work in there. Also, dt only exists in the love.update function, so you will get errors when trying to use that in another function.

Re: Rads^9000 "Safety box" spawn, and alignment bug

Posted: Sun Oct 26, 2014 1:17 pm
by SteweeBee
DaedalusYoung wrote:The love.keyreleased function is called only once, so trying to change the position of an object over time is not going to work in there. Also, dt only exists in the love.update function, so you will get errors when trying to use that in another function.
Ah.... See, I wouldn't know that... Using "dt" works fine for me, I don't get errors, but now I know about the function... Ill get it to work! thanks!