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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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

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:

1
2
3
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 })

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

So:

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

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

1
2
3
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 })

GOOD one Joe and Screaming Leaf :)

(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..:)

Ole' "C" programming pointers stuff...

Good One!!

You guys are smarter than you think, pat yourselves on the
back....;) ;)

Good Job!

Larry
"Follow Your Dreams!"

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...:)

hope this helps...;)

also I edited your code a little ..I don't think you need
the second function in there....but, I could be wrong...;)

1
2
3
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 })

now I'll try that and see what happens :)

Larry

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-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 })

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 ;)

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...

;)

1
2
3
4
5
6
local function buttonListener( e )
 
        if e.phase == "ended" then
               event.target.isVisible = false;
        end
end

good point, ill make the change :)

Larry

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....;)

Larry
"Follow Your Dreams"

views:1970 update:2012/2/8 8:46:10
corona forums © 2003-2011