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
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)
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
Likes: hgvyas123
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!
@Ninjadoodle 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!