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

Mobile Gideros事件在触发后显示为null,mobile,lua,gideros,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.

我已经在我的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>

它打印的是零

我提前感谢你的帮助

问候,, 沃伦

这不是一个答案(我想评论一下,但奇怪的是这需要代表)

您得到的错误是因为在
事件中,
目标
字段中没有任何内容

您可以通过在表上循环找到表中的内容(或者如果glideros定义了一个/您自己定义了一个,则可以使用漂亮的表打印功能):

如果不确定
target
是否始终存在,也可以执行以下操作:

print(not event.target and "event.target does not exist" or event.target.letter)
您可以在此处阅读
A和B或C
构造:

编辑:假设您收到的事件使用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

我明白了。。吉德罗斯论坛上有人帮了我,这也要感谢广告

所以在我的onMouseDown事件中,我分配了关于一般事件的.letter。因此,创建一个局部变量,并在分派它之前将事件分配给它,然后该变量保留我分配的字母

希望这对以后的人有帮助

函数GridLetter:onMouseDown(事件)
如果self:hitTestPoint(event.x,event.y),那么
self.focus=true
本地e=事件。新建(“GridLetterDown”)
e、 字母
self:dispatchEvent(e)
事件:stopPropagation()
结束
结束


感谢所有回复。

因此我在init方法中添加了self.letter=letter。但它仍然没有数据。返回零。奇怪的是,即使是x和y坐标也返回nil。这与我如何将事件联系起来有关吗?我应该使用不同的方式来调用事件,还是可以接受?好的,我已经看了一些文档。似乎这就是问题所在:
self:dispatchEvent(Event.new(“GridLetterDown”)
没有向该事件传递数据。类似于
localev=Event.new(“GridLetterDown”);字母=字母;ev.x=事件x,ev.y=事件y;ev.目标=自我;self:dispatchEvent(ev)
我认为这应该可以实现——但我不能肯定,因为我不熟悉Gideros!)谢谢你的广告,我今天会看看你的解决方案,然后再回复你它是否有效。谢谢你们到目前为止的善意帮助。我会把你们的广告标为答案,但这只是一个评论。谢谢你们,你们是正确的!
for k, v in pairs(target) do print(k, v) end
print(not event.target and "event.target does not exist" or event.target.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