CreatiCode last edited by info-creaticode
Very often, we need to make multiple objects move together. For example, when one object moves or rotates, we want another object to move, rotate or scale with it. This is called “parenting”, because it is very similar to how a parent takes a child across the street: the parent walks, and the child follows.
Block for Setting the Parent
You can set use this block to set any other object to be the “parent” of the current sprite object (the active sprite object).
Parent Name: the name of the object that will become the parent.
Sprite Name: the sprite the parent object belongs to. In other words, the sprite that creates the object that’ll be set as the parent. Note that this option allows you to make an object created by a different sprite to be the parent object of an object in the current sprite.
Update Position/Scale: “Yes” means that the child object will be moved and rescaled relative to the parent. “No” means the child object will keep its current position and size.
Note that each object can have many children, but can only have one parent. Suppose an object A already has a parent object B, and you set A’s parent to object C instead, then A will no longer be the child of B.
Demo: Moving Together
In this example program, we add a truck and a table on top of it. These 2 objects are named “truck” and “table”. When we move the truck, the table does not move with it.
To fix this, we need to run the “set parent” block after we add the “add object” block, so that the table will follow the movement of the truck:
Demo: Rotating Together
In this example program, we add a UFO and then an owl on top of it. These 2 objects are named “UFO” and “Owl”. When we rotate the UFO, the owl stays where it is.
To make them rotate together, we just need to set the UFO as the parent of the owl. Note that the owl is not rotating around its own body center, but around the parent’s body center. You can think of them as “body” (the UFO) and “arm” (the owl): when the body rotates, the arm will rotate around the body.
Demo: Update Position and Scale
In this example program, the parent object is at X of 200, and its scale is updated in the Y direction. The child object is at X of 0 and Z of 200. When we set its parent, we have selected to update the child object’s position and scale, so it is moved to X of 0 and Z of 200 relative to the parent object, and its scale is inherited from the parent as well.
Removing a Parent
For the child object, to end the parenting relationship, we can “unlink” from the parent object use this block:
Note that you need to make sure the child object is the active “sprite object” before using this block.
As shown, when we select the owl object and unlink it from its parent (the UFO), the owl will no longer rotate with the UFO:
Removing All Children
For the parent object, it can also end the parent-child relationship with all its children using the following block:
Note that you need to make sure the parent object is the active “sprite object” before using this block.
Sometimes we need to add a new object to serve as the parent of another object. Although we can add a tiny box that’s almost invisible, a better option is to add a “transformer” object. Such objects are “cheaper”, because they are not visible, so there is no need to draw them on the screen. They also carry no data about its shape or color.
To add a transformer, you can use this block and give it a name:
In this example, we have a sword object. When we make it rotate around the Y-axis, it will rotate around its bottom point. Suppose we want it to rotate around another point, such as its middle point (Z of 50). We can add a transformer named “p” at the middle point, and set it as the parent of the sword. Now if we rotate the transformer object, the sword will rotate around it as well.
The parent relationship can be “chained”. Suppose object A is the parent of object B, and object B is the parent of object C. Then object A is the grandparent of C. Object C’s movement will be influenced by object B, and both of them will be influenced by object A.
In this example, we add 3 spheres to represent the Sun, the Earth and the Moon. We set the Sun as the parent of the Earth, and the Earth is in turn the parent of the Moon. As we rotate the Sun, both the Earth and the Moon will rotate around the Sun. We can go further to make the Earth rotates around itself, then the Moon will rotate around the Earth.
Try it yourself: