Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Event not executing correctly — Gideros Forum

Event not executing correctly

zpopovskizpopovski Member
edited July 2013 in Game & application design
Hi,

I am porting some games form mexican beer sdk to gideros but i stuck in one problem. In my settings screen, i have one part when the player can choose which icon he will use for playing, that is developed like array with objects created from MovieClip.new, where each object contain exactly 2 frames, not selected and selected. In runtime i add that objects to the scene but when i press on one of the items i get nothing, here is my code.

local mc1 = MovieClip.new {
{1, 1, Bitmap.new(Texture.new("gfx/images/settings/abc.png", conf.textureFilter))},
{2, 2, Bitmap.new(Texture.new("gfx/images/settings/abcchecked.png", conf.textureFilter))}}
local mc2 = MovieClip.new {
{1, 1, Bitmap.new(Texture.new("gfx/images/settings/animal.png", conf.textureFilter))},
{2, 2, Bitmap.new(Texture.new("gfx/images/settings/animalchecked.png", conf.textureFilter))}}
local mc3 = MovieClip.new {
{1, 1, Bitmap.new(Texture.new("gfx/images/settings/creature.png", conf.textureFilter))},
{2, 2, Bitmap.new(Texture.new("gfx/images/settings/creaturechecked.png", conf.textureFilter))}}
local mc4 = MovieClip.new {
{1, 1, Bitmap.new(Texture.new("gfx/images/settings/flag.png", conf.textureFilter))},
{2, 2, Bitmap.new(Texture.new("gfx/images/settings/flagchecked.png", conf.textureFilter))}}
local mc5 = MovieClip.new {
{1, 1, Bitmap.new(Texture.new("gfx/images/settings/fruit.png", conf.textureFilter))},
{2, 2, Bitmap.new(Texture.new("gfx/images/settings/fruitchecked.png", conf.textureFilter))}}
local mc6 = MovieClip.new {
{1, 1, Bitmap.new(Texture.new("gfx/images/settings/nature.png", conf.textureFilter))},
{2, 2, Bitmap.new(Texture.new("gfx/images/settings/naturechecked.png", conf.textureFilter))}}
local mc7 = MovieClip.new {
{1, 1, Bitmap.new(Texture.new("gfx/images/settings/sign.png", conf.textureFilter))},
{2, 2, Bitmap.new(Texture.new("gfx/images/settings/signchecked.png", conf.textureFilter))}}
local mc8 = MovieClip.new {
{1, 1, Bitmap.new(Texture.new("gfx/images/settings/smile.png", conf.textureFilter))},
{2, 2, Bitmap.new(Texture.new("gfx/images/settings/smilechecked.png", conf.textureFilter))}}

local memoryCards = {
mc1, mc2, mc3, mc4, mc5, mc6, mc7, mc8
}

local startX = 40
local startY = 300
local vSpace = 10
local hSpace = 10

local memoryCards = createMovieClipArray()

for i=1,memoryCols do
for j=1,memoryRows do

local obj = memoryCards[memoryCardCounter]

obj:setPosition(startX, startY)
obj.id = memoryCardCounter
obj.isChecked = false
obj.framePosition = 1
obj:gotoAndStop(1)

obj.onTouch = function(self, event)
resetCheckedElements()
checkElementById(self.id)
chosenMemoryCardType = self.id
sets:set("currentcardset", chosenMemoryCardType, true)

print ("Clicked element ID: "..chosenMemoryCardType)
--event:stopPropagation()
end

obj:addEventListener(Event.MOUSE_DOWN, obj.onTouch, obj)

self:addChild(obj)
memoryCardCounter = memoryCardCounter + 1

startY = startY + 50 + vSpace
end
startY = 300
startX = startX + 50 + hSpace
end

chosenMemoryCardType = sets:get("currentcardset")

if(chosenMemoryCardType == nil) then chosenMemoryCardType = 1 end

memoryCards[chosenMemoryCardType].isChecked = true
memoryCards[chosenMemoryCardType].framePosition = chosenMemoryCardType
memoryCards[chosenMemoryCardType]:gotoAndStop(2)

function checkElementById(cellID)
memoryCards[cellID].isChecked = true
memoryCards[cellID]:gotoAndStop(2)
memoryCards[cellID].framePosition = 2
end

function resetCheckedElements()
for i=1,#memoryCards do
memoryCards[i].isChecked = false
memoryCards[i]:gotoAndStop(1)
memoryCards[1].framePosition = 1
end
end

function findCheckedElement()
local checkedElement = 1
for i=1,#memoryCards do
if(memoryCards[i].isChecked == true) then
checkedElement = memoryCards[i].id
break
end
end
return checkedElement
end

When i press on one of the icons i get this in console instead add to the app, event is triggered like multiple times which is wrong.
Clicked element ID: 8
Clicked element ID: 7
Clicked element ID: 6
Clicked element ID: 5
Clicked element ID: 4
Clicked element ID: 3
Clicked element ID: 2
Clicked element ID: 1

Thanks

Best Regards
Zarko Popovski

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited July 2013
    Hello @zpopovski ;)
    You need to modify your touch event a bit to check which element was clicked, like this:
    obj.onTouch = function(self, event)
        if self:hitTestPoint(event.x, event.y) then
            --we clicked this one
            resetCheckedElements()
            checkElementById(self.id) 
            chosenMemoryCardType = self.id 
            sets:set("currentcardset", chosenMemoryCardType, true)
     
            print ("Clicked element ID: "..chosenMemoryCardType)
            --event:stopPropagation()
        end
     end
  • Hey ar2rsawseen

    Thanks, it works like a charm, you save me lots of time playing with different workarounds :)
Sign In or Register to comment.