Getting Camera to work

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Manello
Prole
Posts: 7
Joined: Thu May 29, 2014 5:11 pm

Getting Camera to work

Post by Manello » Sat Dec 08, 2018 10:19 pm

So I used like 2 years ago a camera system for my games which I got from this tutorial:
http://ebens.me/2011/04/19/cameras-in-l ... he-basics/

Today I wanted to program again with Löve, but the old camera system doesn't seem to work anymore.

I keep getting the error "Attempt to index local "self" (a number value)".
The Error itself appears in the function camera:setPosition when trying to set x and y.

What is wrong with the code associated with the camera?
I already tried to index the table "camera" directly instead of .self, call it from different functions and so on.

Camera Object

Code: Select all

camera = {}
camera.x = 0
camera.y = 0
camera.scaleX = 1
camera.scaleY = 1
camera.rotation = 0
Camera Functions

Code: Select all

function camera:set()
	love.graphics.push()
	love.graphics.rotate(-self.rotation)
	love.graphics.scale(1 / self.scaleX, 1 / self.scaleY)
	love.graphics.translate(-self.x, -self.y)
end
function camera:unset()
	love.graphics.pop()
end
function camera:move(dx, dy)
	self.x = self.x + (dx or 0)
	self.y = self.y + (dy or 0)
end
function camera:rotate(dr)
	self.rotation = self.rotation + dr 
end
function camera:scale(sx, sy)
	sx = sx or 1
	self.scaleX = self.scaleX * sx
	self.scaleY = self.scaleY * (sy or sx)
end
function camera:setPosition(x, y)
	--self.x = rOriginX+(player.x-player.y)*16-windowX/4+16 USE THIS TO FOCUS ON A OBJECT/ENTITY
	--self.y = rOriginY+((player.y+player.x)*16)/2-windowY/4+24
	self.x = x
	self.y = y
end
function camera:setScale(sx, sy)
	self.scaleX = sx or self.scaleX
	self.scaleY = sy or self.scaleY
end
Call of camera:setPosition() when an Arrow key is pressed

Code: Select all

	if keyMap["up"] then
		camera.setPosition(camera.x, camera.y-100*dt)
	elseif keyMap["down"] then
		camera.setPosition(camera.x, camera.y+100*dt)
	elseif keyMap["left"] then
		camera.setPosition(camera.x-100*dt, camera.y)
	elseif keyMap["right"] then
		camera.setPosition(camera.x+100*dt, camera.y)
	end
And "camera:set()" and "camera:unset()" are called in love.draw each frame.

Ross
Prole
Posts: 11
Joined: Tue Mar 13, 2018 12:12 pm

Re: Getting Camera to work

Post by Ross » Sun Dec 09, 2018 12:53 am

You're calling setPosition with a . instead of a : , so it's not getting the self reference.

Manello
Prole
Posts: 7
Joined: Thu May 29, 2014 5:11 pm

Re: Getting Camera to work

Post by Manello » Sun Dec 09, 2018 10:07 am

Thanks that does the job!

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests