Multiple objects position tracking

Hi guys,

I am trying to make a dogs obstacle game.

Dogs are spawned from the left and as soon as they touch the ground they start running to the right screen side (Landscape) direction.

When I have one dog that is not a problem, below functions work.

But when I have multiple dogs (spawning) and insert them into a group this does not work.

Can you help me please:

a) How can I track multiple objects positions and fire an action (setLinearVelocity) as soon as particular object (child) passes certain x point?

b) I have problem with adding Runtime to a group in this case?

c) Variables “jump_completed” and “oneJump” make no sense when multiple objects are in game, only when one object is in game?

MY RUN/JUMP/RUN CODE:

local jump_completed = false

local function onGroundCollision(event)
if(event.phase == “began” and jump_completed == false) then
 jump_completed = true
 player:setLinearVelocity(250, 0 ) – player runs when touches the ground
  end
  end

local oneJump = false
   
player:addEventListener(“collision”, onCollision)

local function jumpAgain(event)
 if (jump_completed == true and player.x > (obstacle.x - 50) and oneJump == false) then
 player:setLinearVelocity(20, - 250) – player jumps when passes obstacle.x - 50 position

 oneJump = true
 jump_completed = false
end
end

Runtime:addEventListener ( “enterFrame”, jumpAgain )

The best way would be to include jump_completed and oneJump in the dog object when it is created.

-- create your dog object here -- then create your variables myDogObj.jump\_completed = false myDogObj.oneJump = false

Then in your even listener…

local function jumpAgain(event) if event.target.jump\_completed then -- set linear velocity end end

Placing your needed variables inside the object itself (which is just a table), keeps them local to the object.

Note, you may have to modify this code a bit… i just laid it out as an example.

Hope this helps.

–john

Hi John,

Thank you for your help  :smiley: !

I have solved it.

But as always new problem arises  :smiley:

Every second spawned dog collides differently due to group anchor points I assume.

I will try to deal with that tomorrow.

Many thanks!

Ivan

The best way would be to include jump_completed and oneJump in the dog object when it is created.

-- create your dog object here -- then create your variables myDogObj.jump\_completed = false myDogObj.oneJump = false

Then in your even listener…

local function jumpAgain(event) if event.target.jump\_completed then -- set linear velocity end end

Placing your needed variables inside the object itself (which is just a table), keeps them local to the object.

Note, you may have to modify this code a bit… i just laid it out as an example.

Hope this helps.

–john

Hi John,

Thank you for your help  :smiley: !

I have solved it.

But as always new problem arises  :smiley:

Every second spawned dog collides differently due to group anchor points I assume.

I will try to deal with that tomorrow.

Many thanks!

Ivan