I am not embedding the online version here because the wand does not behave as it should online. So, to see it in action you will need to download and run the project on your computer.
There are 16 sprites and 33 scripts in this project. Only five of them are unique - the rest are copies - and only four truly matter. Let's see what we have…
The wandThe wand is easy to code. Easy to create too. I drew it on the Scratch Paint Editor using two lines of different shades. Its script simply instructs it to follow the mouse pointer at all times:
|Scripting wand to follow the mouse pointer|
The stageI put this script on the Stage, but it could really be on any sprite. All this script does is to broadcast a "click" message when it senses a mouse down event.
|Broadcast 'click' when mouse down event occurs|
The magical round thingsThese are the stuff that emerge out of the wand tip. They each have a copy of the same script and their own variables.
A "magical round thing" (yes, I have no better name for it. Ideas, anyone?) has four variables:
- outOfWand indicates whether the sprite has shot out of the wand and is moving on the stage. When outOfWand is 0, it means that the sprite is not on stage - you can think of this as the sprite being "inside the wand" and has not emerged yet. When outOfWand is 1, it means that the sprite has left the wand and is out on stage.
- v is the y-coordinate change for the sprite.
- dv controls the rate of fall for the sprite. You would easily understand what v and dv do if you have seen past tutorials and projects related to falling. (If you have not, you may want to take a look at some of these: Programming Falling Objects in Scratch, Three Ways to Program Falling Objects, Keep Afloat, and Run, Scratch Cat, Run!)
- u is the x-coordinate change for the sprite. We want the sprite to fall some distance to the left or right of the wand. u is the change in x that will help do this.
|Initialize outOfWand to zero when program starts|
|Setting values of variables when 'click' message is received|
The rest of the code in the repeat until (outOfWand = 0) block controls the behavior of the sprite after it has emerged:
|Behaviour having emerged from the wand|
It also checks whether it is touching the edge of the stage. If it is, then it hides itself and sets outOfWand to 0 to indicate that it has left the stage and - in a sense - has gone back in the wand. This means two things:
- The repeat until block terminates, since outOfWand is now 0.
- The next time the mouse is clicked, the if (outOfWand = 0) block will evaluate to true and it will continue the process of resetting its variables and emerge again.