Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Event for Images — Gideros Forum

Event for Images

DoradoLabDoradoLab Member
edited April 2014 in General questions
Hi,

I am new to Lua, Can you tell me how can i use Event on mouse Click on particular images of buttons where it is added to a Container . Please Edit my following code and guide me.


function CommonAssets:addInventory(parent)

print("Adding the Inventory")

local group = Container.new()
self.inventoryJoinBtnDown = Bitmap.new(Texture.new("images/CommonAssets/JoinButtonDown.png", conf.textureFilter))
group:addChild(self.inventoryJoinBtnDown)
AssetsHelper:setPos(self.inventoryJoinBtnDown,130,120)

group:addEventListener("click", function()
sounds:play("button_click")
self:clickedInventoryButton()
end)

parent:addChild(group)
end

function CommonAssets:clickedInventoryButton()
print("Clicked")
end


No errors but event not triggering. Please help me.


Thanks

Dorado Lab

Comments

  • Hi @DoradoLab
    I do not know if I've understood the problem but I think this can help you

    first you change:
    local group = Container.new()     by     self.group = Container.new()
    second:
    function CommonAssets:clickedInventoryButton(event)
       for k=1,  self.group:getNumChildren() do   -- in this case only there is one child
    	if self:getChildAt(k):hitTestPoint(event.x, event.y) then
            print("Clicked")
            event:stopPropagation()
       end
    end
    I hope I've helped you
  • Sorry one mistake (I forgot "end" of "if ") :P
    if self:getChildAt(k):hitTestPoint(event.x, event.y) then
            print("Clicked")
            event:stopPropagation()
    end
  • Also you can create a class for inventory like this:
    inventory = Core.class(Sprite)
     
    function inventory:ini(param)
             self.image =  Bitmap.new(Texture.new(param.dir, param.textureFilter))
             self.image:setAnchorPoint(param.Ax,param.Ay)
             -- and so on
             self.focus = false
             self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
             self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
    end
     
    function inventory:onMouseDown(event)
    	if self:hitTestPoint(event.x, event.y) then
    		self.focus = true
    		event:stopPropagation()
    	end
    end
     
    function inventory:onMouseUp(event)
    	if self.focus then
    		self.focus = false
    		self:dispatchEvent(Event.new("clickInventory")) -- image is clicked, dispatch "click" event
    		event:stopPropagation()
    	end
    end
    After only you have than do:
    local param={}
    param.image = "images/CommonAssets/JoinButtonDown.png"
    -- and so on
    self.inventoryJoinBtnDown = inventory.new(param)
    self.group:addChild(self.inventoryJoinBtnDown)
    self.inventoryJoinBtnDown:addEventListener("clickInventory", function() print("clicked") end)
    I would be inclined for the latter as it allows me to reuse the code
Sign In or Register to comment.