createEdgeSprites()

Category:Sprites

Creates four sprites just off each edge of the screen ("topEdge", "bottomEdge", "leftEdge", "rightEdge") and a group called "edges" containing the four sprites.

createEdgeSprites is a helper command for games where you want to prevent sprites from going off the screen.

Examples

createEdgeSprites();
var sprite = createSprite(200, 200);
sprite.setVelocity(randomNumber(-5, 5), randomNumber(-5, 5));
function draw() {
  background("white");
  drawSprites();
  sprite.bounceOff(edges);
}

Walls Closing In

Make the walls slowly close in using the edge sprite names topEdge, bottomEdge, leftEdge and rightEdge.

// Make the walls slowly close in using the edge sprite names topEdge, bottomEdge, leftEdge and rightEdge.
createEdgeSprites();
var sprite = createSprite(200, 200);
sprite.setVelocity(randomNumber(-5, 5), randomNumber(-5, 5));
function draw() {
  background("white");
  drawSprites();
  topEdge.y=topEdge.y+1;
  bottomEdge.y=bottomEdge.y-1;
  leftEdge.x=leftEdge.x+1;
  rightEdge.x=rightEdge.x-1;
  sprite.bounceOff(edges);
}

Syntax

createEdgeSprites()

Returns

No return value. Creates four sprites just outside the edges of the screen.

Tips

  • Use the group "edges" to manage all four edge sprite interactions with other sprites.
  • Change the image displayed for an edge sprite from the default rectangle using the setAnimation command. All images must be first loaded and given a label using the Animation tab above the display window in Game Lab.
  • Edge sprites all have the same properties and you use the dot notation (combining the name of the edge sprite, followed by a dot, with the label of the property) to both access and update the property for that edge sprite.
  • If you attempt to set an edge sprite property to an incorrect value the property reverts to its default value.

Found a bug in the documentation? Let us know at documentation@code.org