trouble moving animation sprite (jumping)

Hi, 

  
   So I have created a “frog jumping” sprite and I am trying to make it jump across the screen.  Seems easy enough, however I am having a heck of a time getting it to work as I want.  

Right now I have the following;

local function jumpUp() local params = { x = jumpingFrog.x - 60, y = jumpingFrog.y - 60, time = 350 } transition.to(jumpingFrog, params) end local function jumpDown() local params = { x = jumpingFrog.x - 60, y = jumpingFrog.y + 60, time = 350 } transition.to(jumpingFrog, params) end local function startJumping() jumpingFrog:play() local i = 1 while (i \< 5) do timer.performWithDelay(350, jumpUp,1) timer.performWithDelay(700, jumpDown,1) i = i + 1 end end

I have the sprite animating fine at 700ms.  But I can’t seem to transition to the up and down repeatedly without just typing transition.to(…) and increase the time by 350 a bunch of times.  Obviously in the while loop the delays all pretty much get ran at the same time and it is just running the first iteration of up and down. Is there a way to write a loop that allows this to happen multiple times or do I just have to program it to and increase the time (ex, 350, 700, 1050, 1400…).  

Or is there something I am doing horribly wrong?  BTW, I am currently not using any physics in the game, but am not opposed to it if it is required to get this to work. 

-------UPDATED-----

This is how I got it working-ish, is there a better way?

local function jumpUp() local params = { x = jumpingFrog.x - 60, y = jumpingFrog.y - 60, time = 350 } transition.to(jumpingFrog, params) end local function jumpDown() local params = { x = jumpingFrog.x - 60, y = jumpingFrog.y + 60, time = 350 } transition.to(jumpingFrog, params) end local function startJumping() jumpingFrog:play() local i = 1 local delay = 350 while (i \< 5) do timer.performWithDelay(delay, jumpUp,1) delay = delay + 350 timer.performWithDelay(delay, jumpDown,1) delay = delay + 350 i = i + 1 end end

Zip it up and share it with us (tiny project showing your jumping frog).  That way folks can explore it and give feedback.

Interestingly I answered a Jumping Frog question way back in 2012. 

I no longer have the original answer archived, but I copied it and modified it for current Corona.

(This example is so old I had to convert the sprite format for modern Corona.)

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/11/frogJump.zip

  1. Please (for the love of Pete) remember to convert TABS to 3 spaces when posting code… :confused:
     

  2. This is better.

    – May contain typos – May contain typos – May contain typos local jumpUp local jumpDown jumpUp = function( self, delay ) self.onComplete = jumpDown delay = delay or 350 transition.to(self, { x = self.x - 60, y = self.y0 + 60, time = 350, onComplete = self, delay = delay } ) end jumpDown = function() self.jumps = self.jumps + 1 if( self.jumps >= self.maxJumps ) then self.onComplete = nil else self.onComplete = jumpUp end transition.to(self, { x = self.x - 60, y = self.y0, time = 350, onComplete = self } ) end local function startJumping() jumpingFrog:play() jumpingFrog.y0 = jumpingFrog.y jumpingFrog.maxJumps = 5 jumpingFrog.jumps = 0 jumpUp( jumpingFrog, 0 ) end

One Issue,  The prior answer, based on your transitions, will create a triangular jump

WaveformShapes.png
 
This will create an sinusoidal jump (I may have the order of in/out circ easings backwards, so experiment):
 

-- May contain typos -- May contain typos -- May contain typos local jumpUp local jumpDown jumpUp = function( self, delay ) self.onComplete = jumpDown delay = delay or 350 transition.cancel(self) transition.to(self, { y = self.y0 + 60, delay = delay, time = 350, transition = easing.outCirc } ) transition.to(self, { x = self.x - 60, time = 350, delay = delay, onComplete = self } ) end jumpDown = function() self.jumps = self.jumps + 1 if( self.jumps \>= self.maxJumps ) then self.onComplete = nil else self.onComplete = jumpUp end transition.cancel(self) transition.to(self, { y = self.y0, time = 350, transition = easing.inCirc } ) transition.to(self, { x = self.x - 60, time = 350, onComplete = self } ) end local function startJumping() jumpingFrog:play() jumpingFrog.y0 = jumpingFrog.y jumpingFrog.maxJumps = 5 jumpingFrog.jumps = 0 jumpUp( jumpingFrog, 0 ) end

Awesome, I got it to work with a minor tweak.

jumpDown = function(self) --add "self" because it was throwing error in jumpDown on first line (self.jumps) VS jumpDown = function()

I really appreciated this reply, i studied it a bunch and relearned alot of stuff I forgot you could do and learned some I didn’t realize you could do.  

Zip it up and share it with us (tiny project showing your jumping frog).  That way folks can explore it and give feedback.

Interestingly I answered a Jumping Frog question way back in 2012. 

I no longer have the original answer archived, but I copied it and modified it for current Corona.

(This example is so old I had to convert the sprite format for modern Corona.)

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/11/frogJump.zip

  1. Please (for the love of Pete) remember to convert TABS to 3 spaces when posting code… :confused:
     

  2. This is better.

    – May contain typos – May contain typos – May contain typos local jumpUp local jumpDown jumpUp = function( self, delay ) self.onComplete = jumpDown delay = delay or 350 transition.to(self, { x = self.x - 60, y = self.y0 + 60, time = 350, onComplete = self, delay = delay } ) end jumpDown = function() self.jumps = self.jumps + 1 if( self.jumps >= self.maxJumps ) then self.onComplete = nil else self.onComplete = jumpUp end transition.to(self, { x = self.x - 60, y = self.y0, time = 350, onComplete = self } ) end local function startJumping() jumpingFrog:play() jumpingFrog.y0 = jumpingFrog.y jumpingFrog.maxJumps = 5 jumpingFrog.jumps = 0 jumpUp( jumpingFrog, 0 ) end

One Issue,  The prior answer, based on your transitions, will create a triangular jump

WaveformShapes.png
 
This will create an sinusoidal jump (I may have the order of in/out circ easings backwards, so experiment):
 

-- May contain typos -- May contain typos -- May contain typos local jumpUp local jumpDown jumpUp = function( self, delay ) self.onComplete = jumpDown delay = delay or 350 transition.cancel(self) transition.to(self, { y = self.y0 + 60, delay = delay, time = 350, transition = easing.outCirc } ) transition.to(self, { x = self.x - 60, time = 350, delay = delay, onComplete = self } ) end jumpDown = function() self.jumps = self.jumps + 1 if( self.jumps \>= self.maxJumps ) then self.onComplete = nil else self.onComplete = jumpUp end transition.cancel(self) transition.to(self, { y = self.y0, time = 350, transition = easing.inCirc } ) transition.to(self, { x = self.x - 60, time = 350, onComplete = self } ) end local function startJumping() jumpingFrog:play() jumpingFrog.y0 = jumpingFrog.y jumpingFrog.maxJumps = 5 jumpingFrog.jumps = 0 jumpUp( jumpingFrog, 0 ) end

Awesome, I got it to work with a minor tweak.

jumpDown = function(self) --add "self" because it was throwing error in jumpDown on first line (self.jumps) VS jumpDown = function()

I really appreciated this reply, i studied it a bunch and relearned alot of stuff I forgot you could do and learned some I didn’t realize you could do.