Transition an OOP display object

Hi everyone.
 
I’ve got a question regarding transition.to() or transition.from() and the best approach to implementing it.
 
I have a player object with a transition.to assigned inside its move method. It’s a simple move up, down, left and right job. There’s also a local function which is called when the transition is complete.

local function finishedMoving( obj ) print("Finished Moving") print(obj.status) end function player:move(direction, xDir, yDir) self.spriteSheet:setSequence(direction) transition.to( self.spriteSheet, { time=self.speed, delta=true, x=(64\*xDir), y=(64\*yDir), onComplete=finishedMoving } ) self.spriteSheet:play() end

As you can see, I’m passing three variables to the move method, a string (which is also the name of the spritesheet sequences for my player.spritesheet, and an x and y direction which is essentially -1, 0 or +1.

This all works as expected. But when the object is passed to the finishedMoving function, it’s passing player.spritesheet. And I want to be able to tell what player properties such as player.status when it has completed its transition.

So ideally, in the transition.to() I should be passing self (which is the player object) as the target. This would work perfectly, but then the character just plays his sequence and doesn’t move.

Any ideas? I’m really stuck on this one.

Thanks in advance.

Michael

(btw, it’s a bit confusing that you name your “sprite” as “spriteSheet”, but i get it)

restating:  so your lua object (player) “owns” the display object (sprite, aka “spriteSheet”), transition is on the display object whose onComplete listener needs to be able to access the display object’s “owner” (player)

if so, then all you need to do is give your sprite (aka “spriteSheet”) a reference to its player, fe:  player.spriteSheet.owner=player

then in listener you can reference its player as obj.owner

Thanks for this Dave. That makes sense. I was thinking that something along the lines of player.spriteSheet.parent would work but I wasn’t sure if it was overkill.

Also, you’re right, I’m going to change this to player.sprite to make it easier to understand.

Thanks again.

welcome.    fwiw .parent wouldn’t have been quite what you wanted - parent traverses up corona’s display chain, giving you the parent display object (like a scene’s view or other display group) but doesn’t know anything about your lua class, that’s where “owner” (or whatever conceptual name you prefer) comes in, but you have to set it up

(btw, it’s a bit confusing that you name your “sprite” as “spriteSheet”, but i get it)

restating:  so your lua object (player) “owns” the display object (sprite, aka “spriteSheet”), transition is on the display object whose onComplete listener needs to be able to access the display object’s “owner” (player)

if so, then all you need to do is give your sprite (aka “spriteSheet”) a reference to its player, fe:  player.spriteSheet.owner=player

then in listener you can reference its player as obj.owner

Thanks for this Dave. That makes sense. I was thinking that something along the lines of player.spriteSheet.parent would work but I wasn’t sure if it was overkill.

Also, you’re right, I’m going to change this to player.sprite to make it easier to understand.

Thanks again.

welcome.    fwiw .parent wouldn’t have been quite what you wanted - parent traverses up corona’s display chain, giving you the parent display object (like a scene’s view or other display group) but doesn’t know anything about your lua class, that’s where “owner” (or whatever conceptual name you prefer) comes in, but you have to set it up