我的 gideros 游戏中有 Dispatched 事件。我不确定的是,当我尝试访问事件(对象触发)时,它返回一个长度为 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)
它打印为零。
我提前感谢您的帮助。
问候,沃伦