How can I create an invincibility frame effect?

Basically, what I am looking for is for the objects to “flash” multiple times. (by flash I mean its alpha to go up and down).

This is what I’ve got so far, but this flash is not good. (the timing is not correct, some flashes are faster than others)

local square = display.newRect(centerX, centerY, 40, 40) local function invinsibilityFrames() for i = 1, 5 do square.alpha = 0.5 timer.performWithDelay(1000, function() square.alpha = 1 end, 1) end end timer.performWithDelay(500, invinsibilityFrames, 5)

Any help would be great! Thanks!

** FIXED TYPOS and changed settings a little ***

I wouldn’t do that.  I’d use two separate transitions that toggle the other.
 
Or, I’d use the SSK2 Ping Pong helper in misc.* but this is NOT a perfect solution in this case:
 

-- Not a perfect solution, Just meh in this case -- local tmp = display.newRect( 100, 100, 100, 100 ) local ping = { alpha = 0.25, myTime = 500, myDelay = 500 } local pong = { alpha = 1, myTime = 500, myDelay = 500 } ssk.misc.pingPong( tmp, { ping = ping, pong = pong } ) timer.performWithDelay( 5000, function() transition.cancel( tmp ) transition.to( tmp, { alpha = 1, time = 100 } ) end )

What do you mean by using two separate transitions?

Also, by in the case of the ping pong helper, you say its an ok but not perfect solution. What is flawed about it in this case?

Look in the SSK2 code at the implementation of ping pong (in misc.lua). 

That uses two separate transitions.

Ah, I see, very well then. I will look at it.

what about:

[lua]

  local square = display.newRect(100, 100, 40, 40)

  transition.to(square,{
      delay = 500,
      time = 1000,
      alpha = 0.5,

      iterations = 5,
      transition = function(t,tmax,start,delta) if t/tmax < 0.5 then return start else return start+delta end end,
      onComplete = function() square.alpha = 1 end
   })
[/lua]

** FIXED TYPOS and changed settings a little ***

I wouldn’t do that.  I’d use two separate transitions that toggle the other.
 
Or, I’d use the SSK2 Ping Pong helper in misc.* but this is NOT a perfect solution in this case:
 

-- Not a perfect solution, Just meh in this case -- local tmp = display.newRect( 100, 100, 100, 100 ) local ping = { alpha = 0.25, myTime = 500, myDelay = 500 } local pong = { alpha = 1, myTime = 500, myDelay = 500 } ssk.misc.pingPong( tmp, { ping = ping, pong = pong } ) timer.performWithDelay( 5000, function() transition.cancel( tmp ) transition.to( tmp, { alpha = 1, time = 100 } ) end )

What do you mean by using two separate transitions?

Also, by in the case of the ping pong helper, you say its an ok but not perfect solution. What is flawed about it in this case?

Look in the SSK2 code at the implementation of ping pong (in misc.lua). 

That uses two separate transitions.

Ah, I see, very well then. I will look at it.

what about:

[lua]

  local square = display.newRect(100, 100, 40, 40)

  transition.to(square,{
      delay = 500,
      time = 1000,
      alpha = 0.5,

      iterations = 5,
      transition = function(t,tmax,start,delta) if t/tmax < 0.5 then return start else return start+delta end end,
      onComplete = function() square.alpha = 1 end
   })
[/lua]