@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]