Repeat

The repeat object is designed to simplify cases where you have a large number of very similar objects nearby each other.

Explanation

The repeat object generates "count" copies of any objects nested within it. Since you can not have multiple copies of the same named object you insert "[n]" somewhere in the name. After parsing, the "n" is replaced with the index of the generated object in the repeat.

You can also specify the variable that is replaced as the value of the var attribute. This is useful if you need nested repeat objects.

Important: The repeat element does not automatically adjust child element sizing.

Example

For example, the following XML could be simplified by using a repeat element.

<object name="myObj[0]" type="sprite" sprite="mySprite"/>
<object name="myObj[1]" type="sprite" sprite="mySprite"/>
<object name="myObj[2]" type="sprite" sprite="mySprite"/>

The next snippet uses the repeat element to create the same result.

<repeat count="3">
    <object name="myObj[n]" type="sprite" sprite="mySprite"/>
</repeat>

Example - Nested

For example, the following XML could be simplified by using a repeat element.

<object name="myObj[0][0]" type="sprite" sprite="mySprite"/>
<object name="myObj[0][1]" type="sprite" sprite="mySprite"/>
<object name="myObj[1][0]" type="sprite" sprite="mySprite"/>
<object name="myObj[1][1]" type="sprite" sprite="mySprite"/>

The next snippet uses the repeat element to create the same result.

<repeat count="2">
    <repeat count="2" var="m">
        <object name="myObj[n][m]" type="sprite" sprite="mySprite"/>
    </repeat>
</repeat>
Last modified 5 years ago Last modified on Dec 10, 2014, 9:15:51 PM