Scratch für Java

Stage::broadcast(message)

Diese Methode sendet eine Nachricht an alle Figuren und die Bühne. Die Nachricht kann mit whenIReceive verarbeitet werden.

Beispiele

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!");
    }
  }
}
 

Auf GitHub anschauen

Syntax

Java

void broadcast(message)

Scratch

broadcast [message v]

Parameter

Name Datentyp Beschreibung
message String Die Nachricht des Broadcasts.

Rückgabe

void

broadcast(message)