help with timer

I’m trying to experiment with timer and runtime to create a game. But I need some help.

Thanks to anyone who can help me with this.

I need to create an image, very small

local big = display.newImage("buttonSpanish.png") big.x = display.contentWidth / 2; big.y = 700 big:scale (.1, .1)

Then transition tthat image from point A to point B

transition.to(big, {time=1000, y=100})

At the end of point B the image must removeSelf or nil

if big == 100 then         display.remove (big)     end

but the function will create a another image very small and do the same thing all over again.

This is to create a famous “street” with trees coming at you.

or objects moving, to create an illusion of movement.

I hope I make my self clear of what I need, and I hope smeone will help me out.

Thank you in advance.

I’m trying to use runtime and timer, but I don’t really get it.

function tele () timer.performWithDelay( 1000, tele ) big = nil; local big = display.newImage("buttonSpanish.png") big.x = display.contentWidth / 2; big.y = 700 big:scale (.2, .2) transition.to(big, {time=1000, y=100})     if big == 100 then         display.remove (big)     end end Runtime:addEventListener("enterFrame", tele)

I need help

Hi helloworld2013d,

I’m not sure what you’re trying to do but maybe the problem is in the way you’re using the timer as well as the transition.to API.

Maybe doing something like this:

local function tele() -- Create your display object here local big = display.newImage("your-image.png") big.x = display.contentWidth big.y = 700 -- Move the display object transitio.to( big, { time = 1000, y = 100, onComplete = function(obj) -- Check what you need to check if obj.y == 100 then -- Remove your display object obj:removeSelf() obj = nil end end } ) end -- Pass 0 to loop forever local tmr = timer.performWithDelay( 1000, tele, 0 )

And then, at a certain point you may cancel your timer if you wish.

I’ve used an inline function in the transition.to but you can create a function in other section of your program and then call it every time the transition.to completes. Additionally, you can also create a separate function to create your display objects and call it every time the “tele” function is called, depending on your needs.

Hope this help!

Best regards,

Paolo

I think it’s working but…

how do I cancel timer.

I saw this in corona lab

local t = {} function t:timer( event ) local count = event.count print( "Table listener called " .. count .. " time(s)" ) if count \>= 3 then timer.cancel( event.source ) -- after 3rd invocation, cancel timer end end -- Register to call t's timer method an infinite number of times timer.performWithDelay( 1000, t, 0 )

I don’t know how to use it in my code

I have this

timer.cancel (tele)

Do I put it in the exitScene?

in the destroyScene?

or inside the function to go to another scene?

And how do I write it?

Hi Victor,

You can just cancel any timer using this, by putting its reference as the sole parameter. Of course, this must be a valid, running (or paused) timer, or you will receive an error.

So, in your code, “tele” must be a valid timer, like this:

[lua]

local tele = timer.performWithDelay( 1000, t, 0 )

[/lua]

And sometime later…

[lua]

timer.cancel( tele )

[/lua]

Hope this helps,

Brent

Hi,

 Do I put it in the exitScene?

in the destroyScene?

 

or inside the function to go to another scene?

 

And how do I write it?

Well, that depends on what you’re doing in your code. Suppose you want a certain number of display objects to be created and moved and after that you want to cancel the timer. In that case you can track the number of objects created and verify if that number if grater than the desired number of objects. If that is true, then -as Brent mentioned- you cancel the timer passing the reference.

For example, outside of your tele function you can declare a couple of variables and perform verification inside the function, for example:

local max\_objs = 3 local count = 0 local function tele(event) -- count = count + 1 -- if count \>= max\_objs then timer.cancel( event.source ) end -- Create your display object -- Move the object end local timer = timer.performWithDelay( 1000, tele, 0 )

Greetings,

Paolo

If I don’t get crazy learning programming, I think nothing will make me crazy.

It was one little thing. I had a “variable” --tmr and that was = to timer…

when I was canceling, I was canceling the function (tele) – BAD

I had to cancel the variable --tmr , like this

timer.cancel (tmr)

It works!

Now, one more thing here.

I have an object moving from point A (let’s say from Los Angeles California) to point B (New York)

so the object goes every time from – LA to NY – from LA to NY

If I “tap” the button to go to another scene, in the middle of the “trip” let’s say in texas…

the timer is cancelled… so No new image will be displayed, and no more new trip to NY

but the object that it’s already in the middle of the 2 points, still keeps going, untill it gets to point B (NY)

and then removeSelf.

