Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Adding Event Listeners with a for loop — Gideros Forum

Adding Event Listeners with a for loop

NinjadoodleNinjadoodle Member
edited January 2014 in General questions
Hi guys

I have a number of bitmaps declared like this …

local segmentA0 = Bitmap.new(Texture.new("images/heartOfTheDiamondPropDiamondSegmentA.png", true))
segmentA0:setAnchorPoint(0.5, 0.5)
local segmentA1 = Bitmap.new(Texture.new("images/heartOfTheDiamondPropDiamondSegmentA.png", true))
segmentA1:setAnchorPoint(0.5, 0.5)
etc …

and adding listeners in the same manner …

segmentA0:addEventListener(Event.MOUSE_UP, rotateSegment, segmentA0)
segmentA1:addEventListener(Event.MOUSE_UP, rotateSegment, segmentA1)
etc …

I'm trying to add Event Listeners to these images using a for loop but can't seem to work out the syntax :(

If anyone could help that would be awesome :)

Thank you in advance!

Comments

  • this is the what i am doing if you can post full code i can chk what is causing the problem
    myImageList = {"img1.png","img2.png","img3.png"}
    for i=1,#myImageList do
     
    			local myImg = Bitmap.new(Texture.new(myImageList[i]))
    			self:addChild(myImg)
    			myImg:addEventListener(Event.MOUSE_DOWN,onBtnDown,myImg)
    			myImg:addEventListener(Event.MOUSE_MOVE,onBtnMove,myImg)
    			myImg:addEventListener(Event.MOUSE_UP,onBtnUp,myImg)
    end
  • NinjadoodleNinjadoodle Member
    edited January 2014
    @hgvyas123 Thanks for you help!

    The problem is that I'm also adding the images to a group which makes it more complex.

    In your example, I don't understand how you would reference the images you create, if you wanted to change any properties at a later time.

    Also, I'd love to consolidate my code a little bit if possible :) I'm sure there is a shorter way to write this up - any help would be awesome!

    ************************************

    -- CREATE/ADD SEGMENT IMAGES
    local segmentA0 = Bitmap.new(Texture.new("images/heartOfTheDiamondPropDiamondSegmentA.png", true))
    segmentA0:setAnchorPoint(0.5, 0.5)
    local segmentB0 = Bitmap.new(Texture.new("images/heartOfTheDiamondPropDiamondSegmentB.png", true))
    segmentB0:setAnchorPoint(0.5, 0.5)
    local segmentC0 = Bitmap.new(Texture.new("images/heartOfTheDiamondPropDiamondSegmentC.png", true))
    segmentC0:setAnchorPoint(0.5, 0.5)
    local segmentD0 = Bitmap.new(Texture.new("images/heartOfTheDiamondPropDiamondSegmentD.png", true))
    segmentD0:setAnchorPoint(0.5, 0.5)

    -- CREATE/ADD DIAMOND-A GROUP
    local diamondA = Sprite.new()
    self:addChild(diamondA)
    diamondA:setPosition(240,96)

    -- ADD SEGMENTS TO GROUP
    diamondA:addChild(segmentA0)
    segmentA0:setPosition(0,-32)
    segmentA0:setRotation(45)
    segmentA0:setSize(64,64)
    diamondA:addChild(segmentB0)
    segmentB0:setPosition(32,0)
    segmentB0:setRotation(45)
    segmentB0:setSize(64,64)
    diamondA:addChild(segmentC0)
    segmentC0:setPosition(0,32)
    segmentC0:setRotation(45)
    segmentC0:setSize(64,64)
    diamondA:addChild(segmentD0)
    segmentD0:setPosition(-32,0)
    segmentD0:setRotation(45)
    segmentD0:setSize(64,64)

    -- ROTATE SEGMENTS
    local function rotateSegment(target, event)
    if target:hitTestPoint(event.x, event.y) then
    if (not target.isTweening) then
    clickWav:play()
    target.isTweening = true
    segmentTween = GTween.new(target, .25, {rotation = target:getRotation()+90}, {ease = easing.outBack})
    segmentTween.onComplete = function ()
    target.isTweening = false;
    end
    end
    end
    end

    segmentA0:addEventListener(Event.MOUSE_UP, rotateSegment, segmentA0)
    segmentB0:addEventListener(Event.MOUSE_UP, rotateSegment, segmentB0)
    segmentC0:addEventListener(Event.MOUSE_UP, rotateSegment, segmentC0)
    segmentD0:addEventListener(Event.MOUSE_UP, rotateSegment, segmentD0)
  • hi

    in this case what you are doing is fine in the case you wanted to use the for loop then add all the sprites in one array and iterate through it to use for loop but according to me it is fine currently
  • ar2rsawseenar2rsawseen Maintainer
    edited January 2014 Accepted Answer
    How about functions?
    function processSegment(image, parent, x, y)
        local segment = Bitmap.new(Texture.new(image, true))
        segment:setAnchorPoint(0.5, 0.5)
        parent:addChild(segment)
        segment:setPosition(x, y)
        segment:setRotation(45)
        segment:setSize(64,64)
        return segment
    end
     
    -- CREATE/ADD DIAMOND-A GROUP
    local diamondA = Sprite.new()
    self:addChild(diamondA)
    diamondA:setPosition(240,96)
     
    --process segments
    local segmentA0 = processSegment("images/heartOfTheDiamondPropDiamondSegmentA.png", diamondA, 0,-32)
    --etc

    Likes: hgvyas123

    +1 -1 (+1 / -0 )Share on Facebook
  • Thank you guys :)

    Still learning the ropes and trying to get a little bit more efficient with my code.

    Your example will help me cut it down quite a bit!

    Thanks again!
  • Just looking quickly but unless I'm mistaken I think you need to add a "return segment" at the end of the processSegment() function so you can use it later (segmentA0 = processSegment).

    @Ninjadoodle
    Still learning the ropes and trying to get a little bit more efficient with my code.
    If you take time to look at the libs available in the tools section, you'll learn even faster. (Lot of them written by @ar2rsawseen by the way).

    Good luck!
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • Thanks, I'll have a look :)
Sign In or Register to comment.