Scratch for Java

Stage::whenIReceive(message)

This method is called when a broadcast is received.

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 whenIReceive(message)

Scratch

when I receive [message v]

Parameters

Name Data Type Description
message String The message of the broadcast.

Return

void

whenIReceive(message)