Page 1 of 1

How do I make the background always be in front of the player?

Posted: Mon Aug 15, 2016 2:07 pm
by zerofoxgiven
By the way, I'm using Love2D for the 3DS (it's called LovePotion), but the code is basically the same as on PC except for minor differences.

So I'm making a top down game with a background that scrolls when the player is moving but usuallly the player goes to fast and he goes off screen. How do I stop this? This is my code:

Code: Select all

function love.load()
  
  love.graphics.set3D(true)
  
  love.graphics.setScreen('top')
  
  background = love.graphics.newImage('background.png')
  youngBoy = love.graphics.newImage('youngboy.png')
  
  youngboyx = 0
  youngboyy = 0
  
  backgroundx = 0
  backgroundy = 0
  
  distanceBetween = backgroundy - youngboyy 
end


function love.draw()
  love.graphics.draw(background, backgroundx, backgroundy)
  
  love.graphics.draw(youngBoy, youngboyx, youngboyy)
  
 

  end
function love.update(dt)
 
 if love.keyboard.isDown("right") then
     youngboyx = youngboyx + 2
end

if love.keyboard.isDown("left") then
    youngboyx = youngboyx - 2
  end
if love.keyboard.isDown("up") then
  youngboyy = youngboyy - 2
end
if love.keyboard.isDown("down") then
  youngboyy = youngboyy + 2
end

if love.keyboard.isDown("down") then
  backgroundy = backgroundy - 5
end



if love.keyboard.isDown("up") then
  backgroundy = backgroundy + 5
end


function love.keypressed(key)
  if key == 'start' then
    love.event.quit()
  end
end

Sorry for the messy code, I've only started using Love2D since like 20 hours ago. So can you help me? Thanks. I really want to finish this game, but I want to polish the current version first. If you're wondering what genre it will be, it'll be a top down shooter game, but with swords and grenades etc. as well.

Re: How do I make the background always be in front of the player?

Posted: Tue Aug 16, 2016 3:32 am
by Jasoco
You're not multiplying anything movement related by dt. You should think of it as position = position + pixels_per_second * dt. And use that thinking for anything that takes time.

Re: How do I make the background always be in front of the player?

Posted: Tue Aug 16, 2016 3:55 am
by pgimeno
Small correction, position = position + pixels_per_second * dt

Re: How do I make the background always be in front of the player?

Posted: Tue Aug 16, 2016 7:09 am
by Jasoco
Thanks. Silly mistake. It can also be minus depending on which direction it's going.

Re: How do I make the background always be in front of the player?

Posted: Tue Aug 16, 2016 7:32 am
by Plu
So I'm making a top down game with a background that scrolls when the player is moving but usuallly the player goes to fast and he goes off screen. How do I stop this? This is my code:
Two very straight-forward solutions seem to be:
A) make the player slower
B) make the background faster