Timer

Hello,

I am trying to have an object blinking, i have tried making the alpha to 0 on a transition but it is not the best way and still responds to collisions. I now think i need to have a timer on a loop, reappearing and removing it self every 3 seconds. Any help ? I am having difficult time figuring out how to have a timer on a loop and adding functions to my collisions.

Thank you. [import]uid: 23689 topic_id: 13141 reply_id: 313141[/import]

blinking square object
[lua]local object = display.newRect(0,0,50,50)
object.x = 150
object.y = 150

function blink()
object.alpha = 0.5
end

function blink2()
object.alpha = 1
end

timer.performWithDelay(300, blink, 0)
timer.performWithDelay(300, blink2, 0)[/lua]

if thats what you need [import]uid: 16142 topic_id: 13141 reply_id: 48219[/import]

Thanks a lot! That is very similar to what i want! I wanted it to not respond to any collisions when the alpha is 0 or is it possible to remove:self() on the first blink and reappear? Thanks, i want to simulate a line that is trying to kill you and you need to pass it when it blinks so it doesn’t kill you.

Thanks again! [import]uid: 23689 topic_id: 13141 reply_id: 48245[/import]

Shouldn’t the two blink functions call each other:

[lua]local object = display.newRect(0,0,50,50)
object.x = 150
object.y = 150

function blink()
object.alpha = 0.5
timer.performWithDelay(300, blink2, 0)
end

function blink2()
object.alpha = 1
timer.performWithDelay(300, blink, 0)
end

blink2()[/lua]
[import]uid: 19626 topic_id: 13141 reply_id: 48250[/import]

you can do that too, its just another way to do the same) [import]uid: 16142 topic_id: 13141 reply_id: 48301[/import]

my take on this would be to have a recursive call to the routine that alternates between the opacity of 0.5 and 1

[lua]local alternate = true
local alpha

local object = display.newRect(0,0,50,50)
object.x = 150
object.y = 150

function test()

if alternate==true then
alpha=0.5
else
alpha=1.0
end
object.alpha=alpha

alternate = not alternate

timer.performWithDelay(300,test)
end

test()[/lua]

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13141 reply_id: 48345[/import]

Hi,

Thanks for all your replies! I was wandering if i could remove the object then have it reappear again, just like turning the alpha to 0 but having it not react to any collisions? [import]uid: 23689 topic_id: 13141 reply_id: 48480[/import]

have you tried setting the visibility to false? object.isVisible = false to hide it? You can also remove it’s physics body.

[import]uid: 19626 topic_id: 13141 reply_id: 48483[/import]

I have tried that but when it’s blinking and the object’s visibility is false it still responds to collisions.
If i remove the physics body, i need to add a the same object every 3 seconds in a loop? What i want is when it is not visible, collisions don’t react to it. So once it is visible the collisions do react, thanks a lot for your help! [import]uid: 23689 topic_id: 13141 reply_id: 48599[/import]

If my memory is correct there are two ways to keep a physics object from reacting to a collision:

  1. body.isBodyActive = false – Disables the body without destroying it
  2. body.bodyType = “static” – make it a static object (you can switch it back later)
    [import]uid: 7559 topic_id: 13141 reply_id: 48624[/import]