[英]attempt to call method 'print' (a nil value) when implementation OOP in Lua
[英]Error when attempting to call function in different lua file (function is a nil value)
當我嘗試使用試圖在盒子對撞機對象中調用函數時,出現以下錯誤。
錯誤:
main.lua:26:嘗試調用字段“ Update”(一個nil值)
追溯
main.lua:26:在'update'函數中[C]:在'xpcall'函數中
編碼:
require("BoxCollider")
local s1 = {}
s1.x = 10
s1.y = 10
s1.w = 10
s1.h = 10
s1.collider = BoxCollider.new(s1.x,s1.y,s1.w,s1.h)
test = BoxCollider.new(40, 7,15,15)
function love.load()
end
function love.update(dt)
if love.keyboard.isDown("d") then s1.x = s1.x + 100 * dt end
---[[
s1.collider:Update(s1.x,s1.y)
if s1.collider:CheckCollisions(test) then
s1.x = 10
end
--]]
end
function love.draw()
love.graphics.rectangle("fill", s1.x, s1.y, s1.w, s1.h)
love.graphics.rectangle("fill", 40, 7, 15,15)
end
BoxCollider = {}
BoxCollider._index = BoxCollider
--x is the x point of the collider, should be equal to the object's x point if the box is for an object
--y is the y point of the collider, should be equal to the object's x point if the box is for an object
--w is the width of the box
--h is the height of the box
function BoxCollider.new(x,y,w,h)
return setmetatable({x = x or 0, y = y or 0, w = w or 0, h = h or 0},BoxCollider)
end
function BoxCollider:CheckCollisions( v )
return self.x < (v.x + v.h) and v.x < (self.x + self.w) and self.y < (v.y+v.h) and v.y < (self.y+self.h)
end
function BoxCollider:reSize(nw,nh)
self.w = nw
self.h = nh
end
function BoxCollider:Update(nx,ny)
self.x = nx
self.y = ny
end
我認為您只是在BoxCollider .__ index中忘記了下划線。 只有一個下划線._index,當:Update被調用而找不到時,不會搜索BoxCollider方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.