首页 > 解决方案 > 尝试在 Lua 中对字段“x”(表值)执行算术 - 不确定如何解决

问题描述

在 Lua 中工作并尝试将“弹丸”渲染到屏幕上。我的弹丸类:

Projectile = Class{}

function Projectile:init(x, y)
    self.x      = x
    self.y      = y
    self.dx     = math.random(-50, 50)
    self.dy     = math.random(-50, 50)
end

function Projectile:update(dt)

    self.x = self.x + self.dx * dt
    self.y = self.y + self.dy * dt

end

function Projectile:render()
    love.graphics.rectangle('fill', self.x, self.y, 4, 4)
end

当我的实体进入“警报”状态时,我希望该实体产生射弹,所以在 EntityAlertPhase 状态下我有这个:

EntityAlertState = Class{__includes = BaseState}

function EntityAlertState:init(entity)
    self.entity = entity
    self.projectile = self:generateProjectiles()

generateProjectiles 函数调用:

function EntityAlertState:generateProjectiles()
    projectile = Projectile {
        x = self.entity.x, 
        y = self.entity.y
    }
    return projectile
end

但是,此代码(我省略了其他看起来不相关的代码,但如果我应该包含它,请 lmk)导致名义上的错误。

当我替换这个时:

function EntityAlertState:generateProjectiles()
    projectile = Projectile {
        x = self.entity.x, 
        y = self.entity.y
    }
    return projectile
end

有了这个:

function EntityAlertState:generateProjectiles()
    projectile = Projectile {
        x = 50, 
        y = 50
    }
    return projectile
end

我仍然收到错误消息。我相信原因是在 Projectile 的 init 函数中的某个地方:

function Projectile:init(x, y)
    self.x      = x
    self.y      = y
    self.dx     = math.random(-50, 50)
    self.dy     = math.random(-50, 50)
end

因为当我将 x 和 y 替换为整数时,我不再收到错误并且一切都按预期工作(当然我需要这些对实体 x 和 y 的引用,这样我就可以控制射弹的去向,所以这不会t帮助我:()

任何回复都会非常有帮助——这也是我的第一篇文章,所以我对我不遵守的任何礼仪感到抱歉!

更新:所以,在弹丸初始化函数中,如果我这样做:

function Projectile:init(x, y)
    self.x      = x.x
    self.y      = y
    self.dx     = math.random(-50, 50)
    self.dy     = math.random(-50, 50)
end

而不仅仅是 x? 我不再收到错误消息(假设我将 y 分给某个整数值)。由于某种原因,y(甚至 yy)不起作用......而且我不太确定如何调试传递给 init 的值。

更新2:

由于似乎错误可能与类库有关,因此我在此处包含了该代码:

--[[
Copyright (c) 2010-2013 Matthias Richter

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

Except as contained in this notice, the name(s) of the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--

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})

标签: lua

解决方案


例如,当您调用Projectile(a, b, c)时,它会创建一个表,将其与 Projectile 类相关联,然后调用newProjectile:init(a, b, c)- 它会将相同的参数init传递给您传递给Projectile.

当您调用 时Projectile{x = 50, y = 50},您使用一个参数调用它,即表格。因此,您的init函数有一个参数,即表格。


推荐阅读