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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
zerofoxgiven
Prole
Posts: 1
Joined: Mon Aug 15, 2016 2:01 pm

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

Post 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.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

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

Post 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.
Last edited by Jasoco on Tue Aug 16, 2016 7:09 am, edited 1 time in total.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

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

Post by pgimeno »

Small correction, position = position + pixels_per_second * dt
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

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

Post by Jasoco »

Thanks. Silly mistake. It can also be minus depending on which direction it's going.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

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

Post 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
Post Reply

Who is online

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