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 - Setting Sprite Speed Synchronously

Multiplayer Network Games - Setting Sprite Speed Synchronously

Scheduled Pinned Locked Moved 2D Blocks
1 Posts 1 Posters 814 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 most multiplayer network games, each player would control a sprite to navigate the game world. For example, in a pong game, each player would move their own bouncing board up and down.

    Although we can set the position of a sprite synchronously for all players, a much more common way of moving sprites in a network game is to set its x and y speeds. This will ensure the sprite moves smoothly through a continuous path, which is critical for handling collisions between sprites.

     
     

    Setting X and Y Speeds Synchronously

     

    To set a sprite’s speed synchronously, we can use the following block:

    4ca4d0c9-9a9b-49f3-97f6-93fa209347f8-image.png

     
    This block takes 2 inputs, which are the speed in x and y directions. The x speed controls how many steps the sprite would move per second horizontally, and if it is negative, that means the sprite is moving to the left. Similarly, the Y speed controls how fast the sprite moves vertically, and negative Y speed means the sprite will be moving downwards.

    Note that the sprite will stop at the world boundary or when it collides with another sprite.

     
     

    Setting Direction and Speed Synchronously

     

    Sometimes it is simpler to think of a sprite as moving in a given direction at a certain speed. Therefore, another block is available to set the sprite’s speed this way:

    110e069c-733b-4443-9ea9-c82fc1c0c7c1-image.png

     
    The first input is the speed at which the sprite will be moving along the given direction. When it is negative, then the sprite is moving backwards along that direction.

    The second input is the moving direction in degrees. It follows the same convention as the “direction” property of sprites. For example, when it is 0, the direction points up (along the Y axis). When it is 90, it points to the right (along the X axis).

    Similar to the previous block, this sprite and all its replicates on other hosts will be updated to this new speed and direction at the same time.

     
     

    Demo

     

    In this demo project, we repeatedly set the ball’s speed randomly, and its copy on the second computer will move with the same speeds in sync.

    To run it:

    1. Create a new game as host using the “BlueBall” sprite:

    81b8fe03-234e-425c-a6a0-8dbd99d580b1-image.png

    1. Join the game from another account (in an incognito browser tab) using the “YellowBall” sprite:

    e94f147f-c284-4d82-9ce8-7543ea33241a-image.png

    1. In the “BlueBall” sprite, repeatedly set the ball’s speeds to random values synchronously:

    cc114486-ff0c-4048-8f90-039256896adc-image.png

    1. In the “YellowBall” sprite, run the same blocks

    2. As a result, both balls will be moving to random positions, and the 2 tabs will be in sync all the time:

    speed.gif

     
    Note that there might be some delay before the copy sprite changes its moving speed. That’s because the speed update message needs to travel to the game server first, and then it is sent to the guest player’s computer.

    Here is the project URL: https://play.cr eaticode.com/projects/6606029025dc753d5b67bc62

    1 Reply Last reply
    1

    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