Scratch for Java

Sprite::setPosition()

Sets position of the sprite.

Examples

import org.openpatch.scratch.Sprite;
import org.openpatch.scratch.Stage;
 
public class SpriteSetPosition {
  public SpriteSetPosition() {
    Stage myStage = new Stage(600, 240);
    Sprite mySprite = new Sprite("slime", "assets/slime.png");
    myStage.add(mySprite);
 
    while (myStage.getTimer().forMillis(3000)) {
      int x = myStage.pickRandom(-myStage.getWidth() / 2, myStage.getWidth() / 2);
      int y = myStage.pickRandom(-myStage.getHeight() / 2, myStage.getHeight() / 2);
 
      mySprite.setPosition(x, y);
      myStage.wait(200);
    }
    myStage.exit();
  }
 
  public static void main(String[] args) {
    new SpriteSetPosition();
  }
}
 

View on GitHub

Syntax

Java

.setPosition(x, y)
.setPosition(vector)

Scratch

set position to x () y ()

Parameters

Name Data Type Description
x float X coordinate
y float Y coordinate
vector Vector2 A vector representing the new position

Return

void

setPosition()