On YouTube, we could always find some excellent and free resources. Like the following one made by Griffpatch, who taught how to design a platformer game on Scratch. The full tutorial contains 10 episodes and I strongly recommend you to follow them step by step. If you keep patient, you could design an amazing scrolling platformer game at the end of the video. Since those video clips are pretty comprehensive, I will not repeat the same content.
In this post, I would like to emphasize some points which Griffpatch has mentioned but not emphasized in his videos. You could regard this as a supplementary material. So let’s start!
Difference between Tick and Broadcast “Tick”
In Part 1 of the video clips, you might notice that in the main loop of “when I receive Play Game”, there is a “Tick” block and “broadcast Tick and wait” block. What are their difference? Aren’t they a bit repetitive? Here, “Tick” block is used to control the movement of the Player sprite, while “broadcast Tick and wait” message is used to inform other sprites to update their position.
Difference Between Variable “sy” and Input Parameter “sy”
In Part 2 of the video clips, Griffpatch defines two functions “Change Player y by sy” and “Change Player x by sx”. Please note the input parameters “sx” and “sy” (color blue) are different from the variables “sx” and “sy” (color yellow), although the names are totally the same. “sx” and “sy” variables represent the movement speed in x and y direction, respectively. Input parameters “sx” and “sy” are just used to pass the input value to blocks. Personally, I would suggest the author to rename input parameters “sx” and “sy”, so that users new to Scratch programming would feel less confusing.
The similar issue exists for variables “x” and “y”. You might notice that in the demo project of the video clip, each sprite has its own “x” and “y” variables. So why are there so many “x” and “y” variables? Remember that in the video clip, the author creates Platforms sprite by “duplicate” Player sprite. When we conduct “Duplicate” action, those local variables “x” and “y” are copied to another sprite, too. Please note all of them are local variables (that means, “used by this sprite only”). Different “x” and “y” have different values depending on which sprite you have selected.
The only global variables are “SCROLL X” and “SCROLL Y”. They are like the anchor of the project, which every sprite refers for positioning themselves on the stage.
Purpose of “Run without Screen Refresh”
In Part 2 of the video clip, the author introduced a very important option of block, that is “Run without screen refresh”. In most cases, this option is unchecked by default. However, in scrolling platformer game, “Tick” block contains a lots of minor position adjustment for the Player sprite. If this option is unchecked, you will notice that the Player is always adjusting its position when it touches ground or wall, making the whole game look weird.
The following is a quote from discussion forum on “Run without screen refresh”, just for your reference.
Normally, after each iteration of a control structure (like <forever> or <repeat ()>) Scratch stops executing the current script and goes on to the next one (this is called yielding). When it has finished executing one iteration of the rest of the scripts, it refreshes the screen (i.e. redraws the sprites in their new positions with their new attributes). It then repeats this process until it finishes executing the control structure.
“Run without screen refresh” causes a custom block to run all at once and not refresh the screen until it has finished executing. This is useful if you want to prevent the flickering that often results when, e.g., you loop through a list and draw each element.https://scratch.mit.edu/discuss/topic/511/?page=1
Why Are There So Many “Position” Blocks?
In Part 3 of the video clip, you might notice the author uses “Position” block many times, as shown in the below “Change Player x by sx” block. Is it necessary? Please keep in mind that “Position” block contains only one line. It makes Player sprite “go to x: x – SCROLL X, y: y – SCROLL Y”. Without calling “Position” block, the Player does not update its position, so the program could not check if it really touches Platforms or other sprites. That is why “Position” block should always be called before the program checks “touching” status.
The Position of Platforms Sprite
In Part 3 of the video clips, each Platforms sprite clone offsets 360 units from the previous one. Please note that offset amount could be flexible. In the episode 8 of the video clips, you will see that the new Platforms clone is created at a position by tweaking the code several times.
Offscreen Sprite Display
Part 4 of the video clips solves the issue of offscreen sprite display. The first time I saw the code of “x = x position” and “y = y position”, I feel bewildered, too. Therefore, I would like to explain this tricky code here.
Firstly, note that “x” and “y” here are input parameters of Position block, not the variables “x” and “y”. Input parameter “x” represents “x – SCROLL X”, and input parameter “y” represents “y – SCROLL Y”. Any sprite in the scrolling platformer should go to the position of “x – SCROLL X” and “y – Scroll y”.
Meanwhile, in the Motion tab, we could find another two variables “x position” and “y position”. They represent the real position of the sprite/clone.
For the sprite/clone crossing the edge, if its real position “x position” and “y position” is not equal to the expected position “x – SCROLL X” and “y – SCROLL Y”, we judge that it is stuck at the edge of the stage, so the program will hide it.
Set Collectable Sprite’s position and Motion
In the part 5 of video clips, Griffpatch introduced the use of m key. Its purpose is to get the map position of the mouse. In this way, we could add sprite to mouse position by simply adding SCROLL X and SCROLL Y back to “mouse x” and “mouse y”.
In Part 5, if you would like to make the Collectable clones sway within a range, you could use “sin” or “cos” function to add an offset value before calling “position” block. Please note that I rename variable “y” to “Collectable_y” here to avoid confusion.
The above content summarized the key points mentioned in the tutorial videos Part 1 to Part 5. Hope it could help you understand scrolling platformer game better. Keep tuned to our key point summary of video Part 6 to 10. Enjoy the coding and have fun!
Note: All the analysis articles are copyright products of http://www.thecodingfun.com. Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.