applyLinearImpulse error on player.lua
Posted: Tue Jul 12, 2022 8:35 pm
Hi,
I'm starting to learn love2d and I'd love (pun intended) it if somebody can help me to understand this.
I'm using windfield from a327ex. (https://github.com/a327ex/windfield)
If I create only one file named main.lua containing all logic, everything goes well, the problem is if I want to create one file for all "Player" definitions, if I do this, I get an error when I press space for "jumping". the error is:
player.lua
As you can see is very simple, the "Player" and "Ground" are perfectly drawn, the problem is when pressing "Space"
Thank you!
I'm starting to learn love2d and I'd love (pun intended) it if somebody can help me to understand this.
I'm using windfield from a327ex. (https://github.com/a327ex/windfield)
If I create only one file named main.lua containing all logic, everything goes well, the problem is if I want to create one file for all "Player" definitions, if I do this, I get an error when I press space for "jumping". the error is:
main.luaError: main.lua:28: attempt to call method 'applyLinearImpulse' (a nil value)
stack traceback:
[love "boot.lua"]:345: in function 'applyLinearImpulse'
main.lua:28: in function <main.lua:25>
[love "callbacks.lua"]:154: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'
Code: Select all
WF = require("windfield")
Player = require("player")
function love.load()
World = WF.newWorld(0, 0, true)
World:setGravity(0, 512)
World:addCollisionClass("Player")
World:addCollisionClass("Ground")
Player:load()
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
Code: Select all
Player = {}
function Player:load()
self = World:newRectangleCollider(100, 100, 20, 50)
self:setCollisionClass("Player")
end
function Player:update(dt) end
function Player:draw() end
function Player:keypressed(key)
if key == "space" then
self:applyLinearImpulse(0, -500)
end
end
return Player
Thank you!