Help: setLinearVelocity not working for second object

Hello,

I’ve created Reggie the bacteria as a physics body and set his linear velocity in the function that created him. When the player gets him to some water, he is supposed to duplicate. I’m trying to do this by calling the same function that created the first Reggie. For some reason, the second call to the function considers the setLinearVelocity method to be an error, even though it worked correctly for the first one. 

Thanks in advance for any help with this one!

-Jeff

[lua]

function createNewReg (iter, xSpot, ySpot)

  for fg = 1,10 do

    if reg[fg] == nil then --Look for the first available spot in the Reggie matrix. There will be up to ten of him.

      local reg = display.newCircle(xSpot,ySpot,4,4)

      reg:setFillColor(0.2,0.8,0.7)

      reg.xMax = 150  – These are the boundaries for his moving speed.

      reg.yMax = 150

      reg.xMin = -150

      reg.yMin = -150

      local regCollisionFilter = { categoryBits = 1, maskBits = 63 } – Reg collides with everything.

      local regBodyElement = { filter = regCollisionFilter}

      physics.addBody( reg, “dynamic”, { density=0.5, friction=0.3, bounce= 1 }, regBodyElement )

      reg.gravityScale = 0

      reg:setLinearVelocity(40,30)

      reg.myName = “Reg”

      reg.tag = fg

      reg.health = 100

      reg.accel = 1

      reg.hasGoal = 0

      transition.to(reg, {time=4000, xScale = 4, yScale = 4})

      reg[fg] = reg – Place the local reg into a spot in the reg matrix

      – Break to stop looking for an open Reggie spot

      break

    end

  end

end

[/lua]

And the collision event for touching the water:

[lua]

function onCollision (event)

  obj1 = event.object1

  obj2 = event.object2

  if obj1.myName == “water” then

    touchWater(obj2)

  end

  if obj2.myName == “water” then

    touchWater(obj1)

  end

end

[/lua]

And here the code touch water function:

[lua]

function touchWater(obj)

  

    obj.x = water.x

    obj.y = water.y

    createNewReg(1, water.x, water.y)

    display.remove(water)

    water = nil

    updateScore = 0

    timer.performWithDelay(250, scoreCount, 12) --Gradually add to the player’s score for each Reggie that’s alive.

end

[/lua]

Hi Jeff,

Do you have both a table (the “Reggie matrix”) and the locally-created object named identically as “reg”? This may be causing a reference conflict in Lua. Try changing the matrix name to something distinct like “regMatrix”.

Take care,

Brent

Brent,

Thanks for the pointer. It didn’t solve my problem, but I definitely need to remember that in the future for the cases when it might cause other problems.

It seems that the issue is caused by applying velocity while a collision is still in effect. To work around this, I decided to create all possible Reggies at the beginning of the game, but to place them far offscreen. Then, when the first Reggie gets some water, the program simply translates one to that location, rather than create a new one. This worked to allow me to apply velocity to the “new” Reggie.

Hi @zivkovic.jeff,

This is ultimately the best approach anyway. Especially when dealing with physical objects, it’s best to pre-create them and keep them offscreen, then position and use them when needed.

Take care,

Brent

Hi Jeff,

Do you have both a table (the “Reggie matrix”) and the locally-created object named identically as “reg”? This may be causing a reference conflict in Lua. Try changing the matrix name to something distinct like “regMatrix”.

Take care,

Brent

Brent,

Thanks for the pointer. It didn’t solve my problem, but I definitely need to remember that in the future for the cases when it might cause other problems.

It seems that the issue is caused by applying velocity while a collision is still in effect. To work around this, I decided to create all possible Reggies at the beginning of the game, but to place them far offscreen. Then, when the first Reggie gets some water, the program simply translates one to that location, rather than create a new one. This worked to allow me to apply velocity to the “new” Reggie.

Hi @zivkovic.jeff,

This is ultimately the best approach anyway. Especially when dealing with physical objects, it’s best to pre-create them and keep them offscreen, then position and use them when needed.

Take care,

Brent