触发后,Gideros事件显示为null。

我在我的 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。

提前感谢您的帮助。

问候, Warren

点赞
用户2698261
用户2698261

这并不是一个回答(我本来想评论的,但是评论需要声望)。

你得到的错误是因为在“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
2014-07-18 11:17:54
用户3852314
用户3852314

我搞定了.. Gideros 论坛上的某位帮我解决了问题,感谢 Advert。

因此,在我的 onMouseDown 事件中,我将 .letter 赋给了 general 事件。因此,创建一个本地变量并将其分配给事件,然后分派它,该变量将保留我的分配字母。

希望这可以帮助其他人!

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

感谢所有回复。

2014-11-19 14:08:26