Page 1 of 1

delete into a loop repeat

Posted: Thu Dec 10, 2015 11:04 pm
by problème
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

Re: delete into a loop repeat

Posted: Thu Dec 10, 2015 11:16 pm
by Klaus

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

Re: delete into a loop repeat

Posted: Thu Dec 10, 2015 11:25 pm
by SparkOut
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

Re: delete into a loop repeat

Posted: Fri Dec 11, 2015 8:41 am
by problème
thanks for yours helps

Re: delete into a loop repeat

Posted: Fri Dec 11, 2015 12:41 pm
by Klaus
SparkOut wrote:Ha, outklaussed again
:D :D :D