Gideros事件在被触发后显示为null

时间:2014-07-18 09:23:16

标签: mobile lua gideros

我的gideros游戏中有调度事件。我不确定的是,当我尝试访问事件(对象触发)时,它返回一个长度为0的表。

我的信件类如下:

GridLetter = gideros.class(Sprite)

function GridLetter:init(letter)

    self.image = Bitmap.new(Texture.new("GridLetters/"..letter.."Grid.png"))
    self.image:setAnchorPoint(0.5, 0.5)
    self.focus = false
    self:updateVisualState(true)
    self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
end

function GridLetter:onMouseDown(event)
    if self:hitTestPoint(event.x, event.y) then
        self.focus = true
        self:dispatchEvent(Event.new("GridLetterDown"))
        event:stopPropagation()
    end
end

function GridLetter:updateVisualState(state)
    if state then
        self:addChild(self.image)
    end
end

实现此类的代码如下:

grid = Core.class(Sprite)

local letterArr = {"a","b","c","d","e","f","g","h","u","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}
local gridboard = {{},{},{},{}}

local letterBoxArr = {{},{},{},{}}
local letterBox

function grid:init(params)

    local widthOfSingle = (application:getContentWidth() / 4)
    for rowCount = 1,4 do
        for colCount = 1,4 do
            rand = math.random(1, 26)
            gridboard[rowCount][colCount] = letterArr[rand]
        end
    end

    for rowCount = 1,4 do
        for colCount = 1,4 do
            letterBox = GridLetter.new(gridboard[rowCount][colCount])
            letterBoxArr[rowCount][colCount] = {letterBoxItem=letterBox}
            letterBoxArr[rowCount][colCount].letterBoxItem:setPosition(((widthOfSingle * colCount) - (widthOfSingle / 2)), 100 * rowCount)
            letterBoxArr[rowCount][colCount].letterBoxItem.letter = gridboard[rowCount][colCount];
            letterBoxArr[rowCount][colCount].letterBoxItem:addEventListener("GridLetterDown", LetterDown, self)
            self:addChild(letterBoxArr[rowCount][colCount].letterBoxItem)
        end
    end
end

function LetterDown(event)
    print(event.target.letter)
end

当我点击其中一个图像时,事件被触发并且代码在LetterDown事件下运行,但是当尝试访问事件参数时,它返回:

grid.lua:32: attempt to index field 'target' (a nil value)
stack traceback:
    grid.lua:32: in function <grid.lua:31>
    GridLetter.lua:15: in function <GridLetter.lua:12>

任何想法或解决方法?

更换时:

print(event.target.letter)

print(event.x)

打印nil。

我提前感谢您的帮助。

此致 沃伦

2 个答案:

答案 0 :(得分:1)

这不是答案(我发表评论,但奇怪的是需要代表)。

您收到的错误是因为在event中,target字段中没有任何内容。

你可以通过循环来找出表中的内容(或者使用漂亮的表打印函数,如果glideros定义了一个/你自己定义一个):

for k, v in pairs(target) do print(k, v) end

如果您不确定target是否一直存在,您也可以这样做:

print(not event.target and "event.target does not exist" or event.target.letter)

您可以在此处阅读A and B or C构造:http://lua-users.org/wiki/TernaryOperator

编辑:假设您收到的事件是使用GridLetter对象触发的,那么您不会将self.letter设置为任何内容。也许设置该字段将有所帮助:

function GridLetter:init(letter)
    self.letter = letter -- Forgot this?
    self.image = Bitmap.new(Texture.new("GridLetters/"..letter.."Grid.png"))
    self.image:setAnchorPoint(0.5, 0.5)
    self.focus = false
    self:updateVisualState(true)
    self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
end

答案 1 :(得分:1)

我已经得到了..有人在gideros论坛上帮助了我,多亏了广告。

所以在我的onMouseDown事件中,我正在为一般事件分配.letter。因此,创建一个本地var并在调度之前为其分配事件,然后该变量保留我指定的字母。

希望将来能帮助别人!

function GridLetter:onMouseDown(event) if self:hitTestPoint(event.x, event.y) then self.focus = true local e = Event.new("GridLetterDown") e.letter = self.letter self:dispatchEvent(e) event:stopPropagation() end end

感谢所有回复。