Linking multiple transitions and functions together and repeating them

I’m having really hard time trying to come up with the best way to do this. What I want is:

1- move an object using X value

2- change its color

3- wait for few seconds

4- move it back

5- repeat

That object has its own module:

local P = {}    function P.new( group, img, x, y, width, height )   local player = display.newImageRect( group, img, width, height )   player.x = x or 0   player.y = y or 0   function player:moveRight( time,x, backX )   transition.to( self, {time=time, x=self.x + x, onComplete=function()   player:moveBack(500, backX) end} )   end   function player:moveBack( time,x )     transition.to(self, {time=time, x=x})   end   return player   end   return P

The above is a very basic behavior without repeating. I tried many different variations and used timers and passed a lot of arguments around, but things always get complex and managing them later becomes a pain, like if I want to cancel that behavior entirely. 

How do you guys usually go about doing stuff like this?

Transitions support a feature called “tags”. You can give a group of transitions the same “tag” and then you can call transition.cancel() via the tag name to cancel that group of transitions.

The idea of having transition A call transition B as it’s call back is a sound practice. Then transition B could call transition A if you wanted to do a loop. Then use tags to conveniently pause/cancel both of them.

Rob

I thought we solved this here: https://forums.coronalabs.com/topic/65512-infinite-multiple-transitions/#entry338999
 
If you want a delay and color change you can modify the answer I gave in that thread.

Also (as per Robs excellent advice), use tags to make cancelling easier.

 

local forward local backward forward = function( self ) self.onComplete = backward if( self.forwardColor ) then self:setFillColor(unpack(self.forwardColor ) ) end transition.to(self, { tag = "back\_and\_forth", x = obj.x-obj.width, delay = self.params.backwardDelay, time=2000, onComplete = self }) end backward = function( self ) self.onComplete = forward if( self.backwardColor) then self:setFillColor(unpack(self.backwardColor ) ) end transition.to(self, { tag = "back\_and\_forth", x = obj.x + obj.width, delay = self.params.forwardDelay, time=2000, onComplete = self }) end local function createRect( x, y, params ) params = params or {} local rect = display.newRect( x, y, 100, 100) rect.params = params if( params.back) then backward( rect ) else forward( rect ) end end -- No params createRect( 200, 200 ) createRect( 400, 200, { back = true, forwardDelay = 500, backwardDelay = 2000, forwardColor = { 0, 1, 0 }, backwardColor = { 1, 0, 0 } } )

@Rob - Thanks for the tip.

@roaminggamer - thanks for the continuous help. That solution in the other thread worked well for me for that situation, but when I tried to add chunks of code in between things got complicated for me.

The thing is, I always like to build systems that can be flexible and expandable instead of a couple of functions that only solve an immediate problem. So for something like this I was trying to make sure that I can add extra transitions or chunks of code later if I want to without breaking the existing code or having to modify it. 

I see.  Well, understand that it is hard to give an answer that fits those requirements only know part of the scope of your project.  This is not a complaint, just a fact.

That said, I used parameterized builders (params in my example) in my code a lot.  This helps with flexibility, future-proofing, and maintaining function signatures (i.e. no need to keep adding new arguments to the function definition).

I understand, I was just clarifying why I might have more than one topic on this subject, it’s because I’m trying to come up with a system that can be reusable across the app or by some miracle for any future apps.  I tried searching for if anyone already had a library for stuff like this but couldn’t find anything.

I built one  myself (similar to the action system you find in cocos or spriteKit)  and it was great and did everything I wanted it to do until it was time to restart the scene, then some problems appeared and I failed to follow and solve them. I’m guessing with the way Corona is built, a system like this would be near impossible to make.

I’m guessing with the way Corona is built, a system like this would be near impossible to make.

I don’t agree.  As you get better with Corona and Lua, you’ll find systems like you’re talking about are pretty easy to make.  I’m afraid it just takes lots of trial and error to get good at this.

Put in your 10,000 hours by writing and re-writing and you’ll be an expert.   :slight_smile:

PS - I don’t think it will take 10,000 hours.  That bit was just tongue-in-cheek.  

I do however believe in iteration to learn.  You’ll get there just keep trying new things.

Transitions support a feature called “tags”. You can give a group of transitions the same “tag” and then you can call transition.cancel() via the tag name to cancel that group of transitions.

The idea of having transition A call transition B as it’s call back is a sound practice. Then transition B could call transition A if you wanted to do a loop. Then use tags to conveniently pause/cancel both of them.

Rob

I thought we solved this here: https://forums.coronalabs.com/topic/65512-infinite-multiple-transitions/#entry338999
 
If you want a delay and color change you can modify the answer I gave in that thread.

Also (as per Robs excellent advice), use tags to make cancelling easier.

 

local forward local backward forward = function( self ) self.onComplete = backward if( self.forwardColor ) then self:setFillColor(unpack(self.forwardColor ) ) end transition.to(self, { tag = "back\_and\_forth", x = obj.x-obj.width, delay = self.params.backwardDelay, time=2000, onComplete = self }) end backward = function( self ) self.onComplete = forward if( self.backwardColor) then self:setFillColor(unpack(self.backwardColor ) ) end transition.to(self, { tag = "back\_and\_forth", x = obj.x + obj.width, delay = self.params.forwardDelay, time=2000, onComplete = self }) end local function createRect( x, y, params ) params = params or {} local rect = display.newRect( x, y, 100, 100) rect.params = params if( params.back) then backward( rect ) else forward( rect ) end end -- No params createRect( 200, 200 ) createRect( 400, 200, { back = true, forwardDelay = 500, backwardDelay = 2000, forwardColor = { 0, 1, 0 }, backwardColor = { 1, 0, 0 } } )

@Rob - Thanks for the tip.

@roaminggamer - thanks for the continuous help. That solution in the other thread worked well for me for that situation, but when I tried to add chunks of code in between things got complicated for me.

The thing is, I always like to build systems that can be flexible and expandable instead of a couple of functions that only solve an immediate problem. So for something like this I was trying to make sure that I can add extra transitions or chunks of code later if I want to without breaking the existing code or having to modify it. 

I see.  Well, understand that it is hard to give an answer that fits those requirements only know part of the scope of your project.  This is not a complaint, just a fact.

That said, I used parameterized builders (params in my example) in my code a lot.  This helps with flexibility, future-proofing, and maintaining function signatures (i.e. no need to keep adding new arguments to the function definition).

I understand, I was just clarifying why I might have more than one topic on this subject, it’s because I’m trying to come up with a system that can be reusable across the app or by some miracle for any future apps.  I tried searching for if anyone already had a library for stuff like this but couldn’t find anything.

I built one  myself (similar to the action system you find in cocos or spriteKit)  and it was great and did everything I wanted it to do until it was time to restart the scene, then some problems appeared and I failed to follow and solve them. I’m guessing with the way Corona is built, a system like this would be near impossible to make.

I’m guessing with the way Corona is built, a system like this would be near impossible to make.

I don’t agree.  As you get better with Corona and Lua, you’ll find systems like you’re talking about are pretty easy to make.  I’m afraid it just takes lots of trial and error to get good at this.

Put in your 10,000 hours by writing and re-writing and you’ll be an expert.   :slight_smile:

PS - I don’t think it will take 10,000 hours.  That bit was just tongue-in-cheek.  

I do however believe in iteration to learn.  You’ll get there just keep trying new things.