Gideros事件被触发后显示为空

Gideros事件被触发后显示为空

问题描述:

我在我的gideros游戏中获得了分派的事件。什么IM不能确定的是,当我尝试访问该事件(对象触发)它返回一个具有0Gideros事件被触发后显示为空

的长度我有如下的信类的表:

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) 

它打印为零。

我很感谢你的帮助。

问候, 沃伦

我已完成了..上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

感谢所有响应。

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

您遇到的错误是因为在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 
+0

所以我添加self.letter =字母的init方法。但它仍然没有数据。返回nil。奇怪的是,即使x和y坐标返回零。这可能与我如何链接事件有关吗?我应该使用不同的方式来调用事件吗?还是可以接受? – EpicJoker

+0

好吧,我已经看了一下文档。似乎这是问题:'self:dispatchEvent(Event.new(“GridLetterDown”))'没有数据传递给该事件。 “local ev = Event.new(”GridLetterDown“); ev.letter = letter; ev.x = event.x,ev.y = event.y; ev.target = self; self:dispatchEvent(ev)' 我认为这应该可行 - 但我无法确定,因为我不熟悉Gideros! :) – Advert

+0

Thanks Advert,今天我会看看你的解决方案,并回答你是否有用。感谢您迄今为止的善意帮助。 – EpicJoker