170 likes | 181 Views
Learn about methods in programming, including their types, advantages, and how to create and call your own methods. Explore a hide and seek animation example and discover how methods can help organize your code and save time.
E N D
What will we learn today? • What are Methods? • Types of Methods • Using Methods Example • Creating your Own Method • Calling a Method
Methods • A Method is a coordinated sequence of instructions that will be carried out when requested • Are used to organize several instructions • Allow a programmer to think about a collection of instructions as one instruction (e.g. method for walking and moving legs and arms) • Each method performs its own job, but all methods work together to create the over all animation
Advantages of Methods • Better organizing the instructions to make it easier to read the code • Reduces the amount of program code and saves us time • Once the method is defined, we can tell Alice to run it from one main method (e.g. World.MyFirst method) • Methods can be called from several different places in the program
Types of Methods • We can write two types of methods • World-Level Method: methods where objects are interacting with other objects (e.g. a falcon is hunting a bunny) • Object-Level Method: methods where objects are acting alone, not affecting or being affected by other objects (e.g. a bird is flying in the sky)
Hide and Seek Example • Consider an example where a boy is playing “hide and seek” in the garden with his cat and moving around the tree to look for the cat • We want to write this animation using methods • The first step is to think about the animation in terms of large task, we can write the storyboard like this
Hide and Seek Example • The Storyboard Do In Order Seek Cat- the boy walks towards the tree Move Around- the boy and his cat turn around the tree Get Tired- the boy and his cat set together under the tree
Hide and Seek Example • The next step is to break down each major task of our storyboard into simpler steps Seek Cat Do In Order boy moves forward cat sets under tree boys legs and arms move Move Around Do Together boy and cat turn right boy and cat move a step forward boy and cat move legs Get Tired Do In Order boy sets under the tree cat sets under tree
Making 3 Turns Around the Tree • We want the boy and the cat to make three complete turns around the tree • To make one turn, the Move Around method should be repeated 10-15 times (Hint: copy and paste the code 10-15 times) • To make 2 or 3 complete turns, repeat the previous action two or three times Repeat 2-3 times Repeat 10-15 times Move Around
Create your Own Method • Lets write world-level methods for the three tasks “move around, seek cat, and get tired” • In the object tree • Select the world object • Select “methods” tag in the details panel • Click the “create new method” button • Enter the name of the new method in the popup box • Click the “edit” button to the right of your method. Add the instructions for the method
Calling a Method • When we click on play button, Alice automatically executes world.my first method • So, when a user clicks play button the method starts and world.my first method is called • To call a method • Drag the method from where it is listed in the methods tab of the details panel into world.my first method
Let’s Work with Alice • Sara and her horse are practicing a jump • Create an initial scene with horse and rider facing a fence (building) • Write two world-level methods • Gallop, horse and rider gallop forward one step, horse’s legs movement should look realistic • Jump, horse and rider jump the fence. Horse should move up far enough to not hit the fence • Write instructions in world.my first method to call the gallop method as many times as needed