试图索引本地'def'(nil值)

问题描述 投票:1回答:1

我正在开发游戏。在某个时候,我想创建特殊的GameObject。特殊的GameObject称为Projectile,与GameObject相同,但具有dx和dy以及仅射弹具有的其他一些功能。我试图使Projectile扩展GameObject类,但是在尝试创建Projectile实例时遇到问题。我尝试了不同的方式来声明Projectile和改组声明顺序,但似乎无法弄清楚为什么我得到标题中提到的错误。谢谢!

以下功能很好:

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

但是当我将“ GameObject”更改为“ Projectile”时,则不会。

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
    }
}
class inheritance lua love2d
1个回答
2
投票
function Projectile:init()
    GameObject.init(self, def)

    self.dx = 0
    self.dy = 0
end

[defnil

如此

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

您索引一个零值。

xy也会发生相同>

© www.soinside.com 2019 - 2024. All rights reserved.