Page 6 of 15
Re: HUMP - yet another set of helpers
Posted: Sat Jun 04, 2011 1:51 pm
by vrld
Minor tweak in
hump.timer:
You can now stop the execution of periodic timers by returning false from the function, e.g.
Code: Select all
-- spawn a new enemy every 5 seconds or until the hive is destroyed
Timer.addPeriodic(5, function()
if not hive or hive.life <= 0 then return false end
hive:spawnZombieClown()
end)
Re: HUMP - yet another set of helpers
Posted: Sat Jun 04, 2011 3:13 pm
by Gnx
vrld wrote:Minor tweak in
hump.timer:
You can now stop the execution of periodic timers by returning false from the function, e.g.
Code: Select all
-- spawn a new enemy every 5 seconds or until the hive is destroyed
Timer.addPeriodic(5, function()
if not hive or hive.life <= 0 then return false end
hive:spawnZombieClown()
end)
Thats actually very useful
Re: HUMP - yet another set of helpers
Posted: Fri Feb 17, 2012 7:47 am
by Patooty
Apologies if I missed it, but I wasn't able to find any talk on the camera module in this thread. Do you know of a working example somewhere? I'm having some problems getting it to work, and not sure if I'm just doing something stupid / overlooking something.
I'm able to create a camera object by doing like so:
Code: Select all
Vector = require 'library/hump/vector.lua'
Camera = require 'library/hump/camera.lua'
But when I try to call mainCamera:attach() I get the following error:
http://imgur.com/a5eOz
But if I instead initialize the camera like so:
Code: Select all
mainCamera = Camera:new(Vector(100,100), 2, math.pi/2)
then the error I get is:
http://imgur.com/fNhID
Edit: I had initially assumed that the vector was being incorrectly read as not-a-vector, but upon further investigation... In the camera class I decided to check why exactly the assert was failing:
Code: Select all
local typeStr = string.format("zoom is of type: %s", type(self.zoom))
assert(type(self.zoom) == "number", typeStr)
yields:
It seems that the camera's fields are incorrect, though I'm still unsure what correct solution would be.
Re: HUMP - yet another set of helpers
Posted: Fri Feb 17, 2012 2:06 pm
by tentus
Have you seen the documentation at
http://vrld.github.com/hump/#Camera?
Re: HUMP - yet another set of helpers
Posted: Fri Feb 17, 2012 4:33 pm
by Patooty
Yeah, my second attempt to initialize the camera actually came directly from that page. I have no other camera code besides initializing it (in an init function) and trying to attach/detach the camera (within a draw function). I feel like I'm having problems because of my inexperience with Lua. I don't see how the camera.zoom could be reporting that it's a table type. Here's the init function for the camera in camera.lua for easy reference:
Code: Select all
local function new(pos, zoom, rot)
local pos = pos or vector(love.graphics.getWidth(), love.graphics.getHeight()) / 2
local zoom = zoom or 1
local rot = rot or 0
return setmetatable({pos = pos, zoom = zoom, rot = rot}, camera)
end
Re: HUMP - yet another set of helpers
Posted: Fri Feb 17, 2012 4:39 pm
by vrld
You need to replace the "
:" with a "
."
The
: is a lua shortcut to set the first function argument to the thing before colon, so this
Code: Select all
Camera:new(Vector(100,100), 2, math.pi/2)
is equivalent to:
Code: Select all
Camera.new(Camera, Vector(100,100), 2, math.pi/2)
Calling it like so will work:
Code: Select all
Camera.new(Vector(100,100), 2, math.pi/2)
Or use the preferred way:
Code: Select all
Camera(Vector(100,100), 2, math.pi/2)
You can see a working camera example
here, especially
in this file and
in this file. Note that this uses an outdated hump version, where attach()/detach() were named predraw()/postdraw().
Re: HUMP - yet another set of helpers
Posted: Fri Feb 17, 2012 6:55 pm
by Patooty
Seems like I had a pretty warped understanding (or misunderstanding) of how the ':' worked.
Thanks very much for your help
Re: HUMP - yet another set of helpers
Posted: Fri Feb 17, 2012 8:31 pm
by Nixola
I didn't know it at all
'
Re: HUMP - yet another set of helpers
Posted: Fri Feb 17, 2012 10:30 pm
by MarekkPie
The : operator is simply
syntactic sugar.
Code: Select all
obj:foo(x)
-- is equivalent to
obj.foo(self, x)
So that can be a way of helping you understand when you use the . operator and when you use the : operator. If you haven't created an object of that classes type, then use the . operator, since you have no self yet. If you are operating on an already created object, then you use the : operator, so that you can use any variables stored within that object.
Re: HUMP - yet another set of helpers
Posted: Sat Feb 18, 2012 2:27 pm
by kikito
MarekkPie means this:
Code: Select all
obj:foo(x)
-- is equivalent to
obj.foo(obj, x)