Learn Scratch 3.0 – Two Ways to Create Sprite Card

When we play Scratch game, sometimes we need to choose a game weapon from a library. In this post, I will use one example to demonstrate this functionality.

In the following demo project Plant and Zombie, after the user clicks the Peashooter Card, he/she could drag Peashooter and put it to a random position on the stage. So how to realize the process of Click Peashooter Card -> Move the Peashooter with the mouse pointer -> Click the Mouse pointer to plant the Peashooter?

There are several ways to implement it and I will explain two of them.

Method 1

The project code could be accessed at: https://scratch.mit.edu/projects/439049793/

Peashooter Card sprite

In this method, when user presses the mouse down on Peashooter Card sprite, “when Green Flag clicked” code segment will call “create clone of Peashooter”, therefore, a new Peashooter clone is created.

Peashooter Sprite

In the Peashooter’s code area, there are two parallel code segments of “when I start as a clone”.

In the first code segment, the Peashooter clone will always follow the mouse pointer. When the mouse is pressed down within a specified zone, the program records the x and y position of mouse (mouse x, mouse y) and stores them into variables “Peashooter_Pos_x” and “Peashooter_Pos_y”.

Please note a variable called “Clone_settled”. Its initial value is 0. When the user clicks within the specified area on stage, its value changes to 1, which means that the Peashooter clone has been “planted”. At this moment, the code segment uses “stop this script” to exit the forever loop.

In the 2nd code segment of “when I start as a clone”, it is waiting till the variable “Clone_settled” changes to 1. Therefore, when the 1st segment of “when I start as a clone” stops running, the 2nd segment begins its operation.

In summary, the 1st segment is responsible for the clone’s behavior before it is “planted”, while the 2nd segment is responsible for the clone’s behavior after getting “planted”. They are able to run in a sequence via the coordination of variable “Clone_settled”.

You might wonder if it is necessary to use two separate code segments of “when I start as a clone”. Why not put all the code in one segment? Since the 1st segment uses a forever block to trace the movement of mouse pointer, the only way to exit the forever loop is to use “stop this script” block. Therefore, part of the function has to be put into a separate code segment.

Method 2

This method uses a sprite “Generate” to simulate the Peashooter sprite in the dragging process. You could access the code via this address: https://scratch.mit.edu/projects/439053716/

Peashooter Card Sprite

The code for Peashooter Card sprite looks very similar to that in Method 1, except that it broadcasts “Generate Peashooter” message instead of “create clone of Peashooter” directly.

Generate Sprite

Generate sprite has the same costume as that of Peashooter sprite. After receiving the message “Generate Peashooter”, it switches its costume to Peashooter (a completely equal costume of Peashooter) and moves with mouse pointer until mouse is clicked down within the specified zone.

The code will capture the x and y position of the mouse pointer (mouse x, mouse y) and then call “create clone of Peashooter”. Till this moment, the mission of Generate sprite finishes and it could hide itself and stop running.

Peashooter Sprite

The Peashooter sprite has one code segment of “when I start as a clone” in this method. Its function is equivalent to the 2nd code segment of “when I start as a clone” in Method 1. How could it beeome to only have one code segment? it is because Generate sprite shoulders the responsibility before Peashooter gets planted.


Although it looks simple to drag a sprite from Peashooter Card position and put its clone on the stage, I meet some weird issues when using Peashooter sprite to follow mouse pointer and then create Peashooter clones on the stage. The clone positions could not keep independent when program uses both sprite and its clone simutaneously.

Therefore, in both methods, Peashooter sprite keeps hidden and only its clones work. What is different is that in first method, Peashooter clone has tasks in two phases. In the first phase, the clone moves together with mouse pointer and then records the position where it is “planted”. In the second phase, the clone keeps static in that position and continuously monitors its interaction with zombie.

In the second method, the program uses a Generate sprite to move with the mouse pointer. After user clicks a position on the stage to “plant” Peashooter, Generate sprite finishes its task and Peashooter clone begins work.

Which method do you prefer? Programming is flexible and remember “All roads lead to Rome”. Anyway, 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.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.