From Test-Scratch-Wiki

This tutorial assumes you have basic knowledge of 卷轴滚动.
A project utilizing the same scrolling engine for its large open-world.

An extensive scrolling engine is a 专案 engine that maintains an extensively large 卷轴滚动 world. Suppose a world has approximately 300 scrolling tiles for a large open-world game. Having all 300 分身 present simultaneously could cause major lag due to an excessive amount of 程式s 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 舞台, the engine automatically deletes them. This tutorial explains how to develop and maintain a very wide-scrolling world.

Note Note: This tutorial specifically deals only with x scrolling, but the concepts can be used for y scrolling.

Preparation

Costume Arrangement

Only one 角色 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座標 of the landscape. The first clone takes on the 造型编号(值) of 1, the second clone, justified to the right of the first, takes on 造型编号 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

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.

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 像素 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:

當 @greenflag 被點擊
刪除第 (全部 v) 項 \( [clonesPresent v] \)
重複 (50) 次 
  新增項目 [0] \( [clonesPresent v] \)
end // "50" is the number of scrolling tiles

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 造型编号 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 Note: The following script assumes only horizontal scrolling is in usage.
當分身產生
y 設為 (0) // value can be changed to vertical position
顯示
重複直到 <([abs v] 數值 (((480) * (造型编号)) + (scroll x))) > (719)> 
  x 設為 (((480) * (造型编号)) + (scroll x))
end // repeat until too far off
替換第 (造型編號) 項於 [clonesPresent v] 成 [0] // notifies it is now deleting
分身刪除

Therefore, the clones' job is straightforward: set the proper x座標 until too far off, then delete. The actual sprite, which generates the clone, uses the following script:

定義 generate //run without screen refresh
變數 [basePosition v] 設為 ((四捨五入數值 ([abs v] 數值 ((scroll x) / (480)))))) + (1))
變數 [i v] 設為 (-1)
重複 (3) 次 
  如果 <(清單第 ((basePosition) + (i)) 項項目\( [clonesPresent v] \) :: list) = [0]> 那麼 
    造型換成 ((base position) + (i))
    替換第 ((base position) + (i)) 項於 [clonesPresent v] 成 [1] // clone now made
    分身 [自己 v] 建立
  end
  變數 [i v] 改變 (1)
end

當 @greenflag 被點擊
重複無限次 
  generate::custom
end

参见

Cookies help us deliver our services. By using our services, you agree to our use of cookies.