Generating Elements at Runtime
You can create countless clones of other elements by including a Generators dictionary inside of your page. For example…
You can start or stop generating Elements at a frequency you specify by using these properties. Or you can set the Enabled property to YES and it will generate clones immediately. For the start and stop events, you identify the generator by the sub-dictionary name you provide, which will also match the Element in the scene to clone. The element to clone could be hidden initially. If so, the clone is identical in every way (including any Actions), but the hidden property is automatically set to false. Customize the generator with the following options…
- VaryX – when the clone is created it can vary it’s initial location on the x in relation to the original. The number you set here will be used as the random range for the initial x offset. So for example, if set 500, the x axis variation will be anywhere from 0 to 499
- VaryY – identical to VaryX but on the y axis.
- OffsetX – If you choose to vary where the clone appears on the x axis, you’ll typically want to set this to a negative value of half whatever the VaryX value is. When the clone is placed the calculation is basically…. x = x + (OffsetX + VaryX). So a negative value for the offset will make sure that the clone is placed randomly on both the left and right sides of the original.
- OffsetY – the same as OffsetX but on the y.
- Amount – a number value for the amount of clones to generate.
- Frequency – a number value for the time to delay before making another clone.
- Delay – the initial delay before any generating.
- RenameTo – You can rename the cloned element to have any name you want (if this property is excluded, the clone would have the same name as the Element it was cloned from). If you rename the clone, you can also include a new Elements dictionary (for example, DrawnItem in the screenshot below) so the clone gets all the properties of the original, then adds or replaces values based on the new name’s dictionary. So in the example screenshot, DrawElement creates clones based on itself, renames them to DrawnItem, and then the DrawnItem dictionary is used for any new properties.
If you stumbled onto this article, it is part of our documentation for the Story Tellers iOS Starter Kit 2. The kit enables you to make children’s book apps and games without writing any code! But it is Swift 2 based and compatible with iOS9 (or higher) and Xcode 7 (or higher), so kit buyers can even extend the functionality to fit their needs further. Some of what we cover in the kit documentation may apply to Xcode in general, so this article could be worth a read even if you aren’t a user. You can purchase Lifetime Updates the kit here, or subscribe Yearly to CartoonSmart and get the latest version, plus access to all of our other kits / tutorials.
We’ve also created an iBook to document the very latest properties in the kit, so be sure to download that as well.