camera.y

Category:Game Lab

The y position of the camera.

The default y position is 0. All camera properties can be both accessed and updated.

The camera enables scrolling and zooming for scenes extending beyond the display area. A camera has a position, a zoom factor, and mouse coordinates relative to the view. The camera is automatically created and available for use.

Examples

Follow the Alien

Link the camera to the main sprite movement and scroll stationary sprites.

// Link the camera to the main sprite movement and scroll stationary sprites.
var sprite = createSprite(200,200);
sprite.setAnimation("alienBlue_walk_1");
var cactus = createSprite(100, 300);
var fence = createSprite(300, 300);
cactus.setAnimation("cactus_1");
fence.setAnimation("fence_wood_1");
camera.on();
function draw() {
  background("white");
  drawSprites();
  camera.x=sprite.x;
  camera.y=sprite.y;
  if (keyDown("right")) {
    sprite.x = sprite.x+5;
    sprite.mirrorX(1);
  }
  if (keyDown("left")) {
    sprite.x = sprite.x-5;
    sprite.mirrorX(-1);    
  }
  if(sprite.x-cactus.x>200) cactus.x=sprite.x+200;
  if(cactus.x-sprite.x>200) cactus.x=sprite.x-200;
  if(sprite.x-fence.x>200) fence.x=sprite.x+200;
  if(fence.x-sprite.x>200) fence.x=sprite.x-200;
}

Looking for Something

Explore a background by linking the camera to the mouse.

// Explore a background by linking the camera to the mouse.
var back = createSprite(200, 200);
back.setAnimation("stone_snow_1");
back.scale = 5;
camera.on();

function draw() {
  background("white");
  drawSprites();
  camera.x=World.mouseX;
  camera.y=World.mouseY;
}

Syntax

camera.y

Returns

The y coordinate of the center of the camera.

Tips

  • The display area is 400 pixels x 400 pixels and the upper left corner is (0.0).
  • To access or update the camera properties you use the dot notation (camera, followed by a dot, with the label of the property).
  • Any changes to the properties of the camera will not be seen until after drawSprites() is called.

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