So if I have a lot of trees going on and I go to another scene, all the images will still be there in the other scene

How do I remove the images in the middle of the “trip”?

I hope you understand what I’m asking.

Hi helloworld2013d,

I’m glad you’re not going crazy  ;)  

I don’t know if it is a good idea to mix several questions in a single topic. That being said…

To remove the display objects you’ve created when using storyboard you need to add them to the display group of the scene when created.

group:insert( your\_display\_object )

Assuming the display group is assigned to a variable “group”

local group = self.view

That depends on your code. For more information check the Corona docs.

Greetings,

Paolo

Hi helloworld2013d,

I’m not sure what you’re trying to do but maybe the problem is in the way you’re using the timer as well as the transition.to API.

Maybe doing something like this:

local function tele() -- Create your display object here local big = display.newImage("your-image.png") big.x = display.contentWidth big.y = 700 -- Move the display object transitio.to( big, { time = 1000, y = 100, onComplete = function(obj) -- Check what you need to check if obj.y == 100 then -- Remove your display object obj:removeSelf() obj = nil end end } ) end -- Pass 0 to loop forever local tmr = timer.performWithDelay( 1000, tele, 0 )

And then, at a certain point you may cancel your timer if you wish.

I’ve used an inline function in the transition.to but you can create a function in other section of your program and then call it every time the transition.to completes. Additionally, you can also create a separate function to create your display objects and call it every time the “tele” function is called, depending on your needs.

Hope this help!

Best regards,

Paolo

I think it’s working but…

how do I cancel timer.

I saw this in corona lab

local t = {} function t:timer( event ) local count = event.count print( "Table listener called " .. count .. " time(s)" ) if count \>= 3 then timer.cancel( event.source ) -- after 3rd invocation, cancel timer end end -- Register to call t's timer method an infinite number of times timer.performWithDelay( 1000, t, 0 )

I don’t know how to use it in my code

I have this

timer.cancel (tele)

Do I put it in the exitScene?

in the destroyScene?

or inside the function to go to another scene?

And how do I write it?

Hi Victor,

You can just cancel any timer using this, by putting its reference as the sole parameter. Of course, this must be a valid, running (or paused) timer, or you will receive an error.

So, in your code, “tele” must be a valid timer, like this:

[lua]

local tele = timer.performWithDelay( 1000, t, 0 )

[/lua]

And sometime later…

[lua]

timer.cancel( tele )

[/lua]

Hope this helps,

Brent

Hi,

 Do I put it in the exitScene?

in the destroyScene?

 

or inside the function to go to another scene?

 

And how do I write it?

Well, that depends on what you’re doing in your code. Suppose you want a certain number of display objects to be created and moved and after that you want to cancel the timer. In that case you can track the number of objects created and verify if that number if grater than the desired number of objects. If that is true, then -as Brent mentioned- you cancel the timer passing the reference.

For example, outside of your tele function you can declare a couple of variables and perform verification inside the function, for example:

local max\_objs = 3 local count = 0 local function tele(event) -- count = count + 1 -- if count \>= max\_objs then timer.cancel( event.source ) end -- Create your display object -- Move the object end local timer = timer.performWithDelay( 1000, tele, 0 )

Greetings,

Paolo

If I don’t get crazy learning programming, I think nothing will make me crazy.

It was one little thing. I had a “variable” --tmr and that was = to timer…

when I was canceling, I was canceling the function (tele) – BAD

I had to cancel the variable --tmr , like this

timer.cancel (tmr)

It works!

Now, one more thing here.

I have an object moving from point A (let’s say from Los Angeles California) to point B (New York)

so the object goes every time from – LA to NY – from LA to NY

If I “tap” the button to go to another scene, in the middle of the “trip” let’s say in texas…

the timer is cancelled… so No new image will be displayed, and no more new trip to NY

but the object that it’s already in the middle of the 2 points, still keeps going, untill it gets to point B (NY)

and then removeSelf.

So if I have a lot of trees going on and I go to another scene, all the images will still be there in the other scene

How do I remove the images in the middle of the “trip”?

I hope you understand what I’m asking.

Hi helloworld2013d,

I’m glad you’re not going crazy  ;)  

I don’t know if it is a good idea to mix several questions in a single topic. That being said…

To remove the display objects you’ve created when using storyboard you need to add them to the display group of the scene when created.

group:insert( your\_display\_object )

Assuming the display group is assigned to a variable “group”

local group = self.view

That depends on your code. For more information check the Corona docs.

Greetings,

Paolo