Using Timer to show seconds.milliseconds

yup I just saw that you changed your previous code thanks for the help, does it matter if it should be scene:enterFrame or just enterFrame?

Doesn’t make any difference really, if it’s easier for you then I would just use enterFrame.

I have one more question for you, is there a way to make it so that it doesn’t look like the timer is moving.  It would be nice if it would not blink so much if you know what I mean?

Do you mean the text is kind of fidgeting left/right?  

If so this is caused by the reference points not being reset when the text property is changed. To fix it just add this to the end of your enterFrame function:

function enterFrame(e) --do your stuff as before txt\_counter.text = totalTime \* 0.001 --then --if using Corona build 2076 or later txt\_counter.anchorX, txt\_counter.anchorY = 0.5, 0.5 --for earlier builds use txt\_counter:setReferencePoint(display.CenterReferencePoint) --and then set the x and y position again, to the same values you originally used txt\_counter.x = 150 txt\_counter.y = 288

You will possibly need to adjust the anchor / setReferencePoint values based on how you have originally setup your text object (i.e. if it currently has a TopLeft ref point then use that instead of Center).

This works perfect thanks again for all of your help!

So I am coming across a weird problem when I build for an actual android device.  The timer does some weird stuff at the beginning, like adding .0000000000000001 and flashes a few times and then it is showing 6 decimal places instead of 3 like it was doing for the simulator. It is also adding 9999999 after the 6 decimal places, very strange. Any idea what is going on here?  Everything worked perfectly in the simulator but now on a device it is wigging out.

Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {

public void run()
{
//Called each time when 1000 milliseconds (1 second) (the period parameter)
runOnUiThread(new Runnable() {

public void run()
{
TextView tv = (TextView) findViewById(R.id.timer);
tv.setText(String.valueOf(time));
time += 1;

}

});
}

},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);

Thanks
Jeff Jones
Lead at Mobile Application Development Company
 

Where exactly does this code go if I am using storyboard?  This doesn’t look like lua code.

It’s not Lua it’s Obj-C, so not very useful unless you’re using Enterprise, which I’m guessing you’re not.  

To answer your question using Lua:

I think it’s simply floating point rounding which is giving you these extra decimal places. Try using this function to round to X number of decimal places:

--rounds input number n to X decimal places local function roundDec(n, X) local d = 10 ^ X return math.round(n \* d) / d end function enterFrame(e) txt\_counter.text = roundDec(totalTime \* 0.001, 3) end

Ya didn’t think it was lua!  Thanks again for your help it works great on my android device now.  I will be building for IOS this weekend and we will see if there are any other problems I am having with it.