Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
How to retrieve sprite properties through bubble events? — Gideros Forum

How to retrieve sprite properties through bubble events?

EpicJokerEpicJoker Member
edited August 2014 in General questions
Hi,

Not sure what im doing wrong here, i have a GridLetter.lua file that creates a letter object. code below:

GridLetter = gideros.class(Sprite)

local count = -1
function GridLetter:init(letter)
self.letter = 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)
self:addEventListener(Event.MOUSE_DOWN, self.onMouseUp, 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:onMouseUp(event)
if self:hitTestPoint(event.x, event.y) then
self.focus = false
self:dispatchEvent(Event.new("GridLetterUp"))
event:stopPropagation()
end
end

function GridLetter:updateVisualState(state)
if state then
self:addChild(self.image)
end
end

--------------------------------------

Then i use the GridLetter in grid.lua. code 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:addEventListener("GridLetterDown", self.LetterDown, letterBoxArr[rowCount][colCount].letterBoxItem)
letterBoxArr[rowCount][colCount].letterBoxItem:addEventListener("GridLetterUp", self.LetterUp, letterBoxArr[rowCount][colCount].letterBoxItem)
self:addChild(letterBoxArr[rowCount][colCount].letterBoxItem)
end
end
end

function grid:LetterDown(event)
print(event.letter)
end

function grid:LetterUp(event)
print(event.letter)
end

My issue is when i try and print(event.letter) it returns null yet when i use the same code in the event down in the GridLetter.lua file it prints..

What am i doing wrong?

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    letter is a property of the GridLetter
    but what you are dispatching are event object, so you need to do something, like:
    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

    Likes: EpicJoker

    +1 -1 (+1 / -0 )Share on Facebook
  • Awesome, will give it a try and let you know!

    Thank you so much!
  • self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
    self:addEventListener(Event.MOUSE_DOWN, self.onMouseUp, self)

    Must be:

    self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
    self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
  • self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
    self:addEventListener(Event.MOUSE_DOWN, self.onMouseUp, self)

    Must be:

    self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
    self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)

    Thanks for the catch!
  • letter is a property of the GridLetter
    but what you are dispatching are event object, so you need to do something, like:
    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

    Thank you so much, you are a star! :)

Sign In or Register to comment.