Getting character to jump one pad at a time

Hi,

I’ve an animated frog can that jump lilypads from side of the screen to the other. However, I only want the frog to jump one pad at a time here is the code that allows the frog to jump to any lilypad when it is clicked on

local function padTouched(event)  
 local pad = event.target  
 if event.phase == "ended" then  
 local angleBetween = math.ceil(math.atan2( (pad.y - myFrog.y), (pad.x - myFrog.x) ) \* 180 / math.pi ) + 90  
 myFrog.rotation = angleBetween  
 myFrog:play()  
 transition.to ( myFrog, { time=frogJumpSpeed, x=pad.x, y=pad.y, transition=easing.inOutQuad } )  
 end  
end  

[import]uid: 167310 topic_id: 33314 reply_id: 333314[/import]

@lblake,

I didn’t see an actual question. Do you want to disable padTouched() while a jump is happening?

There are lots of ways to do this, but perhaps the simplest is to use a callback and a flag:

[lua]local isJumping = false --EFM

local jumpComplete() --EFM
isJumping = false
end

local function padTouched(event)
local pad = event.target
if event.phase == “ended” then
if( isJumping ) then --EFM
return true
end

isJumping = true --EFM

local angleBetween = math.ceil( math.atan2( (pad.y - myFrog.y),
(pad.x - myFrog.x) ) * 180 / math.pi ) + 90
myFrog.rotation = angleBetween
myFrog:play()
transition.to ( myFrog, {
time=frogJumpSpeed,
x=pad.x,
y=pad.y,
transition=easing.inOutQuad,
onComplete = jumpComplete } ) --EFM
end
end[/lua]

Look for my initials (EFM) to find the changes I made to your code.

  1. isJumping is the flag. I used it (above) to prevent another jump from occurring till the last one is complete.

  2. jumpComplete is a callback. I added code to your original code to have it called when the transition ends. This callback’s only job is to clear the isJumping flag. [import]uid: 110228 topic_id: 33314 reply_id: 132376[/import]

Hi there emaurina,

What I want to do is limit the frog to only jump one pad at a time rather then being able to jump as many pads as I select with the code I posted in my question. I am not sure if your solution achieves this? [import]uid: 167310 topic_id: 33314 reply_id: 132394[/import]

First, sorry. I was giving you a hard time because I wasn’t really sure what you were looking for.

Second, I’m still not, but let me take a stab.

For the sake of example, I will assume you have this situation:

  1. Lily pads left-to-right: #1 --> #2 – > #3 – > #4 – > … – > N

  2. A frog on the left side of the screen.

  3. You allow users to tap a lily pad to get the frog to jump from where it is to that lily pad.

  4. You want the frog to jump from #1 to #2 to #3

  5. You don’t want a situation where people click too many lily pads too quickly and the frog jumps oddly. i.e If a user quickly tapped Lilly pad #2 then #3 (before the jump from #1 to #2 was done), the frog could jump right to #3 or move strangely and then move to #3.

  6. You don’t want the player to be able to bypass a Lilly pad. i.e. If the frog is at #2, you don’t want to be able to jump right to #4. Only adjacent jumps should be allowed.

Am I missing any constraints, or have I imagined your situation incorrectly?

So, what does my code change do?

Well, assuming all Lilly pads call that same function and assuming that all Lilly pads have visibility to the isJumping variable, then the code I wrote will solve #5.

It won’t solve # 6. However, you can solve #6 by assigning all Lilly pads unique numbers and having your code check to see if that number is +/- 1 for the frogs current location. This is a simple solution for a linear case. It won’t work as easily for Lilly pads in a grid or if your jumping rule is distance based and not just adjacency based.

Note: If you do decide to use this idea for solving #6, do this:

  1. When you create your Lily pads, do so left to right and assign them a number:
    [lua]for i = 1, 5 do – create 5 pads
    local pad = newPad() – Your code to create a pad
    pad.myPadNumber = i
    end[/lua]

  2. Assign a number to the frog/player based on the pad it starts at: [lua]frog.atPad = 1[/lua]

  3. Every time you jump to a new pad, assign that pads number to the frog (code marked w/ EFM)
    [lua]local isJumping = false

local function padTouched(event)
local pad = event.target
if event.phase == “ended” then
if( isJumping ) then
return true
end

if( math.abs( myFrog.atPad - pad.myPadNumber ) > 1 ) – EFM Too far
return true
end

isJumping = true

local angleBetween = math.ceil( math.atan2( (pad.y - myFrog.y),
(pad.x - myFrog.x) ) * 180 / math.pi ) + 90
myFrog.rotation = angleBetween
myFrog:play()

myFrog.atPad = pad.myPadNumber --EFM ( change as jump starts )

transition.to ( myFrog, {
time=frogJumpSpeed,
x=pad.x,
y=pad.y,
transition=easing.inOutQuad,
onComplete = jumpComplete } )
end
end[/lua]

