【发布时间】:2020-12-17 00:45:33
【问题描述】:
我正在构建一个包含敌人和玩家的游戏,所有这些都设置为具有不同的动画和行为状态。两者的父类都是 Entity.lua,它们从中继承变量和方法。然而,虽然敌人和玩家都继承了变量,但出于某种原因,敌人并没有继承这些方法。因此,例如,如果我尝试调用 snakey:changeState('search'),它会给我一条错误消息“尝试调用方法 'changeState' (a nil value)”。
我在过去的几款游戏中使用相同的顺序创建实体,但从未遇到过这个问题。事实上,如果我以与敌人相同的方式、文件和位置创建 Player,我不会收到任何错误消息。
这是我创建实体的代码。
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
非常感谢您的帮助。我感到非常沮丧,因为这个序列过去一直有效,我真的很想知道为什么它不调用那些 Entity 方法。
添加类库
--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})
【问题讨论】:
-
Player和Snakey似乎具有完全相同的代码。我们需要一个最小的、可重现的示例来找出问题所在。 -
错误信息是
Attempt to call method 'checkCollision' (a nil value),但是你没有告诉我们你是如何定义这个方法的。 -
Class是如何实现的?可能您的项目中有文件Class.lua。显示它。 -
感谢您的关注!我将着手组装一个 MRC,这需要几个小时。同时,这是我正在使用的 class.lua 库。
-
还进行了编辑以显示“changeState”是我收到错误消息的函数。 Entity 中的所有函数(方法)都会给出该错误,但 changeState 是我第一次注意到它的地方。谢谢!
标签: oop inheritance lua game-development love2d