transition.to on several objects with same name

Hi!

I’m having lots of “enemies” being created with a loop, all coming in from left to right, and I want them to fade out before moving off-screen.

Any idea on how to do this? The below code will only work for the first one…

[lua]local function fadeout(event)

if enemy.x > 300 then
Runtime:removeEventListener(“enterFrame”,fadeout)
transition.to( enemy, { time=500, alpha=0.0, } )

end

end
Runtime:addEventListener(“enterFrame”,fadeout)[/lua] [import]uid: 187595 topic_id: 32313 reply_id: 332313[/import]

what about making a for loop inside the function “fadeout” which go throw the all enemies and make the fade out for each one …

hope it help [import]uid: 118973 topic_id: 32313 reply_id: 128607[/import]

Thanks…

Can’t get this to work though…

[lua]for i = 1, #enemy do
if enemy.x > 300 then
Runtime:removeEventListener(“enterFrame”,fadeout)
transition.to( enemy, { time=500, alpha=0.0, } )
end[/lua]

Any help would be appreciated!

Thanks! [import]uid: 187595 topic_id: 32313 reply_id: 128610[/import]

Is enemy a table? Try this…

[lua]for i = 1, #enemy do
if enemy[i].x > 300 then
Runtime:removeEventListener(“enterFrame”,fadeout)
transition.to( enemy[i], { time=500, alpha=0.0, } )
end
end[/lua] [import]uid: 114363 topic_id: 32313 reply_id: 128649[/import]

@imghost,

I may not be understanding your question, but it sound like you want either:

A. To create an enemy that moves left-to-right while simultaneously fading out of sight.

or

B. To create an enemy that moves left-to-right and then fades out just and it leaves the screen.

or

C. You want the fade to reactive and not to start until the enemy is at a specific position (i.e. 300 px)

Here are some solutions ideas:

Code for use with all cases: A, B, and C:

-- Enemy builder function  
local function enemyBuilder()  
 -- Write code in this function to build the enemy  
 -- Then, return a reference to the enemy  
  
 return enemy  
end  

Case A - simultaneous move and fade

-- Assumptions:  
-- 1. Screen is portrait 320 x 480  
-- 2. Player starts at x position 10  
--   
-- Adjust solution according to your actual needs  
  
local function moveNFade( theEnemy )  
  
 - Transition the object offscreen in 2 seconds  
 - Fade out in 1.9 seconds  
 transition.to( theEnemy { time=2000, x = theEnemy.x + 330 } )  
 transition.to( theEnemy { time=1900, alpha = 0 } )  
  
end  
for i = 1, 10, do   
 local enemy = enemyBuilder()  
 moveNFade( enemy )  
end  

Case B - Move with delayed (fast) fade

-- Same assumptions as above  
  
local function moveNFade( theEnemy )  
  
 - Transition the object offscreen in 2 seconds  
 - Fade out fast starting at 1.9 seconds  
 transition.to( theEnemy { time=2000, x = theEnemy.x + 330 } )  
  
 local closure = function()  
 transition.to( theEnemy { time=100, alpha = 0 } )  
 end  
 timer.performWithDelay( 1900, closure )  
  
end  
for i = 1, 10, do   
 local enemy = enemyBuilder()  
 moveNFade( enemy )  
end  

Case C - Reactive fade, starting and x == 300+

-- Same assumptions as above  
  
local function enemyMover( theEnemy )  
 - Transition the object offscreen in 2 seconds  
 transition.to( theEnemy { time=2000, x = theEnemy.x + 330 } )  
end  
  
-- Warning this can be very costly if you have lots of enemies  
local function addFader( theEnemy )  
 local closure  
  
 closure = function()  
 if( theEnemy.x \>= 300 ) then  
 transition.to( theEnemy { time=100, alpha = 0 } )  
 else  
 theEnemy.lastTimer = timer.performWithDelay( closure, 100 )  
 end  
 end  
  
 theEnemy.lastTimer = timer.performWithDelay( closure, 100 )  
end  
  
  
end  
for i = 1, 10, do   
 local enemy = enemyBuilder()  
 enemyMover( enemy )  
 addFader( enemy )  
end  

I apologize for the length of this response, but I kind of used this as a thought exercise. It is my hope you will see something useful and not be overwhelmed.

Part of the problem is I don’t know enough about how you game works. There are many, many solutions to your question.

A few more are:

  • Have enemies collide with a sensor object at the edge of the screen and have the collision callback start the fade.

  • If you have a mover function that updates then enemy over time or as the results of touch inputs, have it check the position of the enemy and apply the fade function.

  • If none of these works, use the enterFrame event and mark the enemy as ‘alreadyFading’ or something like that so the fade code ignores it.

