Ninja'd by ReFreezed, but since I already wrote it... Also, I think the bit about Player:load() is a bit clearer here.
You seem to have a confusion between class and instance. You also seem to have a confusion about what 'self' means. There also seems to be some confusion between globals and locals.
Let's go in that order.
Player should be a class. This means that it should act as a type, providing a constructor to create instances, which are the variables of that type. However, in main.lua you're using it as if the class was an object. Read a bit about OOP in Lua to try to understand the difference between class and instance and how to make them work. You're not creating a new instance in Player:load().
Then, self. self is an implicit parameter of the function. Normally you don't assign a different value to it. Certainly not in the way you're doing it. Within Player:load(), 'self' is a parameter, and you're changing that parameter so that it has a different value, and when the function ends, the new value is lost forever just as if you did this:
Code: Select all
function setvalue(a)
a = 65
end
setvalue(5) -- This does nothing, because 'a' is lost when the function ends
If you change the parameter inside the function, the new value you set won't be visible outside the function, only until the end of the function.
You probably want the player's collider be
a part of the player, not to be the player itself. So you need to set
a field of the player instance to that value, not the whole instance.
Finally, globals vs. locals. You're using globals everywhere. That's bad because using the same name for different things in different files can lead to misbehaviours (bugs) that are hard to identify. In player.lua you're making Player a global. Later on you redefine the Player global, luckily to the same value, so in the end everything works out, but that's not how you do things. In general, modules should rarely need to define any globals.
Putting all together, this is how you are supposed to do it. I don't know Windfield so I might have made a mistake.
It's customary to start with a capital letter for classes, and with a small letter for instances, to make them easier to distinguish, so I'll follow that.
main.lua
Code: Select all
local WF = require("windfield")
local Player = require("player") -- the class
-- instances local to main.lua
local player
local world
local ground
function love.load()
world = WF.newWorld(0, 0, true)
world:setGravity(0, 512)
world:addCollisionClass("Player")
world:addCollisionClass("Ground")
player = Player:new(world) -- call the constructor
ground = world:newRectangleCollider(0, 500, 800, 100)
ground:setType("static")
ground:setCollisionClass("Ground")
end
function love.update(dt)
world:update(dt)
end
function love.draw()
-- for debugging purposes you can draw the World
world:draw()
end
function love.keypressed(key)
player:keypressed(key)
end
player.lua
Code: Select all
local Player = {}
Player.__index = Player
function Player:new(world)
-- as an exception, 'self' is the class here, not an instance
local obj = setmetatable({}, self)
obj.collider = world:newRectangleCollider(100, 100, 20, 50)
obj.collider:setCollisionClass("Player")
return obj
end
function Player:update(dt) end
function Player:draw() end
function Player:keypressed(key)
if key == "space" then
self.collider:applyLinearImpulse(0, -500)
end
end
return Player
(Edited to fix a few mistakes)