From Test-Scratch-Wiki
- This tutorial assumes you have basic knowledge of Scrolling (sprites).
An extensive scrolling engine is a project engine that maintains an extensively large scrolling world. Suppose a world has approximately 300 scrolling tiles for a large open-world game. Having all 300 clones present simultaneously could cause major lag due to an excessive amount of scripts and rendering. An extensive scrolling engine maintains low processing power by only generating the scrolling tiles that are nearby the current location in the project. When scrolling tiles fall too far off the stage, the engine automatically deletes them. This tutorial explains how to develop and maintain a very wide-scrolling world.
Note: | This tutorial specifically deals only with x scrolling, but the concepts can be used for y scrolling. |
Preparation
Costume Arrangement
Only one sprite is necessary for the entire scrolling world, because the sprite generates clones of itself which arrange properly to form the landscape. Assume the variable scroll x
is used to maintain the proper x position of the landscape. The first clone takes on the costume # of 1, the second clone, justified to the right of the first, takes on costume # 2, and so forth. The arrangement of costumes in the base sprite correlates to the arrangement of the landscape. As the scroll x
variable changes its value, all these clones move correspondingly.
List Usage
Before programming the engine, one list is needed; it can be named anything, though "clonesPresent" represents its purpose closely. The list is used to control what clones are currently "alive" and running as opposed to the clones that are "dead" and non-present.
During every frame of the project's running, the engine first checks the scroll x
variable and is able to detect the three nearest scrolling tiles required to be present. If the scroll x
variable's value is, for example, -480, clones resembling costumes 1, 2, and 3 would need to be generated, under the assumption that each costume is 480 pixels wide. Once scroll x
reaches the value of -720, the engine would generate costumes 2, 3, and 4 of the landscape. However, the engine can use the clonesPresent
list to detect which clones are already present, and it will not generate a duplicate of the same clone.
Assuming the base scrolling sprite has 50 scrolling costumes, the following script should be used within the project:
when gf clicked delete (all v) of [clonesPresent v] repeat (50) //"50" is the number of scrolling tiles add [0] to [clonesPresent v] end
Each clone within the scrolling world has a list item that corresponds to its costume number. The proper hierarchical format begins with the base scrolling sprite, which generates clones. Each clone has its costume set to a unique one, which corresponds with its list item. Item 1 of clonesPresent
relates to clone 1, meaning the clone taking on the costume # of 1.
In the list, every "0" represents a clone that has not been generated yet. At the same time, every "1" represents every clone that has been generated and is still running. When a clone is formed, it sets its value in the list to "1" to show that it is a present object. This way the engine knows not to generate multiple of the same clone. When a clone drifts too far off the stage, it sets its value to "0" again, until it may be generated once again.
Programming
Before focusing on the generation of the clones, the following script can be used to scroll each individual clone. Very similar scripts can be seen in the article Scrolling (sprites).
Note: | The following script assumes only horizontal scrolling is in usage. |
when I start as a clone set y to (0) //value can be changed to vertical position show repeat until <([abs v] of (((480) * (costume #)) + (scroll x))) > (719)> //repeat until too far off set x to (((480) * (costume #)) + (scroll x)) end replace item (costume #) of [clonesPresent v] with [0] //notifies it is now deleting delete this clone
Therefore, the clones' job is straightforward: set the proper x position until too far off, then delete. The actual sprite, which generates the clone, uses the following script:
define generate //run without screen refresh set [basePosition v] to ((round ([abs v] of ((scroll x) / (480)))) + (1)) set [i v] to (-1) repeat (3) if <(item ((basePosition) + (i)) of [clonesPresent v]) = [0]> switch costume to ((base position) + (i)) replace item ((base position) + (i)) of [clonesPresent v] with [1] //clone now made create clone of [myself v] end change [i v] by (1) end when gf clicked forever generate