Dog jumps over obstacles

Hi guys,

I would very much appreciate your help.

I am making Dog jump over obstacles game.

My code is as follows:

local function jumpAgain(event)

if (player.jump_completed == true and player.x < (obstacle1.x - 100) and player.oneJump == false and player.jumped == false) then

player:setLinearVelocity(150, -100)

player.oneJump = true

player.jump_completed = false

player.jumped = true

end

end

local function jumpAgain2(event)

if (player.jump_completed == false and player.x <  (obstacle2.x -50) and player.oneJump == true and player.jumped == true) then

player:setLinearVelocity(150, - 100)

player.oneJump = false

player.jump_completed = true

player.jumped = false

end

end

Runtime:addEventListener ( “enterFrame”, jumpAgain )

Runtime:addEventListener ( “enterFrame”, jumpAgain2 )

I have three short questions regarding this topic:

  • My player jumps over first obstacle but not over second obstacle (it lands before obstacle2.x - 50)?

  • Is there a better way to automate this code?

  • Is there a better function than setLinearVelocity for this?

Many thanks.

Ivan

Hi Ivan,

This may not be exactly related to your current code, but please check out the following tutorial on “Can I Jump?”. This shows how to use a “foot sensor” to detect when an object is currently on the ground vs. in the air, allowing you to more accurately check if the object should jump (or can jump).

https://coronalabs.com/blog/2013/02/19/more-physics-tricks-explained/

Best regards,

Brent

Hi Brent,

Many thanks for your reply.

I have implemented foot sensor.

I have two last questions:

  1. Why jump cannot be detected/triggered when I do:

local function jump(event)

