Touch Events: Making a text/button color "stick" until next text/button pressed.

Hello -
I have 5 text items that are set to respond to touch events on the screen. All of the text items are white.

What I am trying to do is:

  • When the text is pressed, to have the color change to, say, orange and stay changed until the next text item is pressed. At that time, the next text item will change to orange and the previous one reverts to white.

The code I have so far allows for touch events. However, the color doesn’t “stick”. Is there a way to modify this code to allow for the above steps to occur:

[lua]local function textTouch(event)
local id = event.target.id
if(event.phase == “began”) then
transition.to( text[id]:setTextColor(255,255,55), { time=50, alpha=1.0})
d = modesVar[id]
elseif(event.phase == “moved”) then
transition.to( text[id]:setTextColor(255,255,55), { time=50, alpha=1.0 })
elseif(event.phase == “ended”) then
transition.to( text[id]:setTextColor(255,255,255), { time=100, alpha=1.0 })
end
end[/lua]

Thank you. [import]uid: 32833 topic_id: 8520 reply_id: 308520[/import]

i did this in two secs, should get you going. u need to keep a list, and a previous value.

[code]
local text1 = display.newText(“Hello”,20,20,“Helvetica”,12);
text1:setTextColor(255,0,0);

local text2 = display.newText(“Hello”,20,50,“Helvetica”,12);
text2:setTextColor(255,0,0);

local prev = nil;
function myTouch(event)
print (“touch…”);

local id = event.target
if (prev == nil ) then
id:setTextColor(255,255,0);
prev = id
end
prev:setTextColor(255,0,0);
id:setTextColor(255,255,0);
prev = id;

end

text1:addEventListener(“touch”,myTouch);
text2:addEventListener(“touch”,myTouch);[/code] [import]uid: 24 topic_id: 8520 reply_id: 30548[/import]

Thank you Carlos! [import]uid: 32833 topic_id: 8520 reply_id: 30585[/import]