Conversations

Материал из Поле цифровой дидактики

In Scratch, two or more sprites can talk to one another, having a dialogue conversation. There are several scripting methods associated with this, each unique to the other. They mainly utilize the say block or its timed counterpart for conversing.

Methods

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

Broadcast Method

The broadcast method is not always the most efficient way to make sprites communicate many messages, but short conversations can be easily made using broadcasts and no variables or lists. The following script would go into "Scratch Cat".

whenclickedsayHi!for2secsbroadcastGobo1

Then, inside the Gobo sprite:

whenIreceiveGobo1sayHowareyou?for2secsbroadcastScratchCat2

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

Broadcast and Wait method

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

whenclickedinthefirstspritesayHello!for2secsbroadcastGobo1andwaitsayHowareyoutoday?for2.5secsbroadcastGobo2andwaitandsoon

Then, inside of Gobo:

whenIreceiveGobo1sayHithere!for2secswhenIreceiveGobo2sayVeryfineindeed!Andyou?for3secs

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

whenclickedsettalkerto1setsitsotheconversationstartsfromthebeginningandfunctionsproperlysayHiGobofor2secschangetalkerby1signalsGobototalkwaituntiltalker=3waituntilGoboisdonerespondingsayIamgood,howaboutyou?for4secschangetalkerby1

And for the "Gobo" sprite:

whenclickedwaituntiltalker=2waituntilScratchCatgreetssayHiScratchCat.Howareyou?for3secschangetalkerby1signalsScratchCattotalkwaituntiltalker=4sayIamgreatalso!for3secs

The script can keep cycling continuously. The script can become very long from a long conversation, but it does not use multiple of any broadcasts. 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 project'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"

whenclickedsetitem#to1sothefirstmessageisthefirstiteminthelistofmessagessettalkertoScratchCattoknowthat"ScratchCat"speaksfirstforeversayitemitem#ofScratchCatmessagesfor3secssaysthecurrentitemfromthelistofmessagessettalkertoGobosignal"Gobo"tospeakwaituntiltalker=ScratchCatwaituntilitisitsturntospeak

And for "Gobo":

whenclickedforeverwaituntiltalker=Gobowaituntilitisitsturntotalksayitemitem#ofGobomessagesfor3secssaytheiteminthelistofmessageschangeitem#by1sothenextmessageinthelistsissaidbyeachspriteinsteadofthesameonesettalkertoScratchCatsignals"ScratchCat"tospeak