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. 知识库
  3. 二维积木
  4. 广播消息与接收消息(第2部分)

广播消息与接收消息(第2部分)

Scheduled Pinned Locked Moved 二维积木
1 Posts 1 Posters 742 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.
  • C Offline
    C Offline
    creaticodeteacher2-mail
    wrote on last edited by admin
    #1

     

    先修科目

    • 广播消息与接收消息

     

    介绍

    在另一篇文章,大家已经学习了如何广播消息与接收消息。

    有些使用消息的高级方式,我们将在本文章中讨论。

     
     

    带参数的消息

    当我们广播消息时,可选择附加一个参数。这样我们就可以重新使用同一种消息。

    例如这个项目,角色科迪问用户要转几次。如果用户回答“1”,科迪就广播消息“转1次”,另外2个角色就转1次。同理,如果用户回答“2”或“3”,科迪就广播消息“转2次”、“转3次”,另外2个角色同样会响应这个消息。

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b9a2626c-53a1-4042-9ef7-8e87f2635056.png" width="800" style="">

     

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/af0a1c89-d8e7-46a1-84d3-83133964b340.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    尽管这个程序能够运行,但有个问题。用户有可能输入任意数字,比如10、100。为了正确处理所有回答,我们要添加很多种消息:“转4次”、“转5次”……“转10次”……“转100次”……

    好在你不必这么做。你只需发送一条消息“转向”,然后再添加一个参数,就是转向次数:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/aff1887b-e1b2-4c57-bd5d-a2dd06da65e9.png" width="250" style="">

    当其他角色收到这条消息,它们可以设置一个变量,那个参数就存入这个变量。例如,如果科迪广播消息“转向”,并且带参数10,然后螃蟹收到消息“转向”,数值10就存入一个新变量。之后,这个变量就用来控制转几次。

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/77dbe00f-3d2e-4581-a6b4-fc5477f81b02.png" width="280" style="">

     
    同理,当甲虫收到消息“转向”,就把这个参数存入另一个变量:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/5c04e20a-3670-4c15-91ee-64a01d8b5b9c.png" width="280" style="">

     
    现在程序不仅简洁明了,还能响应任意的转向次数!

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4e7b5c97-be46-4923-a158-9264b5a2c66b.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">

     
    注意,利用这个积木,你可以把“等待”与“参数”结合起来。

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/7e01c86a-54b7-4197-82a8-10d749b803ef.png" width="270" style="">

     
     
     

    消息只发送至一个角色

    对于另一个更高级的方式,你可以把消息只发送至一个角色,而不是所有角色。如果你想让一些隐私消息仅对2个角色之间可见,就适合用这个积木。

    例如,在上方例子中,你可以把积木“广播”换成积木“发送”:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/bc1d148e-5c42-42e7-b826-c8fcc50ea5c9.png" width="350" style="">

    角色转动过快,为了看清角色转动过程,使角色每转1次等待0.3秒。
    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8881d907-96f1-4c57-b56e-df6efabd448b.png" width="250" style="">

    结果,只有螃蟹会响应这条消息:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/fa290022-0222-459a-a1bd-9207b47bf77d.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">

    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