I am making a character selection screen in corona where you select different items of clothing for your character by clicking on arrows which increase or decrease a counter and in turn changes the item of clothing selected. How do I make 1 function which handles the events for all of the arrows, but changes a different variable for each one?
Without seeing code we can’t be precise with the answer, but I’m going to assume you’re using two graphics (left arrow, right arrow) and have a touch handler assigned to them. I would do something like:
local function handleArrow( event ) if event.phase == "began" then if event.target.clothingName == "belt" then beltCount = beltCount + event.target.incrementAmount elseif event.target.clothingName == "hat" then hatCount = hatCount + event.target.incrementAmount end end return true end local leftBeltArrow = display.newImageRect("leftarrow.png", 50, 25 ) leftBeltArrow.clothingName = "belt" leftBeltArrow.incrementAmount = -1 leftBeltArrow:addEventListener( "touch", handleArrow ) local rightBeltArrow = display.newImageRect("rightarrow.png", 50, 25 ) rightBeltArrow.clothingName = "belt" rightBeltArrow.incrementAmount = 1 rightBeltArrow:addEventListener( "touch", handleArrow ) local leftHatArrow = display.newImageRect("leftarrow.png", 50, 25 ) leftHatArrow.clothingName = "hat" leftHatArrow.incrementAmount = -1 leftHatArrow:addEventListener( "touch", handleArrow )
etc.
Of course I don’t know what your variables are called and what your objects are called, so you will need to adapt this to your code.
Rob
Without seeing code we can’t be precise with the answer, but I’m going to assume you’re using two graphics (left arrow, right arrow) and have a touch handler assigned to them. I would do something like:
local function handleArrow( event ) if event.phase == "began" then if event.target.clothingName == "belt" then beltCount = beltCount + event.target.incrementAmount elseif event.target.clothingName == "hat" then hatCount = hatCount + event.target.incrementAmount end end return true end local leftBeltArrow = display.newImageRect("leftarrow.png", 50, 25 ) leftBeltArrow.clothingName = "belt" leftBeltArrow.incrementAmount = -1 leftBeltArrow:addEventListener( "touch", handleArrow ) local rightBeltArrow = display.newImageRect("rightarrow.png", 50, 25 ) rightBeltArrow.clothingName = "belt" rightBeltArrow.incrementAmount = 1 rightBeltArrow:addEventListener( "touch", handleArrow ) local leftHatArrow = display.newImageRect("leftarrow.png", 50, 25 ) leftHatArrow.clothingName = "hat" leftHatArrow.incrementAmount = -1 leftHatArrow:addEventListener( "touch", handleArrow )
etc.
Of course I don’t know what your variables are called and what your objects are called, so you will need to adapt this to your code.
Rob