Multiplayer Network Games - Collision Handling
-
Introduction
In traditional Scratch games, we can detect whether one sprite is touching another using the “touching” block in the sensor category, and trigger a message when that occurs. You can use the if block or when block like this:We can do something very similar in multiplayer network games using one block, which helps detect such events and broadcast a message.
Setup the Touch Event
To enable collision detection, we can ask the game server to check if this sprite is touching another sprite using the following block:
This is probably the most complex and powerful block in the multiplayer category since it needs to instruct the game server to do a few things. The inputs are:-
Sprite Name: This is the target sprite. We are asking the game server to continuously check if this sprite (where the above block is running) is touching the target sprite. Whenever that happens, we will ask the game server to take some actions and maybe also send a message. Note that if the target sprite has clones, they are all counted as potential targets.
 . This input also takes a special value of “Edge”, which represents the 4 borders of the stage. To avoid confusion, you should not name your own sprites as “Edge”. -
Clone ID Prefix: The second input is optional and you can leave it as blank. You can use it to specify the target sprite’s clone ID should start with some text. For example, if the sprite name is “Ball” and the prefix is “A”, then the game server will only look for target sprites that are clones of the “Ball” sprite, and only if their clone ID starts with “A”, such as “A1”, “A_20”, etc. This is useful since we often create clones of the same target sprite for different players or teams, and we may only want to trigger the touch event with a subset of those clones.
-
Action Type: The game server will only check for touch events when this sprite is moving, and this is the action to take when this sprite touches the target sprite in its movement.
- The first type is “Stop”, which means this sprite will simply stop. This is often used when the target sprite is an obstacle.
- The second type is “Stop and Collect”, which means this sprite will stop, and the target sprite will be removed from the game. This is often used when the target sprite is something that will be destroyed when this sprite hits it, such as a wall that can be broken when this sprite hits it. Note that this sprite will not be deleted. Only the target sprite will be deleted.
- The third type is “Continue”, which means this sprite will simply continue its movement. This is often used when the target sprite is a sensor, such as a special area (e.g. “no-gravity zone”) that triggers a different behavior for this sprite.
- The forth type is “Continue and Collect”, which means this sprite will continue its movement, and the target sprite will be deleted as well. This is often used when the target sprite is a collectable (like a reward item). It will not block this sprite’s movement, and it will be deleted because it can only be collected once.
- The fifth type is “Self-Destruct”, which means this sprite will be deleted itself, and the target sprite will not be affected. This can be useful when this sprite is a bullet and it hits a wall or the stage edge.
- The sixth type is “Self-Destruct and Collect”, which means both this sprite and the target sprite will be deleted. This can be useful for situations like a bullet hitting an obstacle that will be destroyed by the bullet.
- Message Type: When this sprite touches the target sprite (or any of its clones), we can ask the game server to broadcast a message to this sprite and the target sprite. For example, we can first add a new event block for “when I receive message with parameter”, add a new message called “touching message”, then choose “touching message” from the dropdown of this block. Note that the message will only be sent to the original sprites, and not the remote copies created by the game server.
- Message Parameter: We can also specify a parameter that will be attached to the message. When this sprite and the target sprite receive the message, they will also receive the parameter. The parameter will contain 2 parts, separated by “—”. The first part will be the ** clone ID" of the other sprite that is touching this sprite, and the second part is the parameter specified in this input. Note that the clone ID is “originalsprite” for the original sprites, and a random value for its clones unless you specify the clone ID when the clone is created. For example, suppose “Sprite A” is touching “Sprite B”, and this message parameter input is set to “hit”. Then the parameter received by both sprites will be “originalsprite—hit”, since both sprites are originals (not clones).
Demo 1 - Stopping at Obstacle
https://play.creaticode.com/projects/66142a83564fc287b0e8ebc5
In this demo project, we make the blue ball move left and right repeatedly, and it will stop at the obstacle and broadcast a message.
To run it:
- Create a new game as host using the “BlueBall” sprite, add the sprite to the game, and set up a collision event with the “Obstacle” sprite. When the collision occurs, the blue ball will be stopped, the game server will send out the “touching message”, and this sprite will handle that message by saying the parameter value.
2. Join the game from another account (in an incognito browser tab) using the “Obstacle” sprite and add the sprite as a rectangle. When it receives the “touching message”, print out the parameter as well.
3. In the “BlueBall” sprite, repeatedly set the ball’s speeds to go left and then right:
4. As a result, the blue ball will stop whenever it hits the obstacle, and both the blue ball and the obstacle will say the parameter values:
Demo 2 - Stop and Collect
You can change the project above this way: go to the “BlueBall” sprite, and modify the action type from “Stop” to “Stop and Collect”:
Now if you run the 2 players again, the obstacle will be deleted upon the first hit:
You might notice a small delay before the square obstacle was deleted, since we need to wait for the game server to determine the collision has occurred and the obstacle should be deleted, and then that decision has to be sent to the player’s computer.
Demo 3 - Continue Moving
We can also change the action type to “Continue”, so the blue ball will continue its movement when touching the obstacle:
Note that the message is received repeatedly while the blue ball and the obstacle are touching each other.
Demo 4 - Continue and Collect
Next, we can choose the “Continue and Collect” action type, so the obstacle will get deleted the first time the blue ball touches it:
Demo 5 - Self-Destruct
Now we can try to change the action to “Self-Destruct”, and the blue ball will disappear when it touches the obstacle:
Demo 6 - Self-Destruct and Collect
Lastly, when we use the “Self-Destruct and Colelct” option, both sprites will be deleted when they touch:
-