deleting clones

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
mtecedor
Posts: 68
Joined: Fri Sep 04, 2009 6:40 pm

deleting clones

Post by mtecedor » Fri Oct 30, 2009 11:29 pm

Hi again,

I need help deleting clones on preOpenCard

This is the scenario:

On my card, I have a group containing a graphic and a field. I also have a button that creates clones of the whole group. Everytime a group is cloned, it receives a different name. This is my code.

on mouseUp
clone group Goval
repeat with i = 1 to the number of groups in card "graphics"
set the name of the last group to "Clone" & i
set the name of the last graphic to "oval" & i
set the name of the last field to "topic" & i
set the location of last group to 157, 176
end repeat

Now, I need to make sure that eveerytime the end user opens this card, there won't be any clone. This is the part that is not working.

on preOpenCard
if exists group ("Clone" & i) then
delete group "Clone" & i
end if
if exists (graphic "oval" & i) then
delete graphic "oval" & i
end if
if exists (field "topic" & i) then
delete field "topic" & i
end if
end preOpenCard

This is the message I get:
card "Graphics": compilation error at line 6 (Function: missing '('), char 7

Any suggestions?

Thanks

Marta

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sat Oct 31, 2009 12:07 am

Hi Marta,

I believe that you want something like this:

Code: Select all

on mouseUp
     clone group Goval
     repeat with i = 1 to the number of groups in card "graphics"
          set the name of the last group to "Clone" & i
          set the name of the last graphic to "oval" & i
          set the name of the last field to "topic" & i
          set the location of last group to 157, 176
     end repeat
end mouseUp
          
on preOpenCard   
     repeat with i = 1 to the number of groups in card "graphics"
          if exists(group ("Clone" & i)) then
               delete group ("Clone" & i)
          end if
          if exists(graphic ("oval" & i)) then
               delete graphic ("oval" & i)
          end if
          if exists(field ("topic" & i)) then
               delete field ("topic" & i)
          end if
     end repeat
end preOpenCard 
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4036
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Post by bn » Sat Oct 31, 2009 12:48 am

Marta,

this would compile but not work

Code: Select all

   if exists (group ("Clone" & i)) then
      delete group ("Clone" & i)
   end if
   if exists (graphic ("oval" & i)) then
      delete graphic "oval" & i
   end if
   if exists (field ("topic" & i)) then
      delete field ("topic" & i)
   end if
because rev does not know what i is in a preopencard handler without a repeat loop which Mark did. Then the i is a variable and is filled with a number. Otherwise it would only be i and evalutate to clonei ovali topici.

in your mouseUp handler you could also say

Code: Select all

on mouseUp
   clone group "Goval"
   put the number of groups of this card into tGroupNo
   set the name of last group to "Clone" & tGroupNo
   set the name of the last graphic to "oval" & tGroupNo
   set the name of the last field to "topic" & tGroupNo
   set the location of last group to 157, 176
end mouseUp
no need for a repeat loop here since you clone the group and you just want the numer of groups and this number will be part of the name. If you do it in a repeat loop you will rename your last group and all that is in it to clone1, clone2 up to the actual number of groups and then it will be allright but you cycled through all the names.

In your preopenstack handler you could say

Code: Select all

on preopencard
   put the number of groups of this card into tCountOfGroups
   repeat with i = tCountOfGroups down to 1
      if the short name of group i contains "clone" then delete group i
   end repeat
end preopencard
here you look for clone as part of the name and if true then you delete that group, Since the graphic and the text field are part of that group they are gone with the group. No need to delete them individually. The trick here is to count backwards. You start with the number of groups and count down to 1. This is because you delete groups which changes the numbering scheme of the groups. If you have 6 groups and you delete group 4 then group 5 becomes group 4 and group 6 becomes 5. If you count down to 1 then it does not matter if you delete group 4 of 6, the next group you are looking at is group 3 and the fact that the numbering has changed does not bother you.
regards
Bernd

mtecedor
Posts: 68
Joined: Fri Sep 04, 2009 6:40 pm

Post by mtecedor » Sat Oct 31, 2009 5:12 pm

Mark, Bernd,

Both codes work perfectly. But my original code was less elegant. Probably I have to try to remember that putting things in variables makes the program to work faster and smoothlier.

Thanks a a lot.

Marta

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”