Scratch for Java

Sprite::whenBackdropSwitches()

This method is called when the backdrop of the stage switches. The method will receive the name of the new backdrop as a paramter.

Examples

import org.openpatch.scratch.Sprite;
import org.openpatch.scratch.Stage;

public class SpriteWhenBackdropSwitches {

  public SpriteWhenBackdropSwitches() {
    Stage myStage = new Stage(600, 240);
    myStage.addBackdrop("forest", "assets/background_forest.png");
    myStage.addBackdrop("sea", "assets/background_sea.png");
    myStage.addTimer("backdrop");
    myStage.add(new CustomSprite());

    while (myStage.getTimer().forMillis(3000)) {
      if (myStage.getTimer("backdrop").intervalMillis(1000)) {
        myStage.switchBackdrop("sea");
      } else {
        myStage.switchBackdrop("forest");
      }
    }
    myStage.exit();
  }

  public static void main(String[] args) {
    new SpriteWhenBackdropSwitches();
  }
}

class CustomSprite extends Sprite {

  public CustomSprite() {
    this.addCostume("zeta", "assets/zeta_green_badge.png");
    this.addCostume("gamma", "assets/gamma_purple_badge.png");
  }

  @Override
  public void whenBackdropSwitches(String name) {
    if (name.equals("forest")) {
      this.switchCostume("zeta");
    } else if (name.equals("sea")) {
      this.switchCostume("gamma");
    }
  }
}

View on GitHub

Syntax

Java

void whenBackdropSwitches(name)

Scratch

Parameters

NameData TypeDescription
nameStringName of the new backdrop.

Return

void