如何在 Lua 中定义多分量变量(使用 Solar 2D)

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

我一直在尝试 lua 并尝试在屏幕上绘制一个矩形

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
-- Variable definition:
    local rect_shape
    
-- Initialization
    rect_shape.w = 20
    rect_shape.h = 40
    rect_shape.x = (display.contentWidth/2)-(rect_shape.w/2)
    rect_shape.y = (display.contentHeight/2)-(rect_shape.h/2)
    rect_shape = display.newRect(rect_shape.x, rect_shape.y, rect_shape.w, rect_shape.h)
    rect_shape:setFillColor(49,49,49)

    print("value of rect_shape:", rect_shape, "value of w:", rect_shape.w, "value of h:", rect_shape.h, "value of x:", rect_shape.x, "value of y:", rect_shape.y)

然而,当我尝试运行这个脚本时,

'main.lua:10: attempt to index local 'rect_enemy' (a nil value)'

被退回。 所以不知何故,我似乎需要在定义矩形本身之前定义矩形的组件,因为你不能访问一个空的矩形?

有人可以解释并帮助我解决这个问题吗?

lua coronasdk solar2d
1个回答
0
投票

你完全用...重新定义了

rect_shape

rect_shape = display.newRect(rect_shape.x, rect_shape.y, rect_shape.w, rect_shape.h)

我的感觉应该是……

local rect_enemy = display.newRect(rect_shape.x, rect_shape.y, rect_shape.w, rect_shape.h)
© www.soinside.com 2019 - 2024. All rights reserved.