实体继承 Entity.lua 个变量,但不继承其方法
Entities inheriting Entity.lua variables, but not its methods
我正在构建一个包含敌人和玩家的游戏,所有这些都设置为具有各种动画和行为状态。两者的父 class 都是 Entity.lua,它们从中继承变量和方法。然而,虽然敌人和玩家都在继承变量,但出于某种原因,敌人并没有继承这些方法。因此,例如,如果我尝试调用 snakey:changeState('search'),它会给我一条错误消息“尝试调用方法 'changeState'(零值)”。
我在过去的几个游戏中使用相同的顺序创建实体,但从未遇到过这个问题。事实上,如果我以与敌人相同的方式、文件和位置创建播放器,我不会收到任何错误消息。
这是我创建实体的代码。
local snakey
snakey = Snakey {
platform = platform,
player1 = self.player1,
player2 = self.player2,
stateMachine = StateMachine {
['search'] = function() return SnakeySearchState(snakey) end,
['chasing'] = function() return SnakeyChasingState(snakey) end,
['idle'] = function() return SnakeyIdleState(snakey) end
}
}
-- snakey:changeState('search')
-- snakey.stateMachine:change('search', params)
table.insert(self.entities, snakey)
这两行代码是我注意到问题的地方。第一行给出错误,第二行确实有效,但并不令人满意,因为它是一种变通方法。
下面是 Entity.lua 的代码:为简洁起见,我没有包含函数的详细信息,但是当玩家调用它们时,它们都可以正常工作。
Entity = Class{}
function Entity:init(def)
-- position
self.x = def.x
self.y = def.y
self.gravity = 6
-- many more variables
end
function Entity:changeState(state, params)
self.stateMachine:change(state)
end
function Entity:update(dt)
self.stateMachine:update(dt)
end
function Entity:collides(entity)
-- do something
end
function Entity:onDamage()
-- do something
end
function Entity:render()
- renders sprite
end
玩家代码(简要)
Player = Class{__includes = Entity}
function Player:init(def)
Entity.init(self, def)
-- more variables
end
function Player:update(dt)
Entity.update(self, dt)
end
function Player:render()
Entity.render(self)
end
也许是麻烦点,一对一敌人的剧本
Snakey = Class{__includes = Entity}
function Snakey:init(def)
Entity.init(self, def)
-- yet more variables
end
function Snakey:update(dt)
Entity.update(self, dt)
-- entity behavior (works fine, so omitted)
end
function Snakey:render()
Entity.render(self)
end
非常感谢您的帮助。我感到非常沮丧,因为这个序列在过去一直有效,我真的很想知道为什么它不调用那些实体方法。
添加 class 库
--Copyright (c) 2010-2013 Matthias Richter
local function include_helper(to, from, seen)
if from == nil then
return to
elseif type(from) ~= 'table' then
return from
elseif seen[from] then
return seen[from]
end
seen[from] = to
for k,v in pairs(from) do
k = include_helper({}, k, seen) -- keys might also be tables
if to[k] == nil then
to[k] = include_helper({}, v, seen)
end
end
return to
end
-- deeply copies `other' into `class'. keys in `other' that are already
-- defined in `class' are omitted
local function include(class, other)
return include_helper(class, other, {})
end
-- returns a deep copy of `other'
local function clone(other)
return setmetatable(include({}, other), getmetatable(other))
end
local function new(class)
-- mixins
class = class or {} -- class can be nil
local inc = class.__includes or {}
if getmetatable(inc) then inc = {inc} end
for _, other in ipairs(inc) do
if type(other) == "string" then
other = _G[other]
end
include(class, other)
end
-- class implementation
class.__index = class
class.init = class.init or class[1] or function() end
class.include = class.include or include
class.clone = class.clone or clone
-- constructor call
return setmetatable(class, {__call = function(c, ...)
local o = setmetatable({}, c)
o:init(...)
return o
end})
end
-- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).
if class_commons ~= false and not common then
common = {}
function common.class(name, prototype, parent)
return new{__includes = {prototype, parent}}
end
function common.instance(class, ...)
return class(...)
end
end
-- the module
return setmetatable({new = new, include = include, clone = clone},
{__call = function(_,...) return new(...) end})
事实证明顺序很重要。在尝试创建最小可重现代码时,我无法重现错误。经过一些搜索(有点沮丧),我注意到在 Dependencies.lua 中我需要在 Entity.lua 之前的敌人,但在 Player.lua 之后需要。我原以为这无关紧要,因为所有内容都在第 1 帧导入到程序中,而我在第 1000 帧之类的东西上创建实体,但是唉。总之,问题解决了!在 child 类 之前总是要求 parent 类 ... 吸取教训。 :)
我正在构建一个包含敌人和玩家的游戏,所有这些都设置为具有各种动画和行为状态。两者的父 class 都是 Entity.lua,它们从中继承变量和方法。然而,虽然敌人和玩家都在继承变量,但出于某种原因,敌人并没有继承这些方法。因此,例如,如果我尝试调用 snakey:changeState('search'),它会给我一条错误消息“尝试调用方法 'changeState'(零值)”。
我在过去的几个游戏中使用相同的顺序创建实体,但从未遇到过这个问题。事实上,如果我以与敌人相同的方式、文件和位置创建播放器,我不会收到任何错误消息。
这是我创建实体的代码。
local snakey
snakey = Snakey {
platform = platform,
player1 = self.player1,
player2 = self.player2,
stateMachine = StateMachine {
['search'] = function() return SnakeySearchState(snakey) end,
['chasing'] = function() return SnakeyChasingState(snakey) end,
['idle'] = function() return SnakeyIdleState(snakey) end
}
}
-- snakey:changeState('search')
-- snakey.stateMachine:change('search', params)
table.insert(self.entities, snakey)
这两行代码是我注意到问题的地方。第一行给出错误,第二行确实有效,但并不令人满意,因为它是一种变通方法。
下面是 Entity.lua 的代码:为简洁起见,我没有包含函数的详细信息,但是当玩家调用它们时,它们都可以正常工作。
Entity = Class{}
function Entity:init(def)
-- position
self.x = def.x
self.y = def.y
self.gravity = 6
-- many more variables
end
function Entity:changeState(state, params)
self.stateMachine:change(state)
end
function Entity:update(dt)
self.stateMachine:update(dt)
end
function Entity:collides(entity)
-- do something
end
function Entity:onDamage()
-- do something
end
function Entity:render()
- renders sprite
end
玩家代码(简要)
Player = Class{__includes = Entity}
function Player:init(def)
Entity.init(self, def)
-- more variables
end
function Player:update(dt)
Entity.update(self, dt)
end
function Player:render()
Entity.render(self)
end
也许是麻烦点,一对一敌人的剧本
Snakey = Class{__includes = Entity}
function Snakey:init(def)
Entity.init(self, def)
-- yet more variables
end
function Snakey:update(dt)
Entity.update(self, dt)
-- entity behavior (works fine, so omitted)
end
function Snakey:render()
Entity.render(self)
end
非常感谢您的帮助。我感到非常沮丧,因为这个序列在过去一直有效,我真的很想知道为什么它不调用那些实体方法。
添加 class 库
--Copyright (c) 2010-2013 Matthias Richter
local function include_helper(to, from, seen)
if from == nil then
return to
elseif type(from) ~= 'table' then
return from
elseif seen[from] then
return seen[from]
end
seen[from] = to
for k,v in pairs(from) do
k = include_helper({}, k, seen) -- keys might also be tables
if to[k] == nil then
to[k] = include_helper({}, v, seen)
end
end
return to
end
-- deeply copies `other' into `class'. keys in `other' that are already
-- defined in `class' are omitted
local function include(class, other)
return include_helper(class, other, {})
end
-- returns a deep copy of `other'
local function clone(other)
return setmetatable(include({}, other), getmetatable(other))
end
local function new(class)
-- mixins
class = class or {} -- class can be nil
local inc = class.__includes or {}
if getmetatable(inc) then inc = {inc} end
for _, other in ipairs(inc) do
if type(other) == "string" then
other = _G[other]
end
include(class, other)
end
-- class implementation
class.__index = class
class.init = class.init or class[1] or function() end
class.include = class.include or include
class.clone = class.clone or clone
-- constructor call
return setmetatable(class, {__call = function(c, ...)
local o = setmetatable({}, c)
o:init(...)
return o
end})
end
-- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).
if class_commons ~= false and not common then
common = {}
function common.class(name, prototype, parent)
return new{__includes = {prototype, parent}}
end
function common.instance(class, ...)
return class(...)
end
end
-- the module
return setmetatable({new = new, include = include, clone = clone},
{__call = function(_,...) return new(...) end})
事实证明顺序很重要。在尝试创建最小可重现代码时,我无法重现错误。经过一些搜索(有点沮丧),我注意到在 Dependencies.lua 中我需要在 Entity.lua 之前的敌人,但在 Player.lua 之后需要。我原以为这无关紧要,因为所有内容都在第 1 帧导入到程序中,而我在第 1000 帧之类的东西上创建实体,但是唉。总之,问题解决了!在 child 类 之前总是要求 parent 类 ... 吸取教训。 :)