animation on touch event

I am creating myself a little joystick function that animates buttons on press, all (3) are receiving touch events but the first animates no matter what button i press.

------- DJ\_Joystick Module 1.0 -------- ---------------------------------------- ------- sprite animation options ------- -- object:play() -- -- object:setFrame() -- -- object:setFrame() -- -- object:setSequence() -- ---------------------------------------- display.setStatusBar(display.HiddenStatusBar) --require "sprite" --require "refPointConversions" local butPositionSpacing = 68 local buttonSheet = { } button = { -- Button table { number=1, x=300, y=600, image="ButtonRed.png", does="run" }, { number=2, x=366, y=600, image="ButtonRed.png", does="jump"}, { number=3, x=416, y=600, image="ButtonRed.png", does="jump"} } local options = -- Button sprite sheet options { width = 64, height = 64, numFrames = 2 } local butttonSeqdata = -- Button frame animation options { {name = "up", start = 1, -- start frame count = 1, -- how many frames --frames = { 4,8,12,16 }, -- order of frames time = 700, loopcount = 0, loopdirection = "forward"}, {name = "down", start = 2, -- start frame count = 1, -- how many frames --frames = { 2,6,10,14 }, -- order of frames time = 700, loopcount = 0, loopdirection = "forward"} } ---------------------------------------- -- Button Touch Function -- ---------------------------------------- local function onTouch( event ) for i=1,3 do --#button button[i].isFocus = true if event.phase == "began" then display.getCurrentStage():setFocus( control, event.id ) end if "began" == event.phase then display.getCurrentStage():setFocus(event.target) button[i]:setSequence("down") button[i]:play() --print( "button",button[i].number,"pressed" ) elseif event.phase=='ended' or event.phase=='moved'then button[i]:setSequence("up") button[i]:play() --print( "button",button[i].number ,"de-pressed" ) display.getCurrentStage():setFocus(nil) end return true end end ---------------------------------------- -- Create Button Function -- ---------------------------------------- local function createButton(initialX) for i=1,#button do buttonSheet[i] = graphics.newImageSheet(button[i].image, options) button[i] = display.newSprite(buttonSheet[i], butttonSeqdata) button[i].x = (butPositionSpacing \* i) - (butPositionSpacing / 2); button[i].y = 480 button[i]:addEventListener( "touch", onTouch) end end ----------------------------------------- -- Call Create Button Function -- initial x, y, image, numer of buttons ----------------------------------------- createButton(320) ---------------------------------------- -- Create Button Function -- ---------------------------------------- local mainLoop = function() end ---------------------------------------- -- Call main loop every frame -- ---------------------------------------- Runtime:addEventListener( "enterFrame", mainLoop )

In your onTouch function you’ve got a for loop that returns true on the first iteration. Move that “return true” below the “end” beneath it. Depending on what you’re trying to do, you might want to start an “if event.target == button[i] then” conditional after you start your for loop.

In your onTouch function you’ve got a for loop that returns true on the first iteration. Move that “return true” below the “end” beneath it. Depending on what you’re trying to do, you might want to start an “if event.target == button[i] then” conditional after you start your for loop.