(→Y Scrolling: slight rewrite)
|Line 164:||Line 164:|
a variable called <scratchblocks>(scrolly)</scratchblocks>
to the number 480 to 360.
<scratchblocks>((scrolly) + ((360) * (0))</scratchblocks>
<scratchblocks>((scrolly) + ((360) * (0))</scratchblocks>
== See Also ==
== See Also ==
Revision as of 16:12, 1 August 2014
Scrolling in Scratch is the action of sliding sprites across the Stage. Scrolling in Scratch typically involves using a set of sprites and moving them across the stage while having no gaps between sprites. Common uses of scrolling in Scratch include platformers, scrolling text, maps in adventure games, and sometimes large pictures. Having an object(s) scroll across the stage is commonly seen in Scratch projects. In the Scratch community, knowing how to produce a scrolling effect is a highly valued ability; there is a common misconception that producing such an effect is very difficult. A project that uses scrolling is often referred to as a scroller.
How to Scroll
Scrolling is produced by creating scripts that move the sprite or set or sprites across the screen. Usually, Scratch users want to make an image greater than the stage bounds (480 by 360) scroll which proves difficult because Scratch restricts the size of sprites to the stage bounds. Many methods of producing scrolling involve creating a series of sprites each being the size of the stage bounds.
Scrolling Without Reusing Sprites
This is the most commonly used method of scrolling for beginners. It is sometimes referred to as the "Traditional Method" because it is usually the first method most users try as well as being featured in one of Scratch's sample projects. This uses one sprite per 480x360 tile of scrollable area. This method involves multiple sprites. To create a 2 way scrollable area a script like this is needed:
when flag clicked forever set x to ((scrollX) + ((480) * (0)))
A sprite with this script will appear in position number 0. 480 is the amount of space between sprites, the number it is multiplied by determines its position in a set of sprites, and the scrollX variable lets the user control the movement (in this case on the x-axis) for the set of sprites.
when flag clicked forever set x to ((scrollX) + ((480) * (1)))
Each time a sprite is added it must be manually given a new position number. Placing the following script would place a sprite in position 1 and make it appear beside the sprite with the above script.
Due to the way Scratch keeps sprites from going outside the stage area, a script like this may be desired to prevent overlapping sprites:
when flag clicked forever if <([abs v] of (x position)) > > then hide else show end
The number 480 can be replaced with something else depending on the width of screen that you want the sprites to scroll across.
To solve the issue of overlapping sprites some prefer to include a black border around their screen in which case this script is not necessary.
You will also need some way of changing the variable, so that the set of sprites can be moved by the user. A script like this one is fine.
when flag clicked forever if <key [right arrow v] pressed?> then change [scrollX v] by (-10) end if <key [left arrow v] pressed?> then change [scrollX v] by (10) end
This is a method for infinite scrolling. Just have
when flag clicked forever set x to ((-1) * ((scrollX) mod (480)))
Then, have a second sprite with another script which is
when flag clicked forever set x to ((-1) * (((scrollX) mod (480)) - (480)))
X and Y Scrolling
This is exactly the same as the "Traditional Method", except you can scroll upwards, as well as across. You will need more scripts just like the ones above except with a different variable, and you will have to replace the number 480 with the height of the area you want to scroll in (typically 360).
Two Player Scrolling
Two Player Scrolling is typically split screen scrolling. When using this method, one player's character is on the top of the screen and one on the bottom. The players use different controls, but their characters are in the same landscape with the same backgrounds. To begin you will need to make the background sprites. Afterwards duplicate them for the second player. You need two variables; a scroll variable for player 1 and a scroll variable for player two. You will need to duplicate player one and player two so that they can go be seen on both sides of the split screen.
After your costumes have been created, insert this script or a similar script in Player 1's script area:
when flag clicked set [Player 1 ScrollX v] to  forever if <key [right arrow v] pressed?> then change [Player 1 ScrollX v] by (-10) end if <key [left arrow v] pressed?> then change [Player 1 ScrollX v] by (10) end
However, this script will vary depending on the color of the stage and other elements.
Then duplicate the script seen before and insert it into Player 2's script area. You will need to change the variable used and the arrow keys pressed.
In the duplicate of player one's script area (the sprite that will appear in the other half of the split screen) copy this script:
when flag clicked forever go to x: ((Player 2 ScrollX) - (Player 1 ScrollX)) y: (([y position v] of [Player 1 v]) - (anything)) if <<((Player 2 ScrollX) - (Player 1 Scrollx)) > > or <((Player 2 ScrollX) - (Player 1 ScrollX)) < [-240]>> then hide else show end end
Put exactly the same script on the duplicate of player 2 except with (y position of player 2) rather than (y position of player 1).
For player 1's terrain use exactly the same method as the Traditional Method except use your variable for player 1's scrolling. The same goes for player 2 except with player 2's scrolling variable.
One Sprite One Script method
when gf clicked go to x: (-118) y: (-123) set [scrollx v] to  set [oldx v] to (x position) //These blocks keep the player's position set [oldy v] to (y position) forever clear switch to costume [costume2 v] // This is the first background costume go to x: ((scrollx) + ((480) * (0))) y: (0) stamp switch to costume [costume3 v] // This is the second background costume go to x: ((scrollx) + ((480) * (1))) y: (0) stamp // Continue this process until desired length is met switch to costume [costume1 v] go to x: (oldx) y: (oldy) if <key [right arrow v] pressed?> then// This gives the player controls. change [scrollx v] by (-5) end if <key [left arrow v] pressed?> then change [scrollx v] by (5) end
More information can be found in the One Sprite One Script Project article.
Top Down Scrolling
Top down scrolling is scrolling that you view from the top. Some examples by Nintendo are older versions of Mario Kart (or the bottom screen of the game on any DS device) and older versions of Zelda (or DS/Gameboy Zelda games).
An example Scratch project is Mario Kart by Lucario621.
Scrolling With Reusing Sprites
This method of scrolling reuses the scrolling object by stamping it at one position, switching to the next costume, and stamping again at another scrolling position. Repeat this process until you achieve the desired scrolling distance.
when gf clicked forever clear switch to costume [costume1 v] set x to ((scrollX) + ((480) * (0))) stamp switch to costume [costume2 v] set x to ((scrollX) + ((480) * (1))) stamp switch to costume [costume3 v] set x to ((scrollX) + ((480) * (2))) stamp
The screen can also scroll vertically in the Y axis.First, make a variable called
Setting the position of the sprite is similar to how it is done in horizontal scrolling, except the number 480 needs to be changed to 360, and
set y to () needs to be used.
set y to((scrolly) + ((360) * (0))
This can be combined with horizontal scrolling to scroll both horizontally and vertically.