Page 1 of 1
First game, struggling with camera/movement
Posted: Fri Aug 15, 2014 1:06 pm
by parallax7d
Basic question on how to move the camera around. Using hump camera.
Here is what I have so far. Pressing "d" or "s" doesn't move anything. Also, cam doesn't seem to want to "start" at coordinates 100, 100 for some reason, but I don't get an error.
Code: Select all
function love.load()
Camera = require "lib.camera"
viewx = 100; viewy = 100
cam = Camera(viewx, viewy, 0)
end
function love.keypressed(key)
if key == "d" then
cam:move(250, 250)
if key == "s" then
viewx = viewx + 1
end
end
function love.update(dt)
local dx, dy = viewx - cam.x, viewy - cam.y
cam:move(dx/2, dy/2)
end
Re: First game, struggling with camera/movement
Posted: Fri Aug 15, 2014 7:00 pm
by Mr_Burkes
This function has a syntactical error:
Code: Select all
function love.keypressed(key)
if key == "d" then
cam:move(250, 250)
if key == "s" then
viewx = viewx + 1
end
end
It should be:
Code: Select all
function love.keypressed(key)
if key == "d" then
cam:move(250, 250)
end -- notice the "end" here
if key == "s" then
viewx = viewx + 1
end
end
Re: First game, struggling with camera/movement
Posted: Fri Aug 15, 2014 7:30 pm
by parallax7d
Sorry, just a typo when I was writing out the question. In my code it's correct. I can run the game, get no error messages, but pressing the keys does nothing.
Re: First game, struggling with camera/movement
Posted: Fri Aug 15, 2014 7:59 pm
by MadByte
When doing this:
Code: Select all
function love.update(dt)
local dx, dy = viewx - cam.x, viewy - cam.y
cam:move(dx/2, dy/2)
end
you kinda lock your camera to this point (it automatically moves with the viewx, viewy positions). This is why your key input does not work.
To check the position the camera starts you can print out the camera position in your draw function. I guess it will be at 100, 100 when removing the code in your update function.
Re: First game, struggling with camera/movement
Posted: Sat Aug 16, 2014 12:48 am
by parallax7d
I'm printing out the camera coordinates like you suggested, and they seem to change with the s key. But nothing on the screen moves. I feel like I'm missing some basic thing.
Here's the code, and I'll attach a .love which illustrates the issue
Code: Select all
function love.load()
Camera = require "lib.camera"
viewx = 100; viewy = 100
cam = Camera(viewx, viewy, 0)
debug = false
width = 1024; height = 512
love.window.setMode(1024, 512, {vsync=false})
end
function love.keypressed(key)
if key == "escape" then
love.event.push("quit")
elseif key == "w" then
viewy = viewy-1
elseif key == "a" then
viewx = viewx-1
elseif key == "s" then
viewy = viewy+1
elseif key == "d" then
viewx = viewx+1
end
end
function love.update(dt)
local dx, dy = viewx - cam.x, viewy - cam.y
cam:move(dx/2, dy/2)
ex, why = cam:pos()
campos = "camera x: " .. ex .. "\ncamera y: " .. why
end
function love.draw()
love.graphics.setBackgroundColor(50, 100, 50)
love.graphics.rectangle("fill",50,50,50,50)
love.graphics.print("press a,s,d,f\nnothing moves", 110, 50)
love.graphics.print(campos, 250, 220)
end
Re: First game, struggling with camera/movement
Posted: Sat Aug 16, 2014 7:40 am
by MadByte
Okay, thanks for the *.love file.
I looked into it and I found various problems.
Here is an fixed *.love file, I changed some code while testing stuff, sorry 'bout that.
The major problem was that we didn't set the area the camera need to translate.
protoFixed.love
Re: First game, struggling with camera/movement
Posted: Sun Aug 17, 2014 2:28 am
by parallax7d
MadByte YOU ROCK!!! thanks so much!
Re: First game, struggling with camera/movement
Posted: Tue Aug 19, 2014 12:33 am
by Rukiri
Congrats on working on your first game!
But, you should work out structural order of your lua files, the main.lua file should just be calling everything and updating the game not much else "I do have screen resolution" going on in my main lua file but when it comes time for my RPG KIT release it'll be in it's own lua file.