android - Getting an exception when trying to tile ground in corona Sdk -


i'm using following code:

ground1.x = ground1.x - 10    ground2.x = ground2.x - 10    ground3.x = ground3.x - 10    ground4.x = ground4.x - 10    ground5.x = ground5.x - 10    ground6.x = ground6.x - 10    ground7.x = ground7.x - 10    ground8.x = ground8.x - 10      if(ground1.x < ( 0 - 75 ) )           ground1:removeself()                         ground1 = ground2           ground2 = ground3           ground3 = ground4           ground4 = ground5           ground6 = ground7           ground7 = ground8           local num = math.random ( 1, 4 )           ground8 = display.newimage( group, "normalground"..num..".png", ground7.x + ground7.contentwidth/2, display.contentheight - 52 ) 

to animate moving ground. i'm using 8 tiles, ground1-ground8. code inside animate function called on "enterframe".

what i'm trying detect when "ground1" has moved off left edge. then, i'm reassigning tile ground2 ground1, ground 3 ground2, etc, , @ end, creating new tile , assigning ground8.

i did similar background scrolling, working fine. however, when try run code, works while (it scrolls first 4 tiles successfully) once tries assign tile 5 ground1 , go through animation process, following exception:

attempt perform arithmetic on field 'x' (a nil value)

any ideas?

you forgot shift ground6 down ground5.

i don't know corona don't know removeself internally, i'm guessing destroys object and/or removes it's metatable such x no longer valid index. since copy object reference in ground5 ground4, 3, 2, 1, gets destroy in way, @ point ground5.x returns nil , exception saw.


tip: should never have lists of variables differ number (v1,v2,v3,etc.). that's arrays for. rather have 8 variables hold ground images, should have 1 array holds 8. can use loops perform operations shifting them n pixels.

for example, if had 8 images in list ground (like ground = {ground1,ground2,ground3,ground4,ground5,ground6,ground7,ground8}, though wouldn't initialize way), rewrite code:

-- shift ground 10 pixels right i,tile in pairs(ground)   tile.x = tile.x - 10 end  if ground[1].x < (0 - 75)   -- shift out first tile   ground[1]:removeself()                 i=1,#ground-1     ground[i] = ground[i+1]   end   -- add new tile end   local num = math.random ( 1, 4 )   ground[#ground] = display.newimage( group, "normalground"..num..".png", ground[#ground-1].x + ground[#ground-1].contentwidth/2, display.contentheight - 52 ) end 

the code more succinct, doesn't need changed if shift 10 ground tiles or 100, , avoids errors 1 made in op.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -