# How can I create an invincibility frame effect?

**URL:** <https://forums.solar2d.com/t/how-can-i-create-an-invincibility-frame-effect/345684>\
**Category:** Newbie Questions\
**Created:** [October 17, 2017, 3:51pm UTC](https://forums.solar2d.com/t/how-can-i-create-an-invincibility-frame-effect/345684 "2017-10-17T15:51:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blex](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/b/3bc359/32.png) [@Blex](https://forums.solar2d.com/u/Blex)\
**Post date:** [October 17, 2017, 3:51pm UTC](https://forums.solar2d.com/t/how-can-i-create-an-invincibility-frame-effect/345684/1 "2017-10-17T15:51:47Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![roaminggamer](https://forums.solar2d.com/user_avatar/forums.solar2d.com/roaminggamer/32/134_2.png) [@roaminggamer](https://forums.solar2d.com/u/roaminggamer)\
**Post date:** [October 17, 2017, 4:09pm UTC](https://forums.solar2d.com/t/how-can-i-create-an-invincibility-frame-effect/345684/2 "2017-10-17T16:09:17Z")

</div>

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

I wouldn’t do that.&nbsp; I’d use two separate transitions that toggle the other.  
&nbsp;  
Or, I’d use the [SSK2](https://roaminggamer.github.io/RGDocs/pages/SSK2/) [Ping Pong](https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/misc/#pingpong) helper in [misc.\*](https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/misc)&nbsp;but this is NOT a perfect solution in this case:  
&nbsp;

```
-- 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 )
```

---

<div class="post-metadata">

**Author:** ![Blex](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/b/3bc359/32.png) [@Blex](https://forums.solar2d.com/u/Blex)\
**Post date:** [October 17, 2017, 4:16pm UTC](https://forums.solar2d.com/t/how-can-i-create-an-invincibility-frame-effect/345684/3 "2017-10-17T16:16:20Z")

</div>

What do you mean by using two separate&nbsp;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?

---

<div class="post-metadata">

**Author:** ![roaminggamer](https://forums.solar2d.com/user_avatar/forums.solar2d.com/roaminggamer/32/134_2.png) [@roaminggamer](https://forums.solar2d.com/u/roaminggamer)\
**Post date:** [October 17, 2017, 5:00pm UTC](https://forums.solar2d.com/t/how-can-i-create-an-invincibility-frame-effect/345684/4 "2017-10-17T17:00:30Z")

</div>

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

That uses two separate transitions.

---

<div class="post-metadata">

**Author:** ![Blex](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/b/3bc359/32.png) [@Blex](https://forums.solar2d.com/u/Blex)\
**Post date:** [October 17, 2017, 8:04pm UTC](https://forums.solar2d.com/t/how-can-i-create-an-invincibility-frame-effect/345684/5 "2017-10-17T20:04:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![IDGames](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/i/a9a28c/32.png) [@IDGames](https://forums.solar2d.com/u/IDGames)\
**Post date:** [October 19, 2017, 5:57am UTC](https://forums.solar2d.com/t/how-can-i-create-an-invincibility-frame-effect/345684/6 "2017-10-19T05:57:58Z")

</div>

what about:

[lua]

&nbsp; local square = display.newRect(100, 100, 40, 40)

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

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

---

<div class="post-metadata">

**Author:** ![roaminggamer](https://forums.solar2d.com/user_avatar/forums.solar2d.com/roaminggamer/32/134_2.png) [@roaminggamer](https://forums.solar2d.com/u/roaminggamer)\
**Post date:** [October 17, 2017, 4:09pm UTC](https://forums.solar2d.com/t/how-can-i-create-an-invincibility-frame-effect/345684/7 "2017-10-17T16:09:17Z")

</div>

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

I wouldn’t do that.&nbsp; I’d use two separate transitions that toggle the other.  
&nbsp;  
Or, I’d use the [SSK2](https://roaminggamer.github.io/RGDocs/pages/SSK2/) [Ping Pong](https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/misc/#pingpong) helper in [misc.\*](https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/misc)&nbsp;but this is NOT a perfect solution in this case:  
&nbsp;

```
-- 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 )
```

---

<div class="post-metadata">

**Author:** ![Blex](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/b/3bc359/32.png) [@Blex](https://forums.solar2d.com/u/Blex)\
**Post date:** [October 17, 2017, 4:16pm UTC](https://forums.solar2d.com/t/how-can-i-create-an-invincibility-frame-effect/345684/8 "2017-10-17T16:16:20Z")

</div>

What do you mean by using two separate&nbsp;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?

---

<div class="post-metadata">

**Author:** ![roaminggamer](https://forums.solar2d.com/user_avatar/forums.solar2d.com/roaminggamer/32/134_2.png) [@roaminggamer](https://forums.solar2d.com/u/roaminggamer)\
**Post date:** [October 17, 2017, 5:00pm UTC](https://forums.solar2d.com/t/how-can-i-create-an-invincibility-frame-effect/345684/9 "2017-10-17T17:00:30Z")

</div>

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

That uses two separate transitions.

---

<div class="post-metadata">

**Author:** ![Blex](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/b/3bc359/32.png) [@Blex](https://forums.solar2d.com/u/Blex)\
**Post date:** [October 17, 2017, 8:04pm UTC](https://forums.solar2d.com/t/how-can-i-create-an-invincibility-frame-effect/345684/10 "2017-10-17T20:04:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![IDGames](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/i/a9a28c/32.png) [@IDGames](https://forums.solar2d.com/u/IDGames)\
**Post date:** [October 19, 2017, 5:57am UTC](https://forums.solar2d.com/t/how-can-i-create-an-invincibility-frame-effect/345684/11 "2017-10-19T05:57:58Z")

</div>

what about:

[lua]

&nbsp; local square = display.newRect(100, 100, 40, 40)

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

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