image Tutorial on Inserting New Objects During the Game

You might have wandered if a character can insert another character on the canvas. The answer is yes! This can be easily done as the following:
There is a panda.
The player controls the panda.
When spacebar is pressed, the panda inserts a bamboo.
In this example, the player character inserts the new object. The bamboo will be inserted at the same location as the panda.
There are 3 puppies and 4 pandas.
When a puppy sees the panda, it inserts a bone.
The bone moves toward the panda.
Non-player characters (NPCs) can also insert objects. Unlike player characters, NPCs can insert an object approximately once every 0.5 second. Note that the character making the insertion must be the same character in the antecedent.

You may also insert an object relative to the inserting object's position. For example:

For example: When the rabbit is happy, it inserts[1, 25] a diamond.
Here, the rabbit will insert a diamond 25 pixels to the right of itself.

If you wish to insert an object currently absent, you'd need to add "There are 0 [obj]." in the setting as well. This indicates that your game will make use of the new object, but there are 0 instances of them at the start of the game. Then, you may also change the size and speed of these objects:

Finally, you may use the concept of insertion to replenish objects that have died or disappeared. Refer to the later tutorial on Infinite number of objects for more on this topic.


Try it yourself: Try design a game in which both the player character and the NPCs can insert objects.


Complete Sample Games for Inserting Objects

// Game #1. Shoot the hoops.


// Game #2. Eat everything but shoot the dinos.


// Game #3. Pin-ball.



Programming Concepts (optional material):
We have learned about object declaration and instantiation before. They are generally done at the beginning of a program. In this tutorial, additional objects are created and instantiated during the program by insertion. These new objects are dynamically allocated, as we do not know how many such objects there are at the beginning of the program.

In the example above, we do not know how many times a puppy will see a panda, a thus insert a bone, so we don't know how many bones will be inserted in the game. Therefore, the bones are dynamically allocated.


Next Page

Tutorials Home