unpack(self.fillColor)作为" nil"无论我尝试什么?

时间:2016-09-11 23:14:29

标签: lua corona

首先发布此帖,但其他人的长期用户'解决方案。本学期我已经报名参加了Lua编程课程,我们正在完成第一项任务。我的问题可能很容易回答,但对于那些从未使用像Lua这样的语言工作过的人来说,它正在推动我的问题!

游戏的基础是创建一个彩色方块板,让用户触摸它们来选择它们。在触摸时,方块变得更大并且保持更大。然后,如果下一个被触摸的颜色与第一个匹配,则它们将自动消失/删除,然后一旦所有正方形消失,游戏将重新开始。这是代码,删除了不相关的位。

-- 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)函数来获取选中的方块的颜色,以便以后进行比较。根据我的研究,这是从对象中读取颜色的更简单方法之一。但是,当用户触摸方格时,我收到以下错误:

main.lua:121:尝试索引全球' self' (零值) 堆栈追溯:  main.lua:121:in function< main.lua:120>  ?:在功能< ?:169>

我已经了解了self和event.target对象是如何工作的,但是当我尝试将它们取消引用除了它们的名字之外的任何东西时都会给出相同的错误(打印" nil") 。我觉得我可能错误地理解了一个非常简单的范围概念。使用event.target和self的其他操作(如resize和toFront()操作)工作正常,所以我不太明白为什么获取对象的属性不起作用。

我知道我的代码不是很优秀或超级高效,但我真的只是担心让它运行起来。这项任务将在本周到期,而且我已经在几天内试图自行解决这个问题了。提前谢谢!

1 个答案:

答案 0 :(得分:0)

该功能中没有对象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的更多信息。