Defining Behaviors

Category:Behaviors

When you define a behavior you give a name to a set of actions you want a sprite to perform over and over. When a sprite begins that behavior you are telling the computer to run that set of actions on the sprite over and over until the behavior stops.

Sprite lab provides some premade behaviors, but there is also an option to create a custom behavior. Selecting the “Create a Behavior” button opens a pop-up that takes a behavior name, description of what the behavior does, and the code blocks for the behavior. The “this sprite” block is given so that the behavior can apply to whichever sprite is used in the sprite begins or sprite stops block with it.

Clicking on the “edit” button in any behavior block will reopen the pop-up and allow the behavior to be edited or deleted.

To delete a behavior, click on the behavior’s block and select delete in the top right corner. This will remove the behavior from the toolbox. If the block still exists in the workspace or any other behavior or function definitions, it will no longer work.

To use behaviors once they are defined, review Adding and Removing Behaviors.

Tips

  • Make sure your behavior name is descriptive. It should be easy to understand what a behavior does without having to look at its code.
  • Look at the existing behaviors given in the toolbox for ideas of how to create new ones.
  • Existing behaviors in the toolbox can be edited to change the way sprites move, rotate, and change size or color.

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