Camera
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Camera
Im trying to make the screen follow my character how do I do it?
Re: Camera
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
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
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Re: Camera
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
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
Re: Camera
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!
Try to create the player like: player = { }
so:
That could be the problem.
If it still doesn't work just attach a .love file so that we can help you
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
so:
Code: Select all
function player_load()
--player
player = { }
player.x = 0
player.y = 0
player.pms = 3
--player
end
If it still doesn't work just attach a .love file so that we can help you
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Re: Camera
Here is the file I fixed the player already but the camera is causing a error.
Re: Camera
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!
Also comment the part where you require settings and a couple of images you didn't include.
It works now
Also to avoid all those if statements you could store the states in a table:
to
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)
It works now
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
Code: Select all
states = { "menu", "about", "game", "settings" ]
selectedState = 1
states[selectedState]
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Re: Camera
but the camera still doesnt follow the player. sorry Im new
- yetneverdone
- Party member
- Posts: 448
- Joined: Sat Sep 24, 2016 11:20 am
- Contact:
Re: Camera
Hey, this is flamendless, the one you in twitter (*insight*)KyleFlores1014 wrote: ↑Tue May 30, 2017 9:10 am but the camera still doesnt follow the player. sorry Im new
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
My GameDev Website
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
-
- Citizen
- Posts: 73
- Joined: Thu May 25, 2017 1:43 pm
Re: Camera
Ok thanks, Im gonna try to implement this hump gamestates in my game, but have you found a solution to the camera problem?
Who is online
Users browsing this forum: No registered users and 13 guests