Image Not Updating on Screen

I’m trying to build an increment/decrement bar (like a power indicator or somesuch). When the user clicks, the bar starts incrementing toward a max value, then reverses and heads toward the mininum. This should continue until the user clicks the screen again. However, there are some problems going on that I haven’t been able to figure out. Primarily, once the bar is started with a touch, the program seems to not be capable of receiving the touch to cancel the event. PLUS, the image position is not getting updated, even though its x attribute is being changed. CODE BELOW.

Help! :slight_smile:

  
local max = 200  
local min = 0  
local str = min  
local adj = 1  
local running = false  
local interval = 0.5  
  
-- strimage should float across the screen from min to max then back again  
local strimage = display.newRect( 0, 0, 10, 10 )  
  
lastTime = system.getTimer()  
  
function calc\_bar()  
 if (system.getTimer() - lastTime) \> interval then  
 if (str + adj) \> max or (str + adj) \< min then  
 adj = -adj  
 end  
 str = str + adj  
  
 -- you can watch the value str change on the console  
 print(str)  
  
 -- BUT THE IMAGE DOES NOT MOVE until AFTER the timer is completed  
 -- it should move continuously until running == false :/  
 -- WHY DOESN'T strimage MOVE?  
 strimage.x = str  
 lastTime = system.getTimer()  
 end  
end  
  
function trigger\_bar(e)  
 if e.phase == "began" then  
 if running then  
 timer.cancel(tmr)  
 running = false  
 else  
 lastTime = system.getTimer()  
 -- I would like to use the following line, but it crashes because it takes over the system.  
-- tmr = timer.performWithDelay(0,calc\_bar,0)  
  
 -- have to use this timer so it will at least end at some point  
 -- however, other touch events seem to be disabled as it runs :-(  
 tmr = timer.performWithDelay(0,calc\_bar,750000)  
 running = true  
 end  
 end  
end  
  
-- the first touch triggers calc\_bar to start running  
-- however, subsequent touches (which should kill the timer) are not received!  
Runtime:addEventListener( "touch" , trigger\_bar )  

[import]uid: 61132 topic_id: 10711 reply_id: 310711[/import]

Need some expert advice here. Carlos? :slight_smile:
[import]uid: 61132 topic_id: 10711 reply_id: 38979[/import]