简体   繁体   English

尝试索引本地“def”(零值)

[英]attempt to index local 'def' (a nil value)

I'm working on a game.我正在开发一款游戏。 At a certain point, I would like to create special GameObject.在某个时候,我想创建特殊的 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.特殊的 GameObject 称为 Projectile,与 GameObject 相同,但具有 dx 和 dy 以及一些只有射弹才有的其他功能。 I am trying to make Projectile extend the GameObject class, but I run into issues when I try to create an instance of Projectile.我正在尝试让 Projectile 扩展 GameObject class,但是当我尝试创建 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.我尝试了不同的声明 Projectile 和改组声明顺序的方法,但似乎无法弄清楚为什么我会收到标题中提到的错误。 Thank you!谢谢!

The following works just fine:以下工作正常:

table.insert(self.dungeon.currentRoom.objects, GameObject(
                        GAME_OBJECT_DEFS['pot'],
                        self.player.x,
                        self.player.y
                    ))

But when I change "GameObject" to "Projectile" it does not.但是当我将“GameObject”更改为“Projectile”时,它不会。

table.insert(self.dungeon.currentRoom.objects, Projectile(
                        GAME_OBJECT_DEFS['pot'],
                        self.player.x,
                        self.player.y
                    ))

The rest is supporting code. rest 是支持代码。 I'm using Class code from Matthias Richter我正在使用来自 Matthias Richter 的 Class 代码

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 is nil def nil

so in所以在

function GameObject:init(def, x, y)
    -- string identifying this object type
    self.type = def.type

you index a nil value.你索引一个零值。

Same will happen for x and y xy也会发生同样的情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM