Object to start flashing on tap

Hey guys,

I’ve been stuck on this thing for a while now, and I think I have become so confused that my brain is sabotaging any chance of finding a solution. I want to tap on an object and the object to start fading in and out until it’s destroyed. ( I know I need to mess with the alpha values but I am getting the feeling I need to implement a timer, the problem is I can’t bind it to the “tap” listener cause then the timer is only “alive” for the duration of the tap, which is a splitsecond, or at least that’s how I understand my predicament. )

I have tried timer.performWithDelay( ) in several ways but still can’t make sense of it.

Any help or suggestions is appreciated. [import]uid: 106739 topic_id: 31664 reply_id: 331664[/import]

I think one way to do this would be to have 2 fade functions, 1 event function, 1 lifeCounter:

– life counter
local life = 10
function fadeIn()
yourObject.alpha = yourObject.alpha + .10
life = life - 1
if life == 0 then
– call other function
else
if yourObject.alpha >= 1.0 then
timer.performWithDelay(200, fadeOut, 5)
end
end
end

function fadeOut()
yourObject.alpha = yourObject.alpha - .10
if yourObject.alpha <= .50 then
timer.performWithDelay(200, fadeIn, 5)
end
end

– event handler here
local function onTap(event)
if event.phase == “ended” then
timer.performWithDelay(200, fadeOut, 5)
end
end

Basically this starts by calling fadeOut 5 times, reducing alpha of your object a little each time, once it gets below a certain alpha it calls to fadeIn 5 times, until it reaches full alpha … each cycle of calls to the fadeOut/fadeIn functions it will check the life counter, and when it is zero it calls some other function in your code.

Slapped this together fairly quickly, and did not test, but looks to me like it will work… or at least get you on the right path.

Hope this helps! [import]uid: 148857 topic_id: 31664 reply_id: 126500[/import]

Hi cyberparkstudios,

Thank you, your answer does help, however, the listener never goes into the " if event.phase == ended" loop, and I don’t know why. Does the event end before it gets in the loop? If that doesn’t work, then why is the syntax for it there? Anyway, if you could lend a further hand, I’d be much obliged :slight_smile: [import]uid: 106739 topic_id: 31664 reply_id: 126521[/import]

You have to use ‘touch’ event, not ‘tap’. touch will have begin, moved, and ended phases.
I was writing you a blink function before you posted a reply. Here it is anyways if you still need it… this will make an object blink in/out 5 times when you touch it.

[code]
local blinkTrans

function startBlinking( obj )
local hasFaded
local fadeTime = 150
local blinkTotal = 5
local blinkCount = 0

–RESET TRANSITION
if (blinkTrans) then transition.cancel(blinkTrans) end
obj.alpha = 1

local function blink()
if (blinkCount < blinkTotal) then
if (not hasFaded) then
blinkTrans = transition.to(obj, {time=fadeTime, alpha=0.2, onComplete=function()
hasFaded = true
blink(obj)
end})
else
blinkTrans = transition.to(obj, {time=fadeTime, alpha=1, onComplete=function()
hasFaded=false
blinkCount = blinkCount +1
blink(obj)
end})
else blinkTrans = nil end
end

–START BLINKING
blink()
end

function onTouch(event)
if (event.phase == ‘began’) then
startBlinking(event.target)
end
end

obj:addEventListener(‘touch’, onTouch)
[/code] [import]uid: 36792 topic_id: 31664 reply_id: 126523[/import]

Here is my code without looking at the others. Sorry if this is a repeat.
[lua]local object = display.newRect ( 10, 10, 50, 50 )
local fadeTime = 600
local destructionTime = 2000

– Local Function Definition
local checkDestroyed, flashUntilDestroyed, onTap

function checkDestroyed ( )
if ( object.destroyed ) then
object:removeSelf ( )
– Play destruction sequence
else
flashUntilDestroyed ( )
end
end

function flashUntilDestroyed ( )
local alpha = 0
if ( object.alpha == 0 ) then alpha = 1 end
transition.to ( object, { time = fadeTime / 2, alpha = alpha, onComplete = checkDestroyed } )
end

function onTap ( )
flashUntilDestroyed ( )
timer.performWithDelay ( destructionTime, function () object.destroyed = true end )
end

object:addEventListener ( “tap”, onTap )[/lua] [import]uid: 7721 topic_id: 31664 reply_id: 126530[/import]

@perturbedMullusk

Correct. Tap and Touch are different. So if you take out the 'if phase == “ended” and it’s ‘end’ it should work correctly. However …

ArdentKid is very experienced and talented individual and I think you will find that code very effective, efficient, and educational.

[import]uid: 148857 topic_id: 31664 reply_id: 126539[/import]

Thank you all! Your answers have helped immensely. :slight_smile: [import]uid: 106739 topic_id: 31664 reply_id: 126623[/import]

I think one way to do this would be to have 2 fade functions, 1 event function, 1 lifeCounter:

– life counter
local life = 10
function fadeIn()
yourObject.alpha = yourObject.alpha + .10
life = life - 1
if life == 0 then
– call other function
else
if yourObject.alpha >= 1.0 then
timer.performWithDelay(200, fadeOut, 5)
end
end
end

function fadeOut()
yourObject.alpha = yourObject.alpha - .10
if yourObject.alpha <= .50 then
timer.performWithDelay(200, fadeIn, 5)
end
end

– event handler here
local function onTap(event)
if event.phase == “ended” then
timer.performWithDelay(200, fadeOut, 5)
end
end

Basically this starts by calling fadeOut 5 times, reducing alpha of your object a little each time, once it gets below a certain alpha it calls to fadeIn 5 times, until it reaches full alpha … each cycle of calls to the fadeOut/fadeIn functions it will check the life counter, and when it is zero it calls some other function in your code.

Slapped this together fairly quickly, and did not test, but looks to me like it will work… or at least get you on the right path.

Hope this helps! [import]uid: 148857 topic_id: 31664 reply_id: 126500[/import]

Hi cyberparkstudios,

Thank you, your answer does help, however, the listener never goes into the " if event.phase == ended" loop, and I don’t know why. Does the event end before it gets in the loop? If that doesn’t work, then why is the syntax for it there? Anyway, if you could lend a further hand, I’d be much obliged :slight_smile: [import]uid: 106739 topic_id: 31664 reply_id: 126521[/import]

You have to use ‘touch’ event, not ‘tap’. touch will have begin, moved, and ended phases.
I was writing you a blink function before you posted a reply. Here it is anyways if you still need it… this will make an object blink in/out 5 times when you touch it.

[code]
local blinkTrans

function startBlinking( obj )
local hasFaded
local fadeTime = 150
local blinkTotal = 5
local blinkCount = 0

–RESET TRANSITION
if (blinkTrans) then transition.cancel(blinkTrans) end
obj.alpha = 1

local function blink()
if (blinkCount < blinkTotal) then
if (not hasFaded) then
blinkTrans = transition.to(obj, {time=fadeTime, alpha=0.2, onComplete=function()
hasFaded = true
blink(obj)
end})
else
blinkTrans = transition.to(obj, {time=fadeTime, alpha=1, onComplete=function()
hasFaded=false
blinkCount = blinkCount +1
blink(obj)
end})
else blinkTrans = nil end
end

–START BLINKING
blink()
end

function onTouch(event)
if (event.phase == ‘began’) then
startBlinking(event.target)
end
end

obj:addEventListener(‘touch’, onTouch)
[/code] [import]uid: 36792 topic_id: 31664 reply_id: 126523[/import]

Here is my code without looking at the others. Sorry if this is a repeat.
[lua]local object = display.newRect ( 10, 10, 50, 50 )
local fadeTime = 600
local destructionTime = 2000

– Local Function Definition
local checkDestroyed, flashUntilDestroyed, onTap

function checkDestroyed ( )
if ( object.destroyed ) then
object:removeSelf ( )
– Play destruction sequence
else
flashUntilDestroyed ( )
end
end

function flashUntilDestroyed ( )
local alpha = 0
if ( object.alpha == 0 ) then alpha = 1 end
transition.to ( object, { time = fadeTime / 2, alpha = alpha, onComplete = checkDestroyed } )
end

function onTap ( )
flashUntilDestroyed ( )
timer.performWithDelay ( destructionTime, function () object.destroyed = true end )
end

object:addEventListener ( “tap”, onTap )[/lua] [import]uid: 7721 topic_id: 31664 reply_id: 126530[/import]

@perturbedMullusk

Correct. Tap and Touch are different. So if you take out the 'if phase == “ended” and it’s ‘end’ it should work correctly. However …

ArdentKid is very experienced and talented individual and I think you will find that code very effective, efficient, and educational.

[import]uid: 148857 topic_id: 31664 reply_id: 126539[/import]

Thank you all! Your answers have helped immensely. :slight_smile: [import]uid: 106739 topic_id: 31664 reply_id: 126623[/import]