Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • CreatiCode
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

CreatiCode Scratch Forum

  1. CreatiCode Forum
  2. Knowledge Base
  3. 2D Blocks
  4. Multiplayer Network Games - Collision Handling

Multiplayer Network Games - Collision Handling

Scheduled Pinned Locked Moved 2D Blocks
1 Posts 1 Posters 750 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CreatiCodeI Offline
    CreatiCodeI Offline
    CreatiCode
    wrote on last edited by admin
    #1

    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:

    97db23c2-2a4d-41fd-abb1-47fa67117490-image.png

     

    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:

    a8baf19c-4d01-49b8-913b-ddc127a0ed0b-image.png

     
    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.
      &nbsp. 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:

    1. 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.

    46279db0-cc3c-43bc-b59a-abbabeb0039b-image.png
     
    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.

    5ddff12f-f7fb-4cfb-ab22-84d8dd90934d-image.png

     
    3. In the “BlueBall” sprite, repeatedly set the ball’s speeds to go left and then right:

    8dff5c8f-2200-427f-b297-d0cabfb0cbcf-image.png
     
    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:

    stop2.gif

     
     

    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”:

    b3cd51a1-4056-43b4-87ab-4d9aa3272d91-image.png

     
    Now if you run the 2 players again, the obstacle will be deleted upon the first hit:

    stopdel.gif

     
    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:

    cont.gif

     
    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:

    contdel.gif

     
     

    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:

    dd.gif

     
     

    Demo 6 - Self-Destruct and Collect

     

    Lastly, when we use the “Self-Destruct and Colelct” option, both sprites will be deleted when they touch:

    d2.gif

    1 Reply Last reply
    0

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode