CRCD.T1.M2.06: Objects and Sprites
Objects
In order to create more interesting and detailed images, we use objects. In Code.org, we use objects called sprites. Every sprite can be assigned an image to show, and sprites also keep track of multiple values about themselves, which will prove useful down the road when making animations.
Keeping track of many shapes and the different variables that control aspects of those shapes can get very complex. There will be lots of variables with different variable names. Instead computer scientists created something called an object which allows for one variable name to control both the shape and all its aspects.
In Game Lab we use a certain type of object called a sprite. A sprite is an on screen element with properties that control its look. Properties are the variables that are attached to a sprite. You can access them through dot notation.
Vocabulary
- Sprite - A character on the screen (an object) with properties that describe its location, movement, and look.
- Label - The name of the Sprite
- Property - A label for a characteristic of a sprite, such as its location and appearance
- Dot Notation - assigning a property to a Sprite by using this form: sprite label.sprite property (the dot is between the label and the property)
New Code
drawSprites(group) Links to an external site.
var sprite = createSprite(x, y, width, height) Links to an external site.
sprite.scale Links to an external site.
Sprite Properties
If you think of a sprite as a collection of values that represents an object in the real world, then the properties of a sprite are like its attributes. Some of the most common sprite properties include:
Dot Notation
Notice that all of the examples above follow a common pattern of sprite label . sprite property. We call this format dot notation. The first part will always be unique to the sprite that you want to modify, and the second part will always be one of the properties common to all sprites.
Reading and Writing Properties
Sprite properties, like variables, are values that sometimes we want to just read, and sometimes that we want to write to. When you drag out a sprite property it will take out a different form depending on whether you are reading its value, or writing it.
In the first image above the sprite.x
Links to an external site.
block is being dragged into a place where it is being read, so it keeps its normal form.
In the second image, the sprite.y
Links to an external site.
block is being dragged onto a new line where it will be written to, so it's changed into the form sprite.y = ___
.