Best of luck with this and feel free to write back with more questions.

  • Ed
    [import]uid: 110228 topic_id: 32313 reply_id: 128650[/import]

what about making a for loop inside the function “fadeout” which go throw the all enemies and make the fade out for each one …

hope it help [import]uid: 118973 topic_id: 32313 reply_id: 128607[/import]

Thanks…

Can’t get this to work though…

[lua]for i = 1, #enemy do
if enemy.x > 300 then
Runtime:removeEventListener(“enterFrame”,fadeout)
transition.to( enemy, { time=500, alpha=0.0, } )
end[/lua]

Any help would be appreciated!

Thanks! [import]uid: 187595 topic_id: 32313 reply_id: 128610[/import]

Is enemy a table? Try this…

[lua]for i = 1, #enemy do
if enemy[i].x > 300 then
Runtime:removeEventListener(“enterFrame”,fadeout)
transition.to( enemy[i], { time=500, alpha=0.0, } )
end
end[/lua] [import]uid: 114363 topic_id: 32313 reply_id: 128649[/import]

@imghost,

I may not be understanding your question, but it sound like you want either:

A. To create an enemy that moves left-to-right while simultaneously fading out of sight.

or

B. To create an enemy that moves left-to-right and then fades out just and it leaves the screen.

or

C. You want the fade to reactive and not to start until the enemy is at a specific position (i.e. 300 px)

Here are some solutions ideas:

Code for use with all cases: A, B, and C:

-- Enemy builder function  
local function enemyBuilder()  
 -- Write code in this function to build the enemy  
 -- Then, return a reference to the enemy  
  
 return enemy  
end  

Case A - simultaneous move and fade

-- Assumptions:  
-- 1. Screen is portrait 320 x 480  
-- 2. Player starts at x position 10  
--   
-- Adjust solution according to your actual needs  
  
local function moveNFade( theEnemy )  
  
 - Transition the object offscreen in 2 seconds  
 - Fade out in 1.9 seconds  
 transition.to( theEnemy { time=2000, x = theEnemy.x + 330 } )  
 transition.to( theEnemy { time=1900, alpha = 0 } )  
  
end  
for i = 1, 10, do   
 local enemy = enemyBuilder()  
 moveNFade( enemy )  
end  

Case B - Move with delayed (fast) fade

-- Same assumptions as above  
  
local function moveNFade( theEnemy )  
  
 - Transition the object offscreen in 2 seconds  
 - Fade out fast starting at 1.9 seconds  
 transition.to( theEnemy { time=2000, x = theEnemy.x + 330 } )  
  
 local closure = function()  
 transition.to( theEnemy { time=100, alpha = 0 } )  
 end  
 timer.performWithDelay( 1900, closure )  
  
end  
for i = 1, 10, do   
 local enemy = enemyBuilder()  
 moveNFade( enemy )  
end  

Case C - Reactive fade, starting and x == 300+

-- Same assumptions as above  
  
local function enemyMover( theEnemy )  
 - Transition the object offscreen in 2 seconds  
 transition.to( theEnemy { time=2000, x = theEnemy.x + 330 } )  
end  
  
-- Warning this can be very costly if you have lots of enemies  
local function addFader( theEnemy )  
 local closure  
  
 closure = function()  
 if( theEnemy.x \>= 300 ) then  
 transition.to( theEnemy { time=100, alpha = 0 } )  
 else  
 theEnemy.lastTimer = timer.performWithDelay( closure, 100 )  
 end  
 end  
  
 theEnemy.lastTimer = timer.performWithDelay( closure, 100 )  
end  
  
  
end  
for i = 1, 10, do   
 local enemy = enemyBuilder()  
 enemyMover( enemy )  
 addFader( enemy )  
end  

I apologize for the length of this response, but I kind of used this as a thought exercise. It is my hope you will see something useful and not be overwhelmed.

Part of the problem is I don’t know enough about how you game works. There are many, many solutions to your question.

A few more are:

  • Have enemies collide with a sensor object at the edge of the screen and have the collision callback start the fade.

  • If you have a mover function that updates then enemy over time or as the results of touch inputs, have it check the position of the enemy and apply the fade function.

  • If none of these works, use the enterFrame event and mark the enemy as ‘alreadyFading’ or something like that so the fade code ignores it.

Best of luck with this and feel free to write back with more questions.

  • Ed
    [import]uid: 110228 topic_id: 32313 reply_id: 128650[/import]

Ed, thanks a lot!

I will implement this and see if I can get it to work!

Really appreciate your help!

All my best [import]uid: 187595 topic_id: 32313 reply_id: 128945[/import]

Ed, thanks a lot!

I will implement this and see if I can get it to work!

Really appreciate your help!

All my best [import]uid: 187595 topic_id: 32313 reply_id: 128945[/import]