Object position tracking

Hi guys,

Please help me out here.

I am tracking spawned players x position thru Runtime. Everything works perfectly. Players jump on certain x positions.
But when I want to remove the touched object like this:
if event.phase = “began” then
display.remove(self)
self=nil
table.remove(group, self)

I get an error pointing at line where I track the objects position: “unable to compare number with nil”.

I tried to put in Runtime:
a) for a = group.numChildren,1 -1 do
– self[a] track position

b) for a = 1, group.numChildren, -1 do
– self[a] track position

c) for a = group.numChildren,1 do
– self[a] track position

d) for a = 1, group.numChildren do
– self[a] track position

I have two questions:

  1. How to remove spawned object that is position tracked in Runtime?
  2. What is the difference in above a, and c, between “-1” and no “-1” at the end?

P.S. I can change alpha to 0 on touch, but cant remove touched object from memory.

Many thanks!
Ivan

@ivan,

  1. You don’t need to nil self, nor do you need to remove it from the group.  Deleting an object automatically removes it from a display group.  Also, your code wouldn’t function in the order listed anways, but that is not the problem:

    if event.phase = “began” then display.remove(self) --self=nil --table.remove(group, self) – Would not do anything. Self is nil, so table.remove() can’t find it

  2. To solve your problem, simply check that the object is valid before accessing it.  Here is an example:

    local tmp = display.newCircle( 10, 10, 10 ) tmp.enterFrame = function( self ) – **********CORRECTED THIS LINE ************** if( self.removeSelf == nil ) then Runtime:removeEventListener( “enterFrame”, self ) return end print( self.x, self.y ) end Runtime:addEventListener( “enterFrame”, tmp ) tmp.touch = function( self, event ) if( event.phase == “ended” ) then self:removeEventListener( “touch” ) display.remove( self ) end return true end tmp:addEventListener( “touch” )

for a = group.numChildren, 1, -1 do -- self[a] track position -- REFERENCE TO SELF IS WRONG / MEANINGLESS unless self is also group -- a starts at 'numChildren' and counts backwards to 1, then stops

for a = group.numChildren,1 do -- self[a] track position -- REFERENCE TO SELF IS WRONG / MEANINGLESS unless self is also group -- a starts at numChildren, and increments... this is wrong

Do this instead:

for i = 1, group.numChildren do local x = group[i].x local y = group[i].y -- i starts at 1 and increments till it hits group.numChildren, then stops after executing

Hi roaminggamer,

Thank you for a quick reply.

Above code from your “tmp” Circle example does not work   :frowning:

I tried to test your code as an example.

Error is  “eof expect end” at line:

print( self.x, self.y )

When I put “end” at or before that line I get the same error (“eof expect end”)

How to fix above code?

Many thanks!  :slight_smile:

Ivan

if you don’t know the diference of a) and c) i think you need to read more basic loops theory before go to practice…you will save a lot of time on the long run…

“for”  loop is divided in 3 steps: first value is the start value, second value is the end value, third  value is the steps that the loop will do while running from first value  to second value.

for i=(start value), (finish value), (step value) do

examples to understand more:

