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! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 )

Need some expert advice here. Carlos? :)

views:1441 update:2011/9/30 9:15:39
corona forums © 2003-2011