transition.to - onComplete Help - Small issue maybe i just dont understand.

Please see the code below.

My function to spawn multiple enemies works fine, they span and move to the desired location from random positions of the screen.

I capture their starting points in the homeX, and homeY properties.

My issue is that when the transition.to - completes and the sendEnemyHome home fires. I receive the following error.

Error ->> attempt to index local ‘group’ (a nil value)

If i just try to print anything out for the event i get a ‘nil’

anyone know why the onComplete does not act like other events and sends the object it completed on?

If I am doing something wrong could someone please point it out.

thanks in advance Larry

[code]

local sendEnemyHome = function(event)
local group = event.target

transition.to(group, {time = group.velocity, x = group.homeX, y = group.homeY }) --move back to where we came from

end
local function spawnEnemies( event )

local x,y, velocity, iDirection;
local group = display.newGroup()
local enemyMouse

x,y, velocity, iDirection = getRandomXY()
enemyMouse = display.newImage( mouseimage … iDirection … “.png” )
group:insert( enemyMouse, true ) – accessed in buttonListener as group[1]

group:translate( x,y ) --put it on the screen in the starting randome position

group.homeX = x; group.homeY = y; group.velocity = velocity; group.iDirection =iDirection --Store the home positions to return to

transition.to(group, {time = velocity, x = oCheese.x, y = oCheese.y, delay=100, onComplete=sendEnemyHome }) --start moving the enemy to the cheese

end
[/code] [import]uid: 11860 topic_id: 21018 reply_id: 321018[/import]

the onComplete isn’t a listener function, so there is no event associated with it.

And as far as I know you can’t pass parameters to the onComplete function. So the “event” parameter you have will always be nil.

You could do something like this for your transition.to:

transition.to(group, {time = velocity, x = oCheese.x, y = oCheese.y, delay=100, onComplete=function() transition.to(group, {time = group.velocity, x = group.homeX, y = group.homeY }) end }) [import]uid: 94868 topic_id: 21018 reply_id: 83000[/import]

I’m pretty sure that although it is not a listener, it does pass a reference to itself that you can track.

So:

local sendEnemyHome = function(target)  
 print(target.x)  
end  
  
tweener = transition.to (myActor, {x=100, time=2000, onComplete=sendEnemyHome})  

I think that works… I believe I use that somewhere.
Somebody smarter should confirm though. :wink: [import]uid: 8444 topic_id: 21018 reply_id: 83005[/import]

Thanks, that actually works perfect, but it appears that i may not be able to achieve what I want using this method.

I added code to work like this

transition.to(group, {time = velocity, x = oCheese.x, y = oCheese.y, delay=100, onComplete=function()  
 transition.to(group, {time = group.velocity, x = group.homeX, y = group.homeY, onComplete=function() updateDamage(group) })  
end })  

You see the mouse has a button listener added to it. When you touch the mouse it sets the object’s isVisible = false, and have tried to use group:removeSelf()

But what is still happening is the onComplete still fires and the updateDamage is called which does what it says…

I check to see if the isVisible is true or false

I remove the object when the user touches the mouse, but the function to update the damage during the oncomplete is still called and the group.isVisible says its still visible for some reason.
Any thoughts?
[import]uid: 11860 topic_id: 21018 reply_id: 83021[/import]

GOOD one Joe and Screaming Leaf :slight_smile:

(can’t use event, but a target… )

This is correct, in the sendEnemyHome function group is out
of scope (can’t see it) it’s local to spawnEnemies() only…
and also the NEW version of “local group” made inside sendEnemyHome is just a variable and equal to NIL because EVENT is NIL (at this point anyway) because NO listener…

So by using TARGET your passing a pointer to memory that
references to the the first GROUP inside spawnEnemies

TARGET is referenced by GROUP in the: transition.to(GROUP, {time = velocity, x = oCheese.x,…etc function…:slight_smile:
Ole’ “C” programming pointers stuff…
Good One!!

You guys are smarter than you think, pat yourselves on the
back…:wink: :wink:

Good Job!

Larry
“Follow Your Dreams!” [import]uid: 107633 topic_id: 21018 reply_id: 83023[/import]

doubleslashdesign,

Try using target in the updateDamage function…
like: target.removeSelf();

(you still can’t put group or arguments when using “onComplete”), because you already have that at the beginning
of transition.to(group, etc… group is the TARGET…:slight_smile:

hope this helps…:wink:

also I edited your code a little …I don’t think you need
the second function in there…but, I could be wrong…:wink:

transition.to(group, {time = velocity, x = oCheese.x, y = oCheese.y, delay=100, onComplete=function() transition.to(group, {time = group.velocity, x = group.homeX, y = group.homeY, onComplete = updateDamage }) end })

Hope it all works for ya…
Happy coding…
Larry
“Follow Your Dreams!”
[import]uid: 107633 topic_id: 21018 reply_id: 83030[/import]

now I’ll try that and see what happens :slight_smile:

Larry [import]uid: 11860 topic_id: 21018 reply_id: 83031[/import]

thanks everyone for the assist, I was able to get this working, and pass the object to the updateDamage function.

Because the onComplete fires weather or not the object has been removed I was able to get around it.

In my touch event I set the enemyMice.isVisible to false and then when the updateDamage function is called it checks that property and knows if it should apply damage to the cheese or not.

i was able to pass the object to the updateDamage as shown below.

-oh happy day-

[code]

local updateDamage = function(target)

if target.isVisible then
—do wonderful code here for updating damage
end
end

local buttonListener = function( event )
if “ended” == event.phase then
local tappedObject = event.target
tappedObject.isVisible = false
end
end

transition.to(enemyMice, {time = velocity, x = _imgCheese.x, y = _imgCheese.y, delay=100, onComplete=function()
updateDamage(enemyMice); transition.to(enemyMice, {time = enemyMice.velocity, x = enemyMice.homeX, y = enemyMice.homeY, onComplete=function() enemyMice:removeSelf() end })
end })
[/code] [import]uid: 11860 topic_id: 21018 reply_id: 83167[/import]

Hey doubleslashdesign,

I’m glad it all worked out for ya… hey, if you get a chance
copy/paste the code below and let me know if it works
with your code :wink:

I was just looking at optimizing, and getting rid of the
local variable “tappedObject”…also just used a function
assignment rather a “function = …” variable …assignment

local variables (assignments) take “mov” statements in low level programming and anytime we can get rid of or not use
them,…just a little…helps keep the code clean and fast…

:wink:

local function buttonListener( e )  
  
 if e.phase == "ended" then  
 event.target.isVisible = false;  
 end  
end  

Happy coding and again glad you got it all working :slight_smile:
Happy Regards,

Larry
“Follow Your Dreams”

[import]uid: 107633 topic_id: 21018 reply_id: 83183[/import]

good point, ill make the change :slight_smile:

Larry [import]uid: 11860 topic_id: 21018 reply_id: 83201[/import]

Ha…just noticed a boo boo “event.target.isVisible” should be
“e.target.isVisible”…wow…

Also just noticed (need sleep alert is going off)…

Your name: Larry - Joined 27 Nov 2010
My name: Larry - Joined 28 Nov 2011

Have fun…:wink:

Larry
“Follow Your Dreams”

[import]uid: 107633 topic_id: 21018 reply_id: 83207[/import]