问题描述
我正在开发游戏.在某个时候,我想创建特殊的GameObject.特殊的GameObject称为Projectile,与GameObject相同,但具有dx和dy以及只有射弹才具有的其他一些功能.我试图使Projectile扩展GameObject类,但是在尝试创建Projectile实例时遇到问题.我尝试了不同的方式来声明Projectile和改组声明顺序,但是似乎无法弄清楚为什么我得到标题中提到的错误.谢谢!
I'm working on a game. At a certain point, I would like to create special GameObject. The special GameObject is called Projectile and is the same as GameObject but has a dx and dy as well as some other functions that only a projectile will have. I am trying to make Projectile extend the GameObject class, but I run into issues when I try to create an instance of Projectile. I've tried different ways of declaring Projectile and shuffling declaration order but can't seem to figure out why I'm getting the error mentioned in the title. Thank you!
以下工作正常:
table.insert(self.dungeon.currentRoom.objects, GameObject(
GAME_OBJECT_DEFS['pot'],
self.player.x,
self.player.y
))
但是当我将"GameObject"更改为"Projectile"时,不会.
But when I change "GameObject" to "Projectile" it does not.
table.insert(self.dungeon.currentRoom.objects, Projectile(
GAME_OBJECT_DEFS['pot'],
self.player.x,
self.player.y
))
其余为支持代码.我正在使用Matthias Richter的课程代码
The rest is supporting code. I'm using Class code from 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
}
}
推荐答案
function Projectile:init()
GameObject.init(self, def)
self.dx = 0
self.dy = 0
end
def
为 nil
如此
function GameObject:init(def, x, y)
-- string identifying this object type
self.type = def.type
您为nil值编制索引.
you index a nil value.
x
和 y
这篇关于尝试索引本地"def"(nil值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!