Gideros event showing as null after being triggered

128 Views Asked by At

I have got Dispatched events in my gideros game. What im not certain of, is that when i try access the event (object triggered) it returns a table that has a length of 0.

I have a letter class as below:

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

The code that implements this class is below:

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

When i click on one of these image, the event is fired and code does run under the LetterDown event, but when trying to access the event parameter, it returns:

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>

any idea or workarounds?

When replacing:

print(event.target.letter)

to

print(event.x)

It prints nil.

I appreciate your help in advance.

Regards, Warren

2

There are 2 best solutions below

0
EpicJoker On BEST ANSWER

I have got it.. Someone on gideros forums helped me out, thanks to Advert as well.

So in my onMouseDown event i was assigning the .letter on the general event. So creating a local var and assigning it the event before dispatching it, the variable then keeps my assigned letter.

Hope this helps others in future!

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

Thanks for all responses.

4
Advert On

This isn't an answer (I'd comment, but that strangely requires rep).

The error you're getting is because in event, there's nothing in the target field.

You could find out what's in the table by looping over it (or by using a pretty table print function, if glideros defines one/you define one yourself):

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

If you're not sure if target will exist all the time, you can also do something like this:

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

You can read about the A and B or C construct here: http://lua-users.org/wiki/TernaryOperator

Edit: Assuming that the event you receive fires with a GridLetter object, you are not setting self.letter to anything. Perhaps setting the field will help:

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