Distance between objects problem !

How can I increase the distance between objects related to each other?

I have 10 rectangle objects with different heights in display and I want to make the distance between each other releated to height of each one. For example I need to place the second rectangle after first rectangle depends on height of first rectangle + 50 on y axis
I do this code , but I got error
For I=1 , 10 do
Rect1[i+1].y = 50 + Rect1[i].height
end
Can you demonstrate my issue?

Anther question:
If any one of rectangle is deleted by user I want to re arrange the distance between them again
I think my question is clear to your .

No need to Yell! :slight_smile:

I’ll give you a start on some logic and you can take it from there. ( BEWARE: MY CODE MAY CONTAIN TYPOS)

First, let’s create 10 rectangles of random heights,

local mRand = math.random local objs = {} for i = 1, 10 do local obj = display.newRect( 0, 0, 40, mRand(40,60)) objs[#objs+1] = obj end

Now, let’s make a function to sort the objects into position.

local sortThem( t, x, startY, tweenY ) local curY = startY for i = 1, #t do if( i \> 1) then curY = curY + t[i].contentHeight/2 end t[i].x = x t[i].y = curY curY = curY + t[i].contentHeight/2 curY = curY + tweenY end end

Now, let’s sort the objects we made:

sortThem( objs, 200, 200, 20 ) -- First at \<200,200\>, separate vertically by 20 

Now, lets delete the 5th object and re-position them (after 1 second so we can see them ‘move’)

timer.performWithDelay( 2000, function() display.remove(objs[5]) -- Delete the object table.remove( objs, 5 ) -- Remove reference from table -- Sort them again sortThem( objs, 200, 200, 20 ) end )

roaminggamer’s method is the way forward, but I just thought I’d elaborate on why your original code gave you an error so you know in future:

for i=1 , 10 do Rect1[i+1].y = 50 + Rect1[i].height end 

When you reach the last cycle of the loop (when i = 10), your table index will fail:

Rect1[i+1].y = 50 + Rect1[i].height

which is equivalent to

Rect1[11].y = 50 + Rect1[10].height 

If Rect1[11] is nil (i.e. the object does not exist), then you cannot set a y property on it.

In a case like this, you would need to check that the object exists before trying to reference it:

for i=1 , 10 do if Rect1[i+1] then Rect1[i+1].y = 50 + Rect1[i].height end end

mr Alan QuizTix

thank you igot my issue  :slight_smile:

mr roaminggamer

your idea is very useful thank you 

No need to Yell! :slight_smile:

I’ll give you a start on some logic and you can take it from there. ( BEWARE: MY CODE MAY CONTAIN TYPOS)

First, let’s create 10 rectangles of random heights,

local mRand = math.random local objs = {} for i = 1, 10 do local obj = display.newRect( 0, 0, 40, mRand(40,60)) objs[#objs+1] = obj end

Now, let’s make a function to sort the objects into position.

local sortThem( t, x, startY, tweenY ) local curY = startY for i = 1, #t do if( i \> 1) then curY = curY + t[i].contentHeight/2 end t[i].x = x t[i].y = curY curY = curY + t[i].contentHeight/2 curY = curY + tweenY end end

Now, let’s sort the objects we made:

sortThem( objs, 200, 200, 20 ) -- First at \<200,200\>, separate vertically by 20 

Now, lets delete the 5th object and re-position them (after 1 second so we can see them ‘move’)

timer.performWithDelay( 2000, function() display.remove(objs[5]) -- Delete the object table.remove( objs, 5 ) -- Remove reference from table -- Sort them again sortThem( objs, 200, 200, 20 ) end )

roaminggamer’s method is the way forward, but I just thought I’d elaborate on why your original code gave you an error so you know in future:

for i=1 , 10 do Rect1[i+1].y = 50 + Rect1[i].height end 

When you reach the last cycle of the loop (when i = 10), your table index will fail:

Rect1[i+1].y = 50 + Rect1[i].height

which is equivalent to

Rect1[11].y = 50 + Rect1[10].height 

If Rect1[11] is nil (i.e. the object does not exist), then you cannot set a y property on it.

In a case like this, you would need to check that the object exists before trying to reference it:

for i=1 , 10 do if Rect1[i+1] then Rect1[i+1].y = 50 + Rect1[i].height end end

mr Alan QuizTix

thank you igot my issue  :slight_smile:

mr roaminggamer

your idea is very useful thank you