getting the x,y position of an objects, do i need to create a table?

Hi,

I have been struggling with this for a day or so I hope someone can help me out :slight_smile:

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]

random is totally random - so you have one chance to get the value, because any next call will be a different result.

It appears that you wish to store this value (remember only one chance) and you create it within your display object creation.

So try creating a table then populate the table with the random - and then point to the table[x] within your display object creation.

I think that should help solve your problem.

T. 

Hi ToeKnee,

yes, in my commented out code I thought i was doing that but obviously not.

could you post a small example if possible ?

or a link to a clear example?

Thanks

I’m a newbie and my exp of tables is purely numbered index

But your comment out code

                   – mutants[chaser] = chaser

                   – mutantsX[chaser] = chaser.x

                   – mutantsY[chaser] = chaser.y

and perhaps [chaser] should be swapped to i

as your loop is i = 1,4

or something like

mutantsX[i] = Random(20, screenH - 20)

kind of thing

T.

random is totally random - so you have one chance to get the value, because any next call will be a different result.

It appears that you wish to store this value (remember only one chance) and you create it within your display object creation.

So try creating a table then populate the table with the random - and then point to the table[x] within your display object creation.

I think that should help solve your problem.

T. 

Hi ToeKnee,

yes, in my commented out code I thought i was doing that but obviously not.

could you post a small example if possible ?

or a link to a clear example?

Thanks

I’m a newbie and my exp of tables is purely numbered index

But your comment out code

                   – mutants[chaser] = chaser

                   – mutantsX[chaser] = chaser.x

                   – mutantsY[chaser] = chaser.y

and perhaps [chaser] should be swapped to i

as your loop is i = 1,4

or something like

mutantsX[i] = Random(20, screenH - 20)

kind of thing

T.