for i=1, 10, 1 do – this will loop 10 times and i will print 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (last value tells that the increment of i is 1. i=i+1.

print (i)

end


for i=1, 10 do – same as above, the last value can be omited because 1 is the standard increment value

print (i)

end


for i=1, 10, 2 do – will only loop 5 times and will print (1, 3, 5, 7, 9) why? because the last value (2) will increment i=i+2

print (i)

end

— if you need to loop descendent values you need the negative steps…

for i=10, 1, -1 do – will print (10,9,8,7,6,5,4,3,2,1) because the step is -1. it will decrement i (i=i-1)

print (i)

end


for i=10,1 , 1 do – *WRONG* this is wrong and will not do anyting because step value (1) is not negative…code will increment i=i+1 and last value/(1) is lower than the first(10).

print (i)

end


for i=1, 10, -1 do – *WRONG*  i=i-1 and first value is 1, last is 10…because your putting i=i-1 your telling that i will have 1, 0, -1, -2 ,-3, -4 which is outside your loop parameters (1-10)

hope this helps…my english is not my native language so i can’t help you much more than this…

Hi carloscosta,

Many thanks for your explanation.

Best regards.

Ivan

@Ivan,

The second line of the example was missing the keyword ‘function’.  It is now fixed.

Please realize very rarely will I actually test the code I give.  It was meant as an example to show the principle.  Thus, typos are sometimes present.

Having said that, with a single fix the code now runs and show the example I tried to explain.

Hi RoamingGamer,

Many thanks!
You are the best! :slight_smile:

Ivan

@ivan,

  1. You don’t need to nil self, nor do you need to remove it from the group.  Deleting an object automatically removes it from a display group.  Also, your code wouldn’t function in the order listed anways, but that is not the problem:

    if event.phase = “began” then display.remove(self) --self=nil --table.remove(group, self) – Would not do anything. Self is nil, so table.remove() can’t find it

  2. To solve your problem, simply check that the object is valid before accessing it.  Here is an example:

    local tmp = display.newCircle( 10, 10, 10 ) tmp.enterFrame = function( self ) – **********CORRECTED THIS LINE ************** if( self.removeSelf == nil ) then Runtime:removeEventListener( “enterFrame”, self ) return end print( self.x, self.y ) end Runtime:addEventListener( “enterFrame”, tmp ) tmp.touch = function( self, event ) if( event.phase == “ended” ) then self:removeEventListener( “touch” ) display.remove( self ) end return true end tmp:addEventListener( “touch” )

for a = group.numChildren, 1, -1 do -- self[a] track position -- REFERENCE TO SELF IS WRONG / MEANINGLESS unless self is also group -- a starts at 'numChildren' and counts backwards to 1, then stops

for a = group.numChildren,1 do -- self[a] track position -- REFERENCE TO SELF IS WRONG / MEANINGLESS unless self is also group -- a starts at numChildren, and increments... this is wrong

Do this instead:

for i = 1, group.numChildren do local x = group[i].x local y = group[i].y -- i starts at 1 and increments till it hits group.numChildren, then stops after executing

Hi roaminggamer,

Thank you for a quick reply.

Above code from your “tmp” Circle example does not work   :frowning:

I tried to test your code as an example.

Error is  “eof expect end” at line:

print( self.x, self.y )

When I put “end” at or before that line I get the same error (“eof expect end”)

How to fix above code?

Many thanks!  :slight_smile:

Ivan

if you don’t know the diference of a) and c) i think you need to read more basic loops theory before go to practice…you will save a lot of time on the long run…

“for”  loop is divided in 3 steps: first value is the start value, second value is the end value, third  value is the steps that the loop will do while running from first value  to second value.

for i=(start value), (finish value), (step value) do

examples to understand more:

for i=1, 10, 1 do – this will loop 10 times and i will print 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (last value tells that the increment of i is 1. i=i+1.

print (i)

end


for i=1, 10 do – same as above, the last value can be omited because 1 is the standard increment value

print (i)

end


for i=1, 10, 2 do – will only loop 5 times and will print (1, 3, 5, 7, 9) why? because the last value (2) will increment i=i+2

print (i)

end

— if you need to loop descendent values you need the negative steps…

for i=10, 1, -1 do – will print (10,9,8,7,6,5,4,3,2,1) because the step is -1. it will decrement i (i=i-1)

print (i)

end


for i=10,1 , 1 do – *WRONG* this is wrong and will not do anyting because step value (1) is not negative…code will increment i=i+1 and last value/(1) is lower than the first(10).

print (i)

end


for i=1, 10, -1 do – *WRONG*  i=i-1 and first value is 1, last is 10…because your putting i=i-1 your telling that i will have 1, 0, -1, -2 ,-3, -4 which is outside your loop parameters (1-10)

hope this helps…my english is not my native language so i can’t help you much more than this…

Hi carloscosta,

Many thanks for your explanation.

Best regards.

Ivan

@Ivan,

The second line of the example was missing the keyword ‘function’.  It is now fixed.

Please realize very rarely will I actually test the code I give.  It was meant as an example to show the principle.  Thus, typos are sometimes present.

Having said that, with a single fix the code now runs and show the example I tried to explain.

Hi RoamingGamer,

Many thanks!
You are the best! :slight_smile:

Ivan