Hi,
I have been struggling with this for a day or so I hope someone can help me out
Firstly this code works beautifully as it is from the code exchange.
I need to put the created Mutants into a table so each mutants x,y, values are available to me.
When I do put them in a table, only 1 mutant will move.
My removal isnt working either.
I have commented out my table creation code so you can see.
So do I need to put them in table to get each mutants x,ys ?
I can see each ones table entry but guess i cant access each ones x,y as they have no handle or index?
I figure to get the x.y I could just do print(mutant[1].x) print(mutant[2].x) etc to confirm I can access their positions.
This really would be cool if I can get it working the code snippet has lots potential for me.
Thanks
[lua]
function makeMutant()
– local mutants = {}
local maxChasers = 4
local gameChaserNum = display.newGroup()
function chaserCollision(self, event)
if(event.phase == “began”)then
if(event.other.name == “player”)then
timer.performWithDelay(1,
function()
if(self.chaserTimer)then
timer.cancel(self.chaserTimer)
end
self:removeEventListener(“collision”, self)
--mutants[chaser] = nil
self:removeSelf() self = nil
print(chaser)
end
)
end
end
end
local function CreateChaser()
local chaser
if(gameChaserNum.numChildren < maxChasers)then
– for i =1, 4 do
chaser = display.newRect(gameChaserNum, 20, Random(20, screenH - 20), 10, 10)
physics.addBody(chaser, “dynamic”, {isSensor = true, density = 1.0, friction = 1.0, bounce = 0.1})
chaser.collision = chaserCollision
chaser:addEventListener(“collision”, chaser)
chaser.chaseSpeed = 1.5
chaser.intent = 60
– mutants[chaser] = chaser
--end
function chaser:ChaseActor()
local xDist = player.x - self.x
local yDist = player.y - self.y
self:setLinearVelocity(xDist * self.chaseSpeed, yDist * self.chaseSpeed)
end
chaser.chaserTimer = timer.performWithDelay(chaser.intent, function() chaser:ChaseActor() end, 0)
print(chaser)
end
end
local CreateChaserTimer = timer.performWithDelay(500, CreateChaser, 30) --remove when creating with loop
--local CreateChaserTimer = timer.performWithDelay(500, CreateChaser, 30)
end
makeMutant()
[/lua]