Previous Page  Developer's Guide: Tutorials
 
Dynamically create and clone objects
 5.3 Next Page

  • Start downloading the demo object here and import it as objects inside your theme.

Clone

  • You'll see two buttons and another textured object with two text children inside it.
  • The green button adds a new object (including its children) and stacks it below it.
  • The red button removes the last added clone.
  • This shows the preferred method to clone or "create" object. It can be seen as "creating" in that you are basically providing a "template" objects+children you want to create at runtime. This is a "template" in that it can well be hidden (Visible = No in Relation panel), but is used as prototype to create the actual object. It is much simpler and faster than actually create each object, children and all its state properties at runtime via code (and it is not currently possible).
  • The prototype object is the first one in the picture and it is left visible for simplicity.

Cloning the prototype composition

The interesting part is inside the green button script:

Sub Object_OnLButtonUp(x,y,b)
If b = False Then
    Dim i, strname
    i=0
    Do
        i = i+1
        strname = "clone_base" & i
    Loop until DesktopX.IsObject(strname)=False

    DesktopX.object("clone_base0").Clone strname, DesktopX.object("clone_base0").left, _
    DesktopX.object("clone_base"&(i-1)).bottom+10
    DesktopX.object("clone_child_A").Clone "clone_child_A" & i, _
    DesktopX.object("clone_child_A").left, DesktopX.object("clone_child_A").top
    DesktopX.Object("clone_child_A" & i).Parent = DesktopX.Object("clone_base" & i)
    DesktopX.object("clone_child_B").Clone "clone_child_B" & i, _
    DesktopX.object("clone_child_B").left, DesktopX.object("clone_child_B").top
    DesktopX.Object("clone_child_B" & i).Parent = DesktopX.Object("clone_base" & i)
End If
End Sub

The first piece of the script searches for an available "name", it just calls DesktopX.IsObject to know what existing clones already exist and find a "free" name.

Then, the steps are basically:

  • Clone the parent.
  • For each children.
    • Clone the children.
    • Assign the newly created parent to the newly created children.

Hopefully, the next DesktopX versions will provide handier methods for cloning complex compositions in one shoot, however this is the clean way to do it now.

Destroying Clones

The code for removing a clone is similar and is inside the red object:

Sub Object_OnLButtonUp(x,y,b)
If b = False Then
    Dim i, strname
    i=0
    Do
        i = i+1
        strname = "clone_base" & i
    Loop until DesktopX.IsObject(strname)=False   


    If > 1 Then
        DesktopX.object("clone_child_A" & (i-1)).delete
        DesktopX.object("clone_child_B" & (i-1)).delete
        DesktopX.object("clone_base" & (i-1)).delete
    End If
End If
End Sub

Again, it is first searching for the last created clone.

The second part of the code deletes it:

  • First it deletes each child, then it deletes the parent.
  • If objects are nested in a more complicated way, the script should first delete the most inner children, to avoid that one remains un-parented in the process.

Previous Page     Next Page