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]