Extensive Scrolling Engine

Материал из Поле цифровой дидактики
Файл:Extensive Scrolling World.png
A project utilizing the same scrolling engine for its large open-world.

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 Шаблон:Note

Preparation

Costume Arrangement

Only one sprite is necessary for the entire scrolling world, because each landscape panel is a clone of the landscape sprite arranged in a row, with a different costume on.

The first clone wears costume #1, the second clone, sitting to the right wears costume #2, and so on. The arrangement of costumes in the base sprite must match the order of the landscape move left to right, so that the scrollx variable changes its value, all these clones move together.

The variable scrollx should be created to hold the x position of the landscape.

Positioning the Landscape Panel Clones

To stop the scrolling when the player moves to the far left or far right part of the game, two variables need to be created.

scrollxbeginningforthisspriteonlyscrollxendingforthisspriteonly

These hold the x position of the beginning and end of the landscape.

Another variable needs to be created so that it can be used to determine if the landscape clone has left the visible area of the stage.

widthofviewforthisspriteonly

Now the initial values need to be set.

whenclickedsetwidthofviewto600600isrecommendedsetscrollxbeginningto-440findthevaluethatworksbestforyousetscrollxendingto-1850thishas5landscapetilesandneedstochangethistofityourgame.setscrollxtoscrollxbeginningstartfromthefarleft

Moving the Landscape Panel Clones

When arranging landscape panel clones, the player movement input will need to be captured and used to update the scrollx variable made earlier.

whenclickedforeverifkeyleftarrowpressed?andscrollx<scrollxbeginningthenchangescrollxby1endifkeyrightarrowpressed?andscrollx>scrollxendingthenchangescrollxby-1endend

Notice how moving left increases scrollx, this is so the landscape scrolls in the opposite direction to the player's movement.

List Usage

Файл:Clones Present List.png
In the list, "1"s represent the clones currently generated within the landscape, and "0"s represent non-present clones too far off the stage.

A list needs to be created to help organise the landscape panel clones.

clonesPresentforthisspriteonly

This list is used to control what panels are currently visible and active as opposed to the panels that are not visible and non-present.

During every frame of the game, the landscape sprite checks the scrollx variable and works out which three landscape panels need to be present. If the scrollx variable's value is, -480, the clones panels wear costumes 1, 2, and 3 because each costume is 480 pixels wide. Once scrollx reaches the value of -720, the landscape panel clones would wear costumes 2, 3, and 4. Which means the panel clone that was wearing costume on is no longer needed because its no longer visible on the stage. The landscape sprite uses the clonesPresent list to work out which panel clones are already present on the stage, which is why it doesn't delete the clones wearing costumes 2 and 3.

The list should be initialized properly with the following script.

whenclickeddeleteallofclonesPresentrepeat5"5"isthenumberofscrollingcostumesadd0toclonesPresentend

In the list, every "0" represents a panel clone that is not visible on the stage, and every "1" represents every panel clone that is visible. When a panel clone is created, it sets its value in the list to "1" to show that it is a visibily present. This way the lanscape sprite only creates one planel clone for each landscape costume needed.

When a clone moves too far off the stage, it sets its value to "0" again, because it is no longer visible.

Programming

The following script can be used to scroll each individual panel clone until it is no longer visible on the stage.

Once the panel clone is no longer visible, it is deleted to save memory.

Smooth Edge Scrolling

By scaling the sprite before and after changing its position, it needs to be made so it scrolls smoothly off the Scratch stage.

Шаблон:Note

whenIstartasaclonesetyto0valuecanbechangedtoverticalpositionshowrepeatuntilabsofwidthofcostume*costumenumber+scrollx>719repeatuntilnolongervisible(719pixels)setsizeto200%setxtowidthofcostume*costumenumber+scrollxsetsizeto100%endreplaceitemcostumenumberofclonesPresentwith0recordsdeletionofthisclonedeletethisclone

The panel clones' job is straightforward, it moves with the scrollx value until too far off the stage, then it gets deleted to save memory. The landscape sprite, which creates the clone, uses the following script to check and create new panel clones with the correct costume.

definescrollenginerunwithoutscreenrefreshsetbasePositiontoroundabsofscrollx/widthofview-widthofcostumefindwhichcostumeshouldbedisplayedinthemiddleofthescreensetito-1repeat4makesurethecostumessurroundingbasePositionarebeingdisplayedbyspritesifitembasePosition+iofclonesPresent=0thenswitchcostumetobaseposition+ireplaceitembaseposition+iofclonesPresentwith1clonenowmadecreatecloneofmyselfendchangeiby1end

Finally, the landscape sprite needs to always run the scrollengine.

whenclickedhidehidetheoriginalspriteforeverscrollengineend

See Also

External Links