From Test-Scratch-Wiki
This article is about how to make a scrolling platformer.
The process begins by creating a sprite. In this article it is called terrain0_0 for clarity.
當 @greenflag 被點擊 重複無限次 定位到 x: ((ScrollX) + ((480) * (0))) y: ((ScrollY) + ((360) * (0))) end
This makes it so that the terrain scrolls. The two zeros in this situation correspond to the zeros in the sprite's name. For example, the sprite to the right of the center terrain is called terrain1_0 and it has
((scrollX)+((480)*(1)))
in the x space and
((scrollY)+((360)*(0)))
in the y space.
This can be repeated for the other terrain sprites. In general the terrain sprite that is x screens to the right of the center sprite and y screens above the center sprite is called terrainx_y in this example and has the following script:
當 @greenflag 被點擊 重複無限次 定位到 x: ((ScrollX) + ((480) * (0))) y: ((ScrollY) + ((360) * (0))) end
It looks something like this when the sensor sprites are made to go to the player sprite. It can vary depending on the sprite shape.
It is the same code for every sensor sprite.
當 @greenflag 被點擊 效果 [幻影 v] 設為 (100) 重複無限次 定位到 [player v] 位置 如果 <碰到顏色 [#09FF5C] ?> 那麼 變數 [hitTest v] 設為 [1] 變數 [hitTest v] 設為 [0] end // terrain color may vary end
The code in the picture below is then put into the player sprite.
當 @greenflag 被點擊 變數 [scrollX v] 設為 [0] 變數 [scrollY v] 設為 [0] 變數 [yVelocity v] 設為 [0] 重複無限次 如果 <([hitTest v] \( [rightHitTest v] \)) = [1]> 那麼 變數 [scrollX v] 改變 (10) 如果 <[向右 v] 鍵被按下?> 那麼 變數 [scrollX v] 改變 (-10) 面朝 (90 v) 度 end end 如果 <([hitTest v] \( [LeftHitTest v] \)) = [1]> 那麼 變數 [scrollX v] 改變 (-10) 如果 <[向左 v] 鍵被按下?> 那麼 變數 [scrollX v] 改變 (10) 面朝 (-90 v) 度 end end 如果 <碰到顏色 [#09FF5C] ?> 那麼 變數 [yVelocity v] 設為 [1] 如果 <[向上 v] 鍵被按下?> 那麼 變數 [yVelocity v] 設為 [15] end end // terrain color may vary 如果 <([hitTest v] \( [topHitTest v] \)) = [1]> 那麼 變數 [yVelocity v] 設為 [-3] 變數 [scrollY v] 改變 (3) end 如果 <([hitTest v] \( [topHitTest v] \)) = [1]> 那麼 變數 [scrollY v] 改變 (-5) end 變數 [yVelocity v] 改變 (-1) 變數 [scrollY v] 改變 ((-1) * (yVelocity)) end
This results in a properly working 卷轴滚动.
If the characters are intended to display animations when they move, the code in this 专案 is added.
The final project looks something like this.