I guess that you already know what sprites in Scratch are, but have you been thinking whether it is possible to create a new sprite from the code blocks level? The answer is YES thanks to concept called clones. In the programming language Scratch, a clone is a copy of a sprite, which is an object that can be animated and interacted with. When you create a clone of a sprite, you essentially make a duplicate that can behave independently from the original.
To understand it better let's imagine we have a sprite called "Particle" in Scratch, and we want to create a particle effect. First, we'll create the code for the original particle. We can make it move randomly around the screen, change color, or do any other desired effect. The end result should looks like that:
Let's start with creating variables for our particles. We need variable to store vertical and horizontal speed but also ghost effect (transparency) of the pieces. What is important here, when creating variable For this sprites only must be selected. As a result each clone will have own instance of the variable.
In the end of this step our
Variables section should looks as follows:
Now let's add a particle sprite. You can draw any shape you want. I have created a star-like piece that will help me achieve firework effect in the very end.
Please note that sprite has two costumes. One, default which is a piece for particle effect but also blank.
As a next step we are going to create a new block called CreateParticles that accepts argument being a number. Before clicking OK make sure that Run without screen refresh option is ticked.
With all we did as a prerequisites we can scratch some code. Lets start with definition of our block. The final resalt should be similar to one below:
As you can see the only thing we do here is creating number of clones that has been passed in clones variable. In this case we are going to clone ourselves. Next let's define behavior that will react every time mouse is clicked on the stage.
First thing wneh we start our application is to hide costume to blank. We could achieve similar effect but doing it this way will let us extend out implementation in next lessons. Main action within this block sequence is to call our block, CreateParticles, every time mouse is clicked. Now lets move to the last piece of code which defines behavior when the sprite is created as a clone. To do this we will use when I start as a clone block. Result should be similar to one on the listing below:

Firstly, we are going with our clone to the coordinates mouse was clicked on and make out sprite visible by changing costume to Default. Next we will set some random values for the particle. Please feel free to make experiments with all of those. Then until the particle will not touch the edge of the screen we are going to manipulate with parameters so that speed and transparency is recalculated with every execution of main loop of this block sequence. Of course you can try to manipulate with the parameters within the loop to see what impact on the stage they do have. The final result should looks like that:
As always, you can finc complete solution here. I hope you did like the final effect. Stay tuned since we are going to play with particles more in near future. Stay scratching!
Comments
Post a Comment