Broadcast(メッセージ)
Broadcast.send with params
(引数付きでメッセージを送る)
引数付きのメッセージ送受信
Scratch3 本家では、メッセージ送受信時に引数(可変パラメーター)を一緒に渡すことができません。
これは『TypeScratcher』独自仕様です。
typescript
import { Typescratcher as Ts } from "@tscratch3/typescratcher";
import type { Sprite } from "@tscratch3/typescratcher";
const cat = new Ts.Sprite( 'cat' );
/** メッセージID */
const messageId = 'BroadcastTest';
// 旗が押されたときの『cat』のスレッド
cat.Event.flagPresser().func = async function* ( this : Sprite ) {
await this.Control.wait(10); // 10秒待つ
const bubbleMessage = 'メッセージ受信した';
// メッセージを送る
this.Broadcast.send( messageId, bubbleMessage );
}
// メッセージ『messageId』を受信したときの『cat』のスレッド
// 渡される引数を『bubbleMessage』として受け取る
cat.Broadcast.receiver( messageId ).func = async function* ( this : Sprite,
bubbleMessage: string) {
// 渡された文字列を使って『言う』
this.Looks.bubble.say( bubbleMessage );
}Broadcast.sendAndWait with params
(引数付きでメッセージを送り、終わるまで待つ)
引数付きのメッセージ送受信
Scratch3 本家では、メッセージ送受信時に引数(可変パラメーター)を一緒に渡すことができません。
これは『TypeScratcher』独自仕様です。
typescript
import { Typescratcher as Ts } from "@tscratch3/typescratcher";
import type { Sprite } from "@tscratch3/typescratcher";
const cat = new Ts.Sprite( 'cat' );
/** メッセージID */
const messageId = 'BroadcastTest';
// 旗が押されたときの『cat』のスレッド
cat.Event.flagPresser().func = async function* ( this : Sprite ) {
await this.Control.wait(10); // 10秒待つ
const bubbleMessage = 'メッセージ受信した';
const bubbleSec = 2;
// メッセージを送って待つ
await this.Broadcast.sendAndWait( messageId, bubbleSec );// 引数2つ
// 『変数を使って言う』が終わった
console.log( '終わり' );
}
// メッセージ『messageId』を受信したときの『cat』のスレッド
// 渡される2つの引数を受け取ることができる
cat.Broadcast.receiver( messageId ).func = async function* ( this : Sprite,
bubbleMessage: string, bubbleSec: number) {
// 渡された文字列を使って『言う』
// 渡された秒数の間、言う
await this.Looks.bubble.sayForSecs( 'メッセージ受信した', bubbleSec );
}