如何解决实体继承Entity.lua变量,但不继承其方法
我正在与敌人和玩家一起制作游戏,所有这些游戏都设置为具有各种动画和行为状态。两者的父类都是Entity.lua,它们从中继承变量和方法。但是,当敌人和玩家都继承变量时,由于某种原因,敌人不继承方法。因此,例如,如果我尝试调用snakey:changeState('search'),它会给我一个错误消息“试图调用方法'changeState'(nil值)”。
过去,我在多个游戏中使用相同的顺序来创建实体,但从未遇到过这个问题。实际上,如果我以与敌人相同的方式,文件和位置创建播放器,则不会收到任何错误消息。
这是我创建实体的代码。
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})
解决方法
事实证明,顺序很重要。在尝试创建最小的可复制代码时,我无法重现该错误。经过一番搜索(有些沮丧),我注意到在Dependencies.lua中,我需要在Entity.lua之前的敌人,但之后需要Player.lua。我会以为这没关系,因为所有内容都在第1帧导入到程序中,并且我在第1000帧之类的东西上创建实体,但是entities。无论如何,问题解决了!始终在孩子班级之前要求家长班级。 :)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。