Camera

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.
KyleFlores1014
Citizen
Posts: 73
Joined: Thu May 25, 2017 1:43 pm

Camera

Post by KyleFlores1014 »

Im trying to make the screen follow my character how do I do it?
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Camera

Post by xNick1 »

Use a camera library!

Some Love libs: https://love2d.org/wiki/Category:Libraries
gamera (one of the most popular camera libs, but feel free to use whatever you like): https://love2d.org/forums/viewtopic.php?f=5&t=12033
KyleFlores1014
Citizen
Posts: 73
Joined: Thu May 25, 2017 1:43 pm

Re: Camera

Post by KyleFlores1014 »

game.lua
Camera = require "hump/camera"
cam = Camera(player.x,player.y)
function game_load()
player_load()
end
function game_draw()
player_draw()
OSC_draw()
love.graphics.setBackgroundColor(0, 125, 0)
end
function game_update(dt)
local dx,dy = player.x - cam.x, player.y - cam.y
cam:move(dx/2, dy/2)
player_update(dt)
OSC_update(dt)
end
function game_mousepressed(x,y,button,isTouch)
OSC_mousepressed(x,y,button,isTouch)
end
function game_touchpressed(id,x,y)
OSC_touchpressed(id,x,y)
end
function game_touchreleased(id,x,y)
OSC_touchreleased(id,x,y)
end
function game_keypressed(key)
if key == "escape" then
gamestate = "settings"
end
end
error game.lua:2 attempt to index global 'player' a nil value
player.lua
function player_load()
--player
player.x = 0
player.y = 0
player.pms = 3
--player
end
function player_draw()
love.graphics.rectangle("fill",player.x,player.y,75,75)
end
function player_update(dt)
if love.keyboard.isDown("w") then
player.y = player.y - player.pms
elseif love.keyboard.isDown("s") then
player.y = player.y + player.pms
end
if love.keyboard.isDown("a") then
player.x = player.x - player.pms
elseif love.keyboard.isDown("d") then
player.x = player.x + player.pms
end
if love.keyboard.isDown("w") and love.keyboard.isDown("lshift") then
player.y = player.y - player.pms+1
elseif love.keyboard.isDown("s") and love.keyboard.isDown("lshift") then
player.y = player.y + player.pms+1
end
if love.keyboard.isDown("a") and love.keyboard.isDown("lshift") then
player.x = player.x - player.pms+1
elseif love.keyboard.isDown("d") and love.keyboard.isDown("lshift") then
player.x = player.x + player.pms+1
end
end
KyleFlores1014
Citizen
Posts: 73
Joined: Thu May 25, 2017 1:43 pm

Re: Camera

Post by KyleFlores1014 »

can you help me?
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Camera

Post by xNick1 »

I'll try to help you, could you please attach a .love file or a zip file containing the folder?
I can't understand very much from that code, please try to use the code tag next time!

Code: Select all

function player_load()
--player
player.x = 0
player.y = 0
player.pms = 3
--player
end
Try to create the player like: player = { }
so:

Code: Select all

function player_load()
--player
player = { }
player.x = 0
player.y = 0
player.pms = 3
--player
end

That could be the problem.
If it still doesn't work just attach a .love file so that we can help you
KyleFlores1014
Citizen
Posts: 73
Joined: Thu May 25, 2017 1:43 pm

Re: Camera

Post by KyleFlores1014 »

game.love
(487.77 KiB) Downloaded 468 times
Here is the file I fixed the player already but the camera is causing a error.
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Camera

Post by xNick1 »

Fix like that in player.lua:
The camera took the player as a input param but you declared it after the camera initialization!
also, x is under player, so it's player.x and not player.player.x!

Code: Select all


player = { }
player.x = 0
player.y = 0
player.pms = 3

cam = Camera(player.x, player.y)
Also comment the part where you require settings and a couple of images you didn't include.
It works now
fix.jpg
fix.jpg (121.22 KiB) Viewed 17023 times

Also to avoid all those if statements you could store the states in a table:

Code: Select all


  gamestate = "menu"

  if gamestate == "menu" then
    menu_load()
  elseif gamestate == "about" then 
    about_load()
  elseif gamestate == "game" then
    game_load()
  elseif gamestate == "settings" then
    settings_load()
      
    end
to

Code: Select all


  states = { "menu", "about", "game", "settings" ]
  selectedState = 1
	
  states[selectedState] 

KyleFlores1014
Citizen
Posts: 73
Joined: Thu May 25, 2017 1:43 pm

Re: Camera

Post by KyleFlores1014 »

but the camera still doesnt follow the player. sorry Im new
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Camera

Post by yetneverdone »

KyleFlores1014 wrote: Tue May 30, 2017 9:10 am but the camera still doesnt follow the player. sorry Im new
Hey, this is flamendless, the one you in twitter (*insight*)

I saw your codes, there's a lot of issues with it. It will be hard for you to maintain it on the long run or as your project grows.

--EDIT--

I''ll upload a basic template for you. It is not too late to organize your project to avoid further headaches haha
stuffs.zip
(1.51 KiB) Downloaded 504 times
KyleFlores1014
Citizen
Posts: 73
Joined: Thu May 25, 2017 1:43 pm

Re: Camera

Post by KyleFlores1014 »

Ok thanks, Im gonna try to implement this hump gamestates in my game, but have you found a solution to the camera problem?
Post Reply

Who is online

Users browsing this forum: Google [Bot], 麻猫和黄猫 and 49 guests