Class 尝试索引本地';def&x27;(零值)
我正在做一个游戏。在某一点上,我想创建一个特殊的游戏对象。特殊的游戏对象称为射弹,与游戏对象相同,但具有dx和dy以及一些只有射弹才具有的其他功能。我试图让Sproject扩展GameObject类,但在尝试创建Sproject实例时遇到了问题。我尝试了不同的方法来声明抛射和混乱声明顺序,但似乎无法理解为什么我会遇到标题中提到的错误。谢谢大家! 以下几点效果不错:Class 尝试索引本地';def&x27;(零值),class,inheritance,lua,love2d,Class,Inheritance,Lua,Love2d,我正在做一个游戏。在某一点上,我想创建一个特殊的游戏对象。特殊的游戏对象称为射弹,与游戏对象相同,但具有dx和dy以及一些只有射弹才具有的其他功能。我试图让Sproject扩展GameObject类,但在尝试创建Sproject实例时遇到了问题。我尝试了不同的方法来声明抛射和混乱声明顺序,但似乎无法理解为什么我会遇到标题中提到的错误。谢谢大家! 以下几点效果不错: table.insert(self.dungeon.currentRoom.objects, GameObject(
table.insert(self.dungeon.currentRoom.objects, GameObject(
GAME_OBJECT_DEFS['pot'],
self.player.x,
self.player.y
))
但当我将“游戏对象”改为“投射物”时,它不会
table.insert(self.dungeon.currentRoom.objects, Projectile(
GAME_OBJECT_DEFS['pot'],
self.player.x,
self.player.y
))
其余部分是支持代码。我用的是Matthias Richter的类代码
require 'src/Projectile'
require 'src/GameObject'
require 'src/game_objects'
GameObject = Class{}
function GameObject:init(def, x, y)
-- string identifying this object type
self.type = def.type
self.texture = def.texture
self.frame = def.frame or 1
-- whether it acts as an obstacle or not
self.solid = def.solid
self.defaultState = def.defaultState
self.state = self.defaultState
self.states = def.states
-- dimensions
self.x = x
self.y = y
self.width = def.width
self.height = def.height
-- default empty collision callback
self.onCollide = def.onCollide
end
Projectile = Class{__includes = GameObject}
function Projectile:init()
GameObject.init(self, def)
self.dx = 0
self.dy = 0
end
GAME_OBJECT_DEFS = {
['pot'] = {
type = 'pot',
texture = 'tiles',
frame = 14,
width = 16,
height = 16,
solid = true,
defaultState = 'idle',
states = {
['idle'] = {
frame = 14,
}
},
onCollide = function()
end
}
}
def
为nil
所以在
索引一个nil值
同样的情况也会发生在
x
和y@kleary上,因此您应该使用与射弹:init
相同的定义来使其工作:函数射弹:init(def,x,y)
谢谢大家。非常感谢。
function Projectile:init()
GameObject.init(self, def)
self.dx = 0
self.dy = 0
end
function GameObject:init(def, x, y)
-- string identifying this object type
self.type = def.type