From Test-Scratch-Wiki

In Scratch, two or more 角色s can talk to one another, having a dialogue conversation. There are several 程式ing methods associated with this, each unique to the other. They mainly utilize the 說出 [] block or its timed counterpart for conversing.


For this entire article, assume the two sprites conversing are "Scratch 喵咪" and "Gobo".

Broadcast Method

The 广播 method is not always the most efficient way to make sprites communicate many messages, but short conversations can be easily made using 广播 and no 变量s or 清单s. The following script would go into "Scratch Cat".

當 @greenflag 被點擊
說出 [Hi!] (2) 秒
廣播訊息 [Gobo 1 v]

Then, inside the Gobo sprite:

當收到訊息 [Gobo 1 v]
說出 [How are you?] (2) 秒
廣播訊息 [Scratch Cat 2 v]

The two sprites continuously broadcast back and forth to each other to say a message. However, when the conversation becomes long, multiple 广播 are needed and the scripts can become unorganized.

Broadcast and Wait method

This method is slightly more efficient, because you only need one script in the first sprite to talk.

當 @greenflag 被點擊 // in the first sprite
說出 [Hello!] (2) 秒
廣播訊息 [Gobo 1 v] 並等待
說出 [How are you today?] (2.5) 秒
廣播訊息 [Gobo 2 v] 並等待 // and so on

Then, inside of Gobo:

當收到訊息 [Gobo 1 v]
說出 [Hi there!] (2) 秒

當收到訊息 [Gobo 2 v]
說出 [Very fine indeed! And you?] (3) 秒

Variable Method

The variable method uses a variable to trigger the other sprite to talk. After a sprite finishes speaking, it changes a variable by "1", in which the other sprite recognizes and then begins speaking. The following script would go into "Scratch Cat".

當 @greenflag 被點擊
變數 [talker v] 設為 [1] // sets it so the conversation starts from the beginning and functions properly
說出 [Hi Gobo] (2) 秒
變數 [talker v] 改變 (1) // signals Gobo to talk
等待直到 <(talker) = [3]> // wait until Gobo is done responding
說出 [I am good, how about you?] (4) 秒
變數 [talker v] 改變 (1)

And for the "Gobo" sprite:

當 @greenflag 被點擊
等待直到 <(talker) = [2]> // wait until Scratch Cat greets
說出 [Hi Scratch Cat. How are you?] (3) 秒
變數 [talker v] 改變 (1) // signals Scratch Cat to talk
等待直到 <(talker) = [4]>
說出 [I am great also!] (3) 秒

The script can keep cycling continuously. The script can become very long from a long conversation, but it does not use multiple of any 广播. The variable method is often best for unordered conversations, in which there are multiple sprites not speaking to one another in a patterned fashion.

List Method

The list method uses the same amount of scripting no matter how long the conversation is. It is a great method to reduce large scripts and a 专案's file size. The list method uses two scripts, one of "Scratch Cat's" messages and one of "Gobo's". The first sprite says a message, triggers the second sprite to speak, and then the cycle continues, with a variable being changed after both have fully spoken, to signal the next message from the lists. The lists are used to simply store the text which each sprite will speak, and is ordered within. The following script goes into "Scratch Cat"

當 @greenflag 被點擊
變數 [item# v] 設為 [1] // so the first message is the first item in the list of messages
變數 [talker v] 設為 [Scratch Cat] // to know that "Scratch Cat" speaks first
  說出 (清單第 (item#) 項項目\( [Scratch Cat messages v] \) :: list) (3) 秒 // says the current item from the list of messages
  變數 [talker v] 設為 [Gobo] // signal "Gobo" to speak
  等待直到 <(talker) = [Scratch Cat]> // wait until it is its turn to speak

And for "Gobo":

當 @greenflag 被點擊
  等待直到 <(talker) = [Gobo]> // wait until it is its turn to talk
  說出 (清單第 (item#) 項項目\( [Gobo messages v] \) :: list) (3) 秒 // say the item in the list of messages
  變數 [item# v] 改變 (1) // so the next message in the lists is said by each sprite instead of the same one
  變數 [talker v] 設為 [Scratch Cat] // signals "Scratch Cat" to speak
Cookies help us deliver our services. By using our services, you agree to our use of cookies.