Names and Sprite Objects
CreatiCode last edited by info-creaticode
In 2D projects, each sprite corresponds to one object (one image) on the stage. However, for 3D projects, most likely we would create more than one object, such as 3 avatars or 100 boxes. Therefore, to work with multiple objects, you need to specify a name when you create an object, so that you can refer to that object using its name later.
Specifying a Name
- Name: the name of this object. Note that there are many blocks that allow you to specify a name, not just this one block. All of them end with the “as ( )”. If you leave that input empty, this new object will be assigned a random and unique name, such as “ID234234239”, by the 3D engine.
When you specify a name, the newly added object will be given that name. You can actually verify that by looking at the “object” list below the 3D scene window, as the name of the new object will appear in that list: And if you leave the name field empty, a random name will appear in that object list.
Suppose you add 2 tiger avatars with different names, such as “tiger1” and “tiger2”, then both names will appear in the object list:
A special situation is when you specify a name that’s already used by another object in the scene. In this case, the 3D engine will remove that existing object before adding this new object, since each name can only be used by one object:
The Most Recently Added Object
Suppose there are 2 avatars in the scene, one tiger and one cow. When you run the command “move 50 steps”, which one of them would receive your command?
To avoid any argument between the tiger and the cow, we use a simple rule called “the most recently added object”. It means whichever object is added right before a command, that object will be receiving the command. For example, in this program, the cow is the most recently added object before the “move” command, so the cow will move forward.
The Sprite Object
We can also send commands to a specific object using its name. We just need to pick one of the objects as the “active object”, so all commands go to that object. Since each sprite can have one of such active objects, we call it the “sprite object”. In other words, a sprite object is the object that’s receiving commands from a sprite.
We can use the above block to select an object as the sprite object by its name. Note that the name has to be an existing name that you have already used to create some object. Now going back to our example, we can select the tiger as the sprite object using its name “Tony”, then the move command will be carried out by the tiger.
When a new object is added to the scene, it will be selected as the sprite object automatically. That’s how the “most recently added object” rule works. The name of the current sprite object will be selected in the object list under the 3D scene window. You can run the program step by step to examine how the sprite object name changes:
Removing an Object by Name
You can use this block to remove an object by supplying its name. If no name is given, the current sprite object will be removed.
For example, this program adds an avatar named “r”, then we can use this name to remove it.
Organize Your Code Using Sprites
In 2D Scratch, each object on the stage corresponds to one sprite. For 3D coding, although you can add more than one objects in each sprite’s code, it is generally a good idea to add a new sprite for each important object in your scene.
For example, if your project has a car and a robot, then it’s better to have a “car” sprite that creates the car, and you can put all the blocks for moving/changing that car in that sprite. Then you can have another “robot” sprite to add and control the robot.