Again, sorry for the slight jibe in my last e-mail. Let me know if I missed the mark or if more clarification is needed
[import]uid: 110228 topic_id: 33314 reply_id: 132418[/import]

I never noticed the jibe and I’ll take a look at the code to see if it works for me… [import]uid: 167310 topic_id: 33314 reply_id: 132429[/import]

@lblake,

I didn’t see an actual question. Do you want to disable padTouched() while a jump is happening?

There are lots of ways to do this, but perhaps the simplest is to use a callback and a flag:

[lua]local isJumping = false --EFM

local jumpComplete() --EFM
isJumping = false
end

local function padTouched(event)
local pad = event.target
if event.phase == “ended” then
if( isJumping ) then --EFM
return true
end

isJumping = true --EFM

local angleBetween = math.ceil( math.atan2( (pad.y - myFrog.y),
(pad.x - myFrog.x) ) * 180 / math.pi ) + 90
myFrog.rotation = angleBetween
myFrog:play()
transition.to ( myFrog, {
time=frogJumpSpeed,
x=pad.x,
y=pad.y,
transition=easing.inOutQuad,
onComplete = jumpComplete } ) --EFM
end
end[/lua]

Look for my initials (EFM) to find the changes I made to your code.

  1. isJumping is the flag. I used it (above) to prevent another jump from occurring till the last one is complete.

  2. jumpComplete is a callback. I added code to your original code to have it called when the transition ends. This callback’s only job is to clear the isJumping flag. [import]uid: 110228 topic_id: 33314 reply_id: 132376[/import]

Hi there emaurina,

What I want to do is limit the frog to only jump one pad at a time rather then being able to jump as many pads as I select with the code I posted in my question. I am not sure if your solution achieves this? [import]uid: 167310 topic_id: 33314 reply_id: 132394[/import]

First, sorry. I was giving you a hard time because I wasn’t really sure what you were looking for.

Second, I’m still not, but let me take a stab.

For the sake of example, I will assume you have this situation:

  1. Lily pads left-to-right: #1 --> #2 – > #3 – > #4 – > … – > N

  2. A frog on the left side of the screen.

  3. You allow users to tap a lily pad to get the frog to jump from where it is to that lily pad.

  4. You want the frog to jump from #1 to #2 to #3

  5. You don’t want a situation where people click too many lily pads too quickly and the frog jumps oddly. i.e If a user quickly tapped Lilly pad #2 then #3 (before the jump from #1 to #2 was done), the frog could jump right to #3 or move strangely and then move to #3.

  6. You don’t want the player to be able to bypass a Lilly pad. i.e. If the frog is at #2, you don’t want to be able to jump right to #4. Only adjacent jumps should be allowed.

Am I missing any constraints, or have I imagined your situation incorrectly?

So, what does my code change do?

Well, assuming all Lilly pads call that same function and assuming that all Lilly pads have visibility to the isJumping variable, then the code I wrote will solve #5.

It won’t solve # 6. However, you can solve #6 by assigning all Lilly pads unique numbers and having your code check to see if that number is +/- 1 for the frogs current location. This is a simple solution for a linear case. It won’t work as easily for Lilly pads in a grid or if your jumping rule is distance based and not just adjacency based.

Note: If you do decide to use this idea for solving #6, do this:

  1. When you create your Lily pads, do so left to right and assign them a number:
    [lua]for i = 1, 5 do – create 5 pads
    local pad = newPad() – Your code to create a pad
    pad.myPadNumber = i
    end[/lua]

  2. Assign a number to the frog/player based on the pad it starts at: [lua]frog.atPad = 1[/lua]

  3. Every time you jump to a new pad, assign that pads number to the frog (code marked w/ EFM)
    [lua]local isJumping = false

local function padTouched(event)
local pad = event.target
if event.phase == “ended” then
if( isJumping ) then
return true
end

if( math.abs( myFrog.atPad - pad.myPadNumber ) > 1 ) – EFM Too far
return true
end

isJumping = true

local angleBetween = math.ceil( math.atan2( (pad.y - myFrog.y),
(pad.x - myFrog.x) ) * 180 / math.pi ) + 90
myFrog.rotation = angleBetween
myFrog:play()

myFrog.atPad = pad.myPadNumber --EFM ( change as jump starts )

transition.to ( myFrog, {
time=frogJumpSpeed,
x=pad.x,
y=pad.y,
transition=easing.inOutQuad,
onComplete = jumpComplete } )
end
end[/lua]

Again, sorry for the slight jibe in my last e-mail. Let me know if I missed the mark or if more clarification is needed
[import]uid: 110228 topic_id: 33314 reply_id: 132418[/import]

I never noticed the jibe and I’ll take a look at the code to see if it works for me… [import]uid: 167310 topic_id: 33314 reply_id: 132429[/import]