unpack(self.fillColor) 无论我尝试什么都会以 "nil" 的形式返回?
unpack(self.fillColor) coming back as "nil" no matter what I try?
首先 post 在这里,但长期使用其他人的解决方案。我这学期报名参加了 Lua 编程课程,我们正在进行第一个作业。我的问题可能很容易回答,但对于从未使用过 Lua 这样的语言的人来说,这让我感到困惑!
游戏的基础是创建一个彩色方块板,让用户触摸它们 select。触摸时,正方形变大并保持更大。然后,如果下一个触摸的颜色与第一个匹配,则它们将 disappear/delete 自己,然后一旦所有方块都消失,游戏将重新开始。这是代码,删除了不相关的位。
-- Get screen dimensions in easier to reference variables
maxXValue = display.contentWidth;
maxYValue = display.contentHeight;
-- Set up table of boxes to play game with
-- May have a better way to do this...
gameBoard = {}
for i = 1, 16 do
if i == 1 then boxNum = display.newRect(0, 0, (0.25*maxXValue-5),
(0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 2 then boxNum = display.newRect((0.25*maxXValue), 0,
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 3 then boxNum = display.newRect((0.5*maxXValue), 0,
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 4 then boxNum = display.newRect((0.75*maxXValue), 0,
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 5 then boxNum = display.newRect(0, (0.25*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 6 then boxNum = display.newRect((0.25*maxXValue), (0.25*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 7 then boxNum = display.newRect((0.5*maxXValue), (0.25*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 8 then boxNum = display.newRect((0.75*maxXValue), ((0.25*maxYValue)),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 9 then boxNum = display.newRect(0, (0.5*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 10 then boxNum = display.newRect((0.25*maxXValue), (0.5*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 11 then boxNum = display.newRect((0.5*maxXValue), (0.5*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 12 then boxNum = display.newRect((0.75*maxXValue), (0.5*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 13 then boxNum = display.newRect(0, (0.75*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 14 then boxNum = display.newRect((0.25*maxXValue), (0.75*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 15 then boxNum = display.newRect((0.5*maxXValue), (0.75*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 16 then boxNum = display.newRect((0.75*maxXValue), (0.75*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
end
end
--Irrelevant code here
local selected = {};
local function toucher (event)
local red, green, blue = unpack(self.fillColor); -- ERROR APPEARS HERE
table.insert(selected, self, red, green, blue)
event.target:scale(1.1, 1.1)
event.target:toFront();
if selected[2] == event.target then
if (selected[1].red == selected[2].red and selected[1].green == selected[2].green
and selected[1].blue == selected[2].blue) then
selected[1]:removeSelf();
selected[2]:removeSelf();
end
selected[1] = selected[2];
selected[2] = nil;
end
--[[Code to go here will tell the program to restart. Want to get a single iteration before
moving to this.]]
end
for i = 1, 16 do gameBoard[i]:addEventListener("touch", toucher);
end
在 "toucher" 函数中,我尝试使用 unpack(self.fillColor) 函数来获取 selected 的方块的颜色,以便稍后保存以供比较。根据我的研究,这是从对象中读取颜色的更简单方法之一。但是,当用户触摸正方形时,出现以下错误:
main.lua:121:尝试索引全局 'self'(零值)
堆栈回溯:
main.lua:121:在函数中 < main.lua:120 >
?:在函数中 < ?:169 >
我已经阅读了关于 self 和 event.target 对象如何工作的信息,但是当我试图取消对它们的引用而不是它们的名称(打印 "nil")时,它们都给出了相同的错误。我觉得我可能错误地理解了一个非常简单的范围界定概念。其他使用 event.target 和 self 的操作,如 resize 和 toFront() 操作,工作正常所以我不太明白为什么获取对象的属性不起作用。
我知道我的代码不漂亮或效率不高,但我真的只担心让它工作。作业将于本周到期,几天来我一直在尝试自己解决这个问题。提前致谢!
该函数中没有对象 self
。所有事件侦听器都有一个事件参数,它有一个 target
属性,它指的是调度事件的对象。 More info here.
在您的情况下,您缺少一行来指定 self
,例如:
local function toucher (event)
local self = event.target -- This line is missing.
local red, green, blue = unpack(self.fillColor)
从没听说过 .fillColor
属性,不过,所以我猜你是自己指定的,如果不是,请包装函数 setFillColor
以保存颜色,就像这样:
function gameBoard[1]:saveFillColor(r, g, b, a) -- New function that saves the color and sets it.
self.fillColor = {r, g, b, a}
self:setFillColor(r, g, b, a)
end
self
仅在函数作为对象的一部分被调用时可用,例如 gameBoard[1]:toucher()
和 :
您可以阅读有关此 here 的更多信息。
首先 post 在这里,但长期使用其他人的解决方案。我这学期报名参加了 Lua 编程课程,我们正在进行第一个作业。我的问题可能很容易回答,但对于从未使用过 Lua 这样的语言的人来说,这让我感到困惑!
游戏的基础是创建一个彩色方块板,让用户触摸它们 select。触摸时,正方形变大并保持更大。然后,如果下一个触摸的颜色与第一个匹配,则它们将 disappear/delete 自己,然后一旦所有方块都消失,游戏将重新开始。这是代码,删除了不相关的位。
-- Get screen dimensions in easier to reference variables
maxXValue = display.contentWidth;
maxYValue = display.contentHeight;
-- Set up table of boxes to play game with
-- May have a better way to do this...
gameBoard = {}
for i = 1, 16 do
if i == 1 then boxNum = display.newRect(0, 0, (0.25*maxXValue-5),
(0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 2 then boxNum = display.newRect((0.25*maxXValue), 0,
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 3 then boxNum = display.newRect((0.5*maxXValue), 0,
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 4 then boxNum = display.newRect((0.75*maxXValue), 0,
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 5 then boxNum = display.newRect(0, (0.25*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 6 then boxNum = display.newRect((0.25*maxXValue), (0.25*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 7 then boxNum = display.newRect((0.5*maxXValue), (0.25*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 8 then boxNum = display.newRect((0.75*maxXValue), ((0.25*maxYValue)),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 9 then boxNum = display.newRect(0, (0.5*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 10 then boxNum = display.newRect((0.25*maxXValue), (0.5*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 11 then boxNum = display.newRect((0.5*maxXValue), (0.5*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 12 then boxNum = display.newRect((0.75*maxXValue), (0.5*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 13 then boxNum = display.newRect(0, (0.75*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 14 then boxNum = display.newRect((0.25*maxXValue), (0.75*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 15 then boxNum = display.newRect((0.5*maxXValue), (0.75*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
elseif i == 16 then boxNum = display.newRect((0.75*maxXValue), (0.75*maxYValue),
(0.25*maxXValue-5), (0.25*maxYValue-5));
table.insert(gameBoard, boxNum);
end
end
--Irrelevant code here
local selected = {};
local function toucher (event)
local red, green, blue = unpack(self.fillColor); -- ERROR APPEARS HERE
table.insert(selected, self, red, green, blue)
event.target:scale(1.1, 1.1)
event.target:toFront();
if selected[2] == event.target then
if (selected[1].red == selected[2].red and selected[1].green == selected[2].green
and selected[1].blue == selected[2].blue) then
selected[1]:removeSelf();
selected[2]:removeSelf();
end
selected[1] = selected[2];
selected[2] = nil;
end
--[[Code to go here will tell the program to restart. Want to get a single iteration before
moving to this.]]
end
for i = 1, 16 do gameBoard[i]:addEventListener("touch", toucher);
end
在 "toucher" 函数中,我尝试使用 unpack(self.fillColor) 函数来获取 selected 的方块的颜色,以便稍后保存以供比较。根据我的研究,这是从对象中读取颜色的更简单方法之一。但是,当用户触摸正方形时,出现以下错误:
main.lua:121:尝试索引全局 'self'(零值) 堆栈回溯: main.lua:121:在函数中 < main.lua:120 > ?:在函数中 < ?:169 >
我已经阅读了关于 self 和 event.target 对象如何工作的信息,但是当我试图取消对它们的引用而不是它们的名称(打印 "nil")时,它们都给出了相同的错误。我觉得我可能错误地理解了一个非常简单的范围界定概念。其他使用 event.target 和 self 的操作,如 resize 和 toFront() 操作,工作正常所以我不太明白为什么获取对象的属性不起作用。
我知道我的代码不漂亮或效率不高,但我真的只担心让它工作。作业将于本周到期,几天来我一直在尝试自己解决这个问题。提前致谢!
该函数中没有对象 self
。所有事件侦听器都有一个事件参数,它有一个 target
属性,它指的是调度事件的对象。 More info here.
在您的情况下,您缺少一行来指定 self
,例如:
local function toucher (event)
local self = event.target -- This line is missing.
local red, green, blue = unpack(self.fillColor)
从没听说过 .fillColor
属性,不过,所以我猜你是自己指定的,如果不是,请包装函数 setFillColor
以保存颜色,就像这样:
function gameBoard[1]:saveFillColor(r, g, b, a) -- New function that saves the color and sets it.
self.fillColor = {r, g, b, a}
self:setFillColor(r, g, b, a)
end
self
仅在函数作为对象的一部分被调用时可用,例如 gameBoard[1]:toucher()
和 :
您可以阅读有关此 here 的更多信息。