Automatic Fire

Hello all, been reviewing API’s for timers but cannot seem to find my answer. 

I want simply to put a player on Automatic Fire every two seconds…

I set up a function that calls my animation 

function autoMaticFire( event )

 playerAlex:play()

end

tmr = timer.performWithDelay(2000,autoMaticFire);

I thought it would fire every two seconds but like the it says performWithDelay !! :) 

any other calls for what I am looking for ?

Regards

JZ 

You are missing iteration parameter: http://docs.coronalabs.com/daily/api/library/timer/performWithDelay.html

tmr = timer.performWithDelay(2000,autoMaticFire , 0 );

Thanks Jon , i will check it out. 

Regards

JZ 

Oh well, I tried all kinds of combinations with your suggestion, still cannot get it to fire every 2 seconds. 

tmr = timer.performWithDelay(2000,autoMaticFire,0,timer.cancel)

tmr = timer.performWithDelay(2000,autoMaticFire,0)

tmr = timer.performWithDelay(2000,autoMaticFire,1,timer.cancel)

I read the API docs and still cannot get results. 

What does the 0 represent, i know a parameter -1 means loop for ever does 0 mean only once ?

This is what I got.

local function autoMaticFire( event )

   playerAlex:play()

      end

tmr = timer.performWithDelay(2000,autoMaticFire,0)

This is the right way:

tmr = timer.performWithDelay(2000,autoMaticFire,0)

So check if the problem is not with your function autoMaticFire(event)

Replace that playerAlex:play() bit with a simple print statement and see if that works. Also, try making the function global and see if that works. The simple codeblock above is correct (with the local declaration), but maybe this is just a simplified version so I’m checking!

Hi Thomas , yes I did make it global , my playerAlex is firing but a continuous loop, it does not fire every 2 seconds as i hope. 

and changing the parameters -1, 0 , 1 all have no effect on the loop. 

Seems like this should be an easy task,

Thanks for all your help.

Much appreciated. 

going to the gym to let off some steam !!! :) 

JZ 

Hi!

That’s why I said “replace the line with a simple print statement”. If that print statement executes nicely, once every two seconds, you know that the problem is not in the timer function but in your :play call.

And then we’ll take it from there.

Okay you were correct the print statement fires every two seconds as expected. here is the rest of the player code. 

This is what it looks like…

----------------------------------             Create Player         -------------------------

 function addPlayer( event )

   playerSpriteSheetData = {

   width = 120,

   height = 240,

   numFrames = 4,

   sheetContentWidth = 480,

   sheetContentHeight = 240

 }

     playerSheet = graphics.newImageSheet(“SpriteSheetFolder/playerSheet.png”,playerSpriteSheetData)

     playerSequence = {

      {name = “normalRun”,start = 1, count = 4, time = 250},

      {name = “fastRun”,frames = {1,2,3,4},time = 500}

  }

      playerAlex = display.newSprite(playerSheet,playerSequence)

      playerAlex.x = 60

      playerAlex.y = self.view.contentHeight - 50

      playerAlex.xScale = .3

      playerAlex.yScale = .3 

      playerAlex:setSequence (“normalRun”)

      sceneGroup1:insert(playerAlex)

 end

                            -------------------        AUTOMATIC FIRE -------------

function listener( event )

print(“I am printing”)

  playerAlex:play()

end

tmr = timer.performWithDelay(2000,listener,0)

addPlayer()

Thanks so much !!

Hi,

Where’s the firing? As far as I can see you are triggering the sequence “normal run”, right? That sequence is a loop, as far as I can see, so it won’t stop playing, and after two seconds all you’re doing is saying “play that loop”, which it is already doing.

Basically “normalRun” sequence says: play 4 images starting from frame 1, in 250 milliseconds, and then loops indefinitely. What were you expecting to see happen? If you don’t want the sequence to loop, use the loopCount parameter as option!

Excellent got it working loopCount !! knew it was something I was missing…

Thanks for all your help

JZ 

You’re welcome! Yeah, I looked at that code and thought “That count parameter’s name is awfully confusing!” :wink:

A little i guess !! lol

You are missing iteration parameter: http://docs.coronalabs.com/daily/api/library/timer/performWithDelay.html

tmr = timer.performWithDelay(2000,autoMaticFire , 0 );

Thanks Jon , i will check it out. 

Regards

JZ 

Oh well, I tried all kinds of combinations with your suggestion, still cannot get it to fire every 2 seconds. 

tmr = timer.performWithDelay(2000,autoMaticFire,0,timer.cancel)

tmr = timer.performWithDelay(2000,autoMaticFire,0)

tmr = timer.performWithDelay(2000,autoMaticFire,1,timer.cancel)

I read the API docs and still cannot get results. 

What does the 0 represent, i know a parameter -1 means loop for ever does 0 mean only once ?

This is what I got.

local function autoMaticFire( event )

   playerAlex:play()

      end

tmr = timer.performWithDelay(2000,autoMaticFire,0)

This is the right way:

tmr = timer.performWithDelay(2000,autoMaticFire,0)

So check if the problem is not with your function autoMaticFire(event)

Replace that playerAlex:play() bit with a simple print statement and see if that works. Also, try making the function global and see if that works. The simple codeblock above is correct (with the local declaration), but maybe this is just a simplified version so I’m checking!

Hi Thomas , yes I did make it global , my playerAlex is firing but a continuous loop, it does not fire every 2 seconds as i hope. 

and changing the parameters -1, 0 , 1 all have no effect on the loop. 

Seems like this should be an easy task,

Thanks for all your help.

Much appreciated. 

going to the gym to let off some steam !!! :) 

JZ 

Hi!

That’s why I said “replace the line with a simple print statement”. If that print statement executes nicely, once every two seconds, you know that the problem is not in the timer function but in your :play call.

And then we’ll take it from there.

Okay you were correct the print statement fires every two seconds as expected. here is the rest of the player code. 

This is what it looks like…

----------------------------------             Create Player         -------------------------

 function addPlayer( event )

   playerSpriteSheetData = {

   width = 120,

   height = 240,

   numFrames = 4,

   sheetContentWidth = 480,

   sheetContentHeight = 240

 }

     playerSheet = graphics.newImageSheet(“SpriteSheetFolder/playerSheet.png”,playerSpriteSheetData)

     playerSequence = {

      {name = “normalRun”,start = 1, count = 4, time = 250},

      {name = “fastRun”,frames = {1,2,3,4},time = 500}

  }

      playerAlex = display.newSprite(playerSheet,playerSequence)

      playerAlex.x = 60

      playerAlex.y = self.view.contentHeight - 50

      playerAlex.xScale = .3

      playerAlex.yScale = .3 

      playerAlex:setSequence (“normalRun”)

      sceneGroup1:insert(playerAlex)

 end

                            -------------------        AUTOMATIC FIRE -------------

function listener( event )

print(“I am printing”)

  playerAlex:play()

end

tmr = timer.performWithDelay(2000,listener,0)

addPlayer()

Thanks so much !!

Hi,

Where’s the firing? As far as I can see you are triggering the sequence “normal run”, right? That sequence is a loop, as far as I can see, so it won’t stop playing, and after two seconds all you’re doing is saying “play that loop”, which it is already doing.

Basically “normalRun” sequence says: play 4 images starting from frame 1, in 250 milliseconds, and then loops indefinitely. What were you expecting to see happen? If you don’t want the sequence to loop, use the loopCount parameter as option!