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

Stay Cool!

Keep all the bad vibes from getting you down! Use the mouse to move around your emoji and stop the mean emojis from reaching the bottom

                            // Stay cool! Keep all the bad vibes from getting you down.
// Uses createGroup to make a bunch of bouncing baddies, 
// createEdgeSprites to make sure the baddies don't just fly off.

var cool = createSprite(200, 325);
cool.setAnimation("cool");
createEdgeSprites();
var group = createGroup();

for (var i = 0; i < 20; i++) {
  var sprite = createSprite(randomNumber(0, 400), randomNumber(0, 100), 10, 10);
  sprite.velocityY=randomNumber(5, 10);
  sprite.velocityX=randomNumber(-5, 5);
  sprite.bounciness = 1
  group.add(sprite);
}

group.setAnimationEach("mean");

function draw() {
  background("white");
  drawSprites();
  cool.x = World.mouseX;
  group.bounceOff(cool);
  group.bounceOff(topEdge);
  group.bounceOff(leftEdge);
  group.bounceOff(rightEdge);  
}

                        

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