Scratch for Java

Sprite::distanceToSprite()

Returns the distance to the other sprite.

Examples

import org.openpatch.scratch.Sprite;
import org.openpatch.scratch.Stage;
 
public class SpriteDistanceToSprite {
  public SpriteDistanceToSprite() {
    Stage myStage = new Stage(600, 240);
 
    Sprite gamma = new Sprite("gamma", "assets/gamma_purple_badge.png");
    gamma.setPosition(-120, 50);
    myStage.add(gamma);
    Sprite zeta = new Sprite("zeta", "assets/zeta_green_badge.png");
    zeta.setPosition(100, 50);
    myStage.add(zeta);
 
    while (myStage.getTimer().forMillis(3000)) {
      gamma.changeX(5);
      zeta.changeX(-5);
      myStage.display("Distance: " + gamma.distanceToSprite(zeta));
      myStage.wait(100);
    }
    myStage.exit();
  }
 
  public static void main(String[] args) {
    new SpriteDistanceToSprite();
  }
}
 

View on GitHub

Syntax

Java

.distanceToSprite(sprite)

Scratch

distance to [sprite v]?

Parameters

Name Data Type Description
sprite Sprite A reference to a sprite.

Return

float

distanceToSprite()