First game, struggling with camera/movement

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
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

First game, struggling with camera/movement

Post 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
Mr_Burkes
Prole
Posts: 1
Joined: Fri Aug 15, 2014 5:57 pm

Re: First game, struggling with camera/movement

Post 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
hi
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Re: First game, struggling with camera/movement

Post 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.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: First game, struggling with camera/movement

Post 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.
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Re: First game, struggling with camera/movement

Post 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
Attachments
proto.love
(601.35 KiB) Downloaded 97 times
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: First game, struggling with camera/movement

Post 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
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Re: First game, struggling with camera/movement

Post by parallax7d »

MadByte YOU ROCK!!! thanks so much! :ultrahappy:
User avatar
Rukiri
Citizen
Posts: 68
Joined: Mon Jun 27, 2011 5:52 am

Re: First game, struggling with camera/movement

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

Who is online

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