我不明白,错误在第 36 行。如何修复它以及为什么会发生?我究竟做错了什么?
行运行时错误:-1:d:\ gameobject.lua:36:尝试调用方法“位置”(表值)
Vector3f = {}
Vector3f.__index = Vector3f
function Vector3f:new(x, y, z)
local vector = {x = x or 0, y = y or 0, z = z or 0}
setmetatable(vector, self)
return vector
end
Transform = {}
Transform.__index = Transform
function Transform:new(position, rotation, scale)
local transform = {
position = position or Vector3f:new(0, 0, 0),
rotation = rotation or Vector3f:new(0, 0, 0),
scale = scale or Vector3f:new(0, 0, 0)
}
setmetatable(transform, self)
return transform
end
function Transform:position(x, y, z)
self.position.x = x or self.position.x
self.position.y = y or self.position.y
self.position.z = z or self.position.z
end
local pos = Vector3f:new(1, 2, 3)
local rot = Vector3f:new(4, 5, 6)
local sca = Vector3f:new(7, 8, 9)
local transform = Transform:new(pos, rot, sca)
print("X: " .. transform.position.x .. " Y: " .. transform.position.y .. " Z: " .. transform.position.z)
print("X: " .. transform.rotation.x .. " Y: " .. transform.rotation.y .. " Z: " .. transform.rotation.z)
print("X: " .. transform.scale.x .. " Y: " .. transform.scale.y .. " Z: " .. transform.scale.z)
transform:position(10, 20, 30)
print("X: " .. transform.position.x .. " Y: " .. transform.position.y .. " Z: " .. transform.position.z)