Scratch for Java

Stage::broadcast(message)

This method sends a broadcast to all sprites and the stage. The broadcast can be received by using the whenIReceived method.

Examples

import org.openpatch.scratch.*;
 
public class SpriteBroadcast {
 
  public SpriteBroadcast() {
    Stage myStage = new ReceiveStage();
    myStage.add(new BroadcastSprite());
    myStage.add(new ReceiveSprite());
    while (myStage.getTimer().forMillis(3000)) {}
    myStage.exit();
  }
 
  public static void main(String[] args) {
    new SpriteBroadcast();
  }
}
 
class ReceiveStage extends Stage {
  public ReceiveStage() {
    super(600, 240);
  }
 
  public void whenIReceive(String message) {
    if (message.equals("change-custome")) {
      this.changeColor(50);
    }
  }
}
 
class BroadcastSprite extends Sprite {
 
  public BroadcastSprite() {
    this.addCostume("zeta", "assets/zeta_green_badge.png");
    this.addCostume("gamma", "assets/gamma_purple_badge.png");
  }
 
  public void whenClicked() {
    this.broadcast("change-custome");
    this.nextCostume();
  }
}
 
class ReceiveSprite extends Sprite {
 
  public ReceiveSprite() {
    this.addCostume("zeta", "assets/zeta_green_badge.png");
    this.addCostume("gamma", "assets/gamma_purple_badge.png");
    this.changeX(50);
  }
 
  @Override
  public void whenIReceive(String message) {
    if ("change-custome".equals(message)) {
      this.nextCostume();
      this.say("Got it!");
    }
  }
}
 

View on GitHub

Syntax

Java

void broadcast(message)

Scratch

broadcast [message v]

Parameters

Name Data Type Description
message String The message to broadcast.

Return

void

broadcast(message)