为什么我的 Vector2:new(ax, ay) 函数得到的是 table 而不是数字值?
Why is my Vector2:new(ax, ay) function getting a table instead of a num value?
我是编程和学习 LÖVE2D 和 LUA 的新手。我根据教程制作了一个 Vec2 模块,但是当我使用它时,(例如 vec2 = Vector2:new(128,128)
),Vector2.x 和 Vector2.y 得到 table 值,我无法操作用他们作为数字。有没有办法限制 Vector2:new() 的参数类型,或者我做错了什么?如果我正在做一些超出良好做法的事情,请也纠正它。谢谢!
文件vector2.lua
return {
new = function(ax, ay)
local Vector2 = {
x = ax or 0,
y = ay or 0
}
function Vector2:move(a, b, dt)
self.x = self.x + a * dt
self.y = self.y + b * dt
end
--To see which values are the fields getting
print(Vector2.x)
print(Vector2.y)
return Vector2
end
}
什么 print(Vector2.x) returns 是这样的:
table: 0x0971d7d0
根据文档,Using Vector2:
Vector2s 是用一个简单的命令创建的:
local point = Vector2.new(x, y)
您不能使用 Vector2(x,y)
或 Vector:new(x,y)
,因为没有相应的定义。
函数调用 Vector2:new(x,y)
是 Vector2(Vector2, x,y)
的语法糖,为了工作需要像
这样的定义
function Vector2:new(x,y)
-- stuff
end
这是
的语法糖
function Vector2.new(self, x, y)
-- stuff
end
否则当您调用 Vector2:new(x,y)
.
时 table Vector2
将以 x
结束
有关如何定义和调用函数的详细信息,请参阅 Lua 参考手册。
我是编程和学习 LÖVE2D 和 LUA 的新手。我根据教程制作了一个 Vec2 模块,但是当我使用它时,(例如 vec2 = Vector2:new(128,128)
),Vector2.x 和 Vector2.y 得到 table 值,我无法操作用他们作为数字。有没有办法限制 Vector2:new() 的参数类型,或者我做错了什么?如果我正在做一些超出良好做法的事情,请也纠正它。谢谢!
文件vector2.lua
return {
new = function(ax, ay)
local Vector2 = {
x = ax or 0,
y = ay or 0
}
function Vector2:move(a, b, dt)
self.x = self.x + a * dt
self.y = self.y + b * dt
end
--To see which values are the fields getting
print(Vector2.x)
print(Vector2.y)
return Vector2
end
}
什么 print(Vector2.x) returns 是这样的:
table: 0x0971d7d0
根据文档,Using Vector2:
Vector2s 是用一个简单的命令创建的:
local point = Vector2.new(x, y)
您不能使用 Vector2(x,y)
或 Vector:new(x,y)
,因为没有相应的定义。
函数调用 Vector2:new(x,y)
是 Vector2(Vector2, x,y)
的语法糖,为了工作需要像
function Vector2:new(x,y)
-- stuff
end
这是
的语法糖function Vector2.new(self, x, y)
-- stuff
end
否则当您调用 Vector2:new(x,y)
.
Vector2
将以 x
结束
有关如何定义和调用函数的详细信息,请参阅 Lua 参考手册。