delete into a loop repeat

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

delete into a loop repeat

Post by problème » Thu Dec 10, 2015 11:04 pm

Hello,

i scroll image and when a image leaves the screen to the left, i delete him exept the floor but after delete i have a error to the line put the short name of image x into nomImage

Code: Select all

   repeat with x=1 to the number of images of card "niveau 1"
      put x & cr after message
      put the short name of image x into nomImage
      if  not(nomImage contains "sun" ) and not(nomImage contains "sky" ) then
         set the loc of image nomImage to (item 1 of the location of image x - 10), (item 2 of the loc of image x)
         if not(nomImage contains "floor" ) then
            if the right of image nomImage < 0 then
               delete image nomImage
            end if
         end if
      end if
   end repeat

Klaus
Posts: 14191
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: delete into a loop repeat

Post by Klaus » Thu Dec 10, 2015 11:16 pm

Code: Select all

repeat with x=1 to the number of images of card "niveau 1"
If you have 10 images and you delete one, then you have 9 images left!
But your repeat loop will nevertheless go to 10! 8)

This does really make sense in these cases:

Code: Select all

repeat with x= the number of images of card "niveau 1" DOWN TO 1

:D

SparkOut
Posts: 2944
Joined: Sun Sep 23, 2007 4:58 pm

Re: delete into a loop repeat

Post by SparkOut » Thu Dec 10, 2015 11:25 pm

When looping with index from 1 upwards, if you delete an object with that index number then on the next loop your index will have been incremented but the set of objects will not have a "gap" so if you deleted image 4 then next loop you will still have an image 4 but it was image 5 last time. The index will now be 5 though, but image 5 has become image 4 and so the index no longer corresponds to the relevant image.
When looping like this where the index and the set over which you are iterating may chang, start at the end and count downwards.
repeat with x=the number of images of card "niveau 1" down to 1


Ha, outklaussed again

problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

Re: delete into a loop repeat

Post by problème » Fri Dec 11, 2015 8:41 am

thanks for yours helps

Klaus
Posts: 14191
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: delete into a loop repeat

Post by Klaus » Fri Dec 11, 2015 12:41 pm

SparkOut wrote:Ha, outklaussed again
:D :D :D

Post Reply