if (player.x == (obstacle.x -70) then

– do jump action

Runtime:addEventListener(“enterFrame”, jump)

But when I replace == with < or > jump works OK?

  1. How would you implement below code on group of objects where Group is called Dogs and Dog is group element?

local character = display.newRect(100, 300, 120, 120)

character:setFillColor(51, 223, 0)

physics.addBody ( character, “dynamic”, {density=1.0 , friction=0.0, bounce=0.0 },

{shape = {20, 00, 20, 65, -20, 65, -20, 0}, isSensor = true }

)

character.isFixedRotation = true

character.canJump = 0

stage:insert(character)

function touchAction(event)

if ( event.phase == “began” and character.canJump > 0) then

character:setLinearVelocity(100, - 300)

end

end

stage:addEventListener(“touch”, touchAction)

function charCollide( self, event )

if (event.selfElement == 2 and event.other.objType == “ground” ) then

if( event.phase == “began” ) then

self.canJump = self.canJump + 1

elseif( event.phase == “ended” ) then

self.canJump = self.canJump - 1

end

end

end

character.collision = charCollide;

character:addEventListener( “collision”, character )

Many, many thanks!

Best regards.

Ivan

Hi Ivan,

For #1, the == operator will never (or almost never) trigger because it’s nearly impossible that the player’s X will be -70 of the obstacle. Depending on how fast things are moving, on any particular time-step it might be -71, -78, -74.465585, -68.35447, or virtually anything in between. So, you need to use either the < or > depending on the direction of movement.

For #2, I don’t really understand your question. What are you attempting to accomplish there?

Best regard,

Brent

Hi Brent, #1 Many thanks for your reply. #2 This code is from “foot” tutorial you suggested. I have implemented that code for one “character”. But I would like to implement that code to a group of characters that are created thru Spawn function. Lets assume Group is called Alldogs and spawned objects are dogs. How to do that (implement one foot code for multiple objects that are beign spawned)? Many thanks. Ivan

Hi Ivan,

Basically, every “dog” will need to be created just like “character”, with its own foot sensor. But then, for each dog that is spawned, you can use the same “charCollide()” function.

Of course I should ask, if there are multiple dogs, what does touching the screen do? Does it make ALL dogs jump? That would be a separate issue to work out…

Brent

Hi Brent,

Touch in not triggering Dogs group jump, timer performWithDelay does. Dogs group are beign spawned as dogs, but I get error: “attempt to index a global “dog” (a nil value)… in main chunk”… 

I get that error pointing at this like of code: “dog.collision = charCollide”

How to fix below code?

function jumpAction(event)

if ( dog.canJump > 0) then

dog:setLinearVelocity(100, 0)

end

end

jumping = timer.performWithDelay ( 1000, jumpAction , 0 )

function charCollide( self, event )

if (event.selfElement == 2 and event.other.objType == “ground” ) then

if( event.phase == “began” ) then

self.canJump = self.canJump + 1

elseif( event.phase == “ended” ) then

self.canJump = self.canJump - 1

end

end

end

dog.collision = charCollide;

dogs:addEventListener( “collision”, dog)

Many thanks.

Ivan

Hi Ivan,

The error you’re getting is a “scope” issue. If you don’t fully understand scope, you should consult the following tutorial. Scope is a crucial aspect of programming in Lua, and you should become 100% familiar with the concept before proceeding with your project.

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Best regards,

Brent

Thank you Brent.

Best regards.

Ivan

Hi Ivan,

This may not be exactly related to your current code, but please check out the following tutorial on “Can I Jump?”. This shows how to use a “foot sensor” to detect when an object is currently on the ground vs. in the air, allowing you to more accurately check if the object should jump (or can jump).

https://coronalabs.com/blog/2013/02/19/more-physics-tricks-explained/

Best regards,

Brent

Hi Brent,

Many thanks for your reply.

I have implemented foot sensor.

I have two last questions:

  1. Why jump cannot be detected/triggered when I do:

local function jump(event)

if (player.x == (obstacle.x -70) then

– do jump action

Runtime:addEventListener(“enterFrame”, jump)

But when I replace == with < or > jump works OK?

  1. How would you implement below code on group of objects where Group is called Dogs and Dog is group element?

local character = display.newRect(100, 300, 120, 120)

character:setFillColor(51, 223, 0)

physics.addBody ( character, “dynamic”, {density=1.0 , friction=0.0, bounce=0.0 },

{shape = {20, 00, 20, 65, -20, 65, -20, 0}, isSensor = true }

)

character.isFixedRotation = true

character.canJump = 0

stage:insert(character)

function touchAction(event)

if ( event.phase == “began” and character.canJump > 0) then

character:setLinearVelocity(100, - 300)

end

end

stage:addEventListener(“touch”, touchAction)

function charCollide( self, event )

if (event.selfElement == 2 and event.other.objType == “ground” ) then

if( event.phase == “began” ) then

self.canJump = self.canJump + 1

elseif( event.phase == “ended” ) then

self.canJump = self.canJump - 1

end

end

end

character.collision = charCollide;

character:addEventListener( “collision”, character )

Many, many thanks!

Best regards.

Ivan

Hi Ivan,

For #1, the == operator will never (or almost never) trigger because it’s nearly impossible that the player’s X will be -70 of the obstacle. Depending on how fast things are moving, on any particular time-step it might be -71, -78, -74.465585, -68.35447, or virtually anything in between. So, you need to use either the < or > depending on the direction of movement.

For #2, I don’t really understand your question. What are you attempting to accomplish there?

Best regard,

Brent

Hi Brent, #1 Many thanks for your reply. #2 This code is from “foot” tutorial you suggested. I have implemented that code for one “character”. But I would like to implement that code to a group of characters that are created thru Spawn function. Lets assume Group is called Alldogs and spawned objects are dogs. How to do that (implement one foot code for multiple objects that are beign spawned)? Many thanks. Ivan

Hi Ivan,

Basically, every “dog” will need to be created just like “character”, with its own foot sensor. But then, for each dog that is spawned, you can use the same “charCollide()” function.

Of course I should ask, if there are multiple dogs, what does touching the screen do? Does it make ALL dogs jump? That would be a separate issue to work out…

Brent

Hi Brent,

Touch in not triggering Dogs group jump, timer performWithDelay does. Dogs group are beign spawned as dogs, but I get error: “attempt to index a global “dog” (a nil value)… in main chunk”… 

I get that error pointing at this like of code: “dog.collision = charCollide”

How to fix below code?

function jumpAction(event)

if ( dog.canJump > 0) then

dog:setLinearVelocity(100, 0)

end

end

jumping = timer.performWithDelay ( 1000, jumpAction , 0 )

function charCollide( self, event )

if (event.selfElement == 2 and event.other.objType == “ground” ) then

if( event.phase == “began” ) then

self.canJump = self.canJump + 1

elseif( event.phase == “ended” ) then

self.canJump = self.canJump - 1

end

end

end

dog.collision = charCollide;

dogs:addEventListener( “collision”, dog)

Many thanks.

Ivan

Hi Ivan,

The error you’re getting is a “scope” issue. If you don’t fully understand scope, you should consult the following tutorial. Scope is a crucial aspect of programming in Lua, and you should become 100% familiar with the concept before proceeding with your project.

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Best regards,

Brent

Thank you Brent.

Best regards.

Ivan