Object-Level Methods Andinheritance
Total Page:16
File Type:pdf, Size:1020Kb
Object-Level methods and inheritance Class date: November 14, 2014 Objective: You will use new concepts (Object-Level & creating a new class) to write new methods that define new actions to be carried out by an object acting alone.
Log on. Create the scene (above). Today you will learn to write new methods that define new actions to be carried out by an object acting alone. Please take your time and follow the directions carefully. Use Google Chrome to view the following videos. Let PepperMint Patty show you some of her moves. https://www.youtube.com/watch? feature=player_embedded&v=yH_RlC6Ahe8 Bet you wish you could do this: https://www.youtube.com/watch?v=QG4702aAkcQ And here are your instructions on how to skate like a pro. https://www.youtube.com/watch? v=cC54A9c2dEE&feature=player_embedded 1. Consider the iceSkater scene you just created. We want the skater to perform typical figure- skating actions. Watch Peppermint Patty. What moves does a figure skater make? 2. Our skater is dressed in a skating costume and is wearing ice skates, but this does not mean she knows how to skate. However, all Alice objects “know” how to perform simple methods such as move, turn, and roll. We can use a combination of these simple methods to “teach” the ice skater how to perform a more complex action. We begin with a method to make the skater perform a skating motion. 3. What is our scenario? Is this a scenario? 4. Planning/designing/implementing/testing “skate” design a textural story board for an Object-Level method called skate. stepwise refinement: slide o slideLeft o slideRight 5. Planning/designing/implementing/testing “spin” design a textural story board for an Object-Level method called spin. o prepareToSpin o finishSpin howManySpins (parameter) 6. Creating a new class Writing and testing the methods took sometime and effort to achieve. It would be a shame to put all this work into one world and not be able to use it again in another animation program we might create later. We can save the iceSkater and her newly defined methods so we can use them in another world. To do this, the iceSkater must be saved out as a new 3D model (class). This is a two step process: 1. Rename the iceSkater. VERY IMPORTANT! We want Alice to save this new class with a different 3D filename than the original IceSkater class. To rename an object, right-click on the name of the object in the Object tree, select rename from the popup menu, and enter the new name in the box. 2. The second step is to save out as a new class: right click on cleverSkater in the Object tree and this time select save object. Navigate to the bolder/directory where you wish to save the new class and then click the Save button. The class is automatically named with the new name, beginning with a capital letter and a filename extension .a2c. Once a new class has been created, it can be used in a new world by selecting Import from the File menu. 7. Inheritance-benefits: Creating a new class based on a previously defined class is called inheritance. It allows you to write code once and reuse it in other programs. Inheritances allow you to share code with others in team projects. 8. Guidelines for writing Object-Level methods 1. Do create many different object-level methods. They are extremely useful and helpful. 2. Play a sound in an object-level method ONLY IF the sound has been imported for the object. 3. Do not call world-level methods from within an object-level method. 4. Do not use instructions for other objects from within an object-level method. We expect to save out the object as a new class and reuse it in a later world. We cannot depend on other objects being present in other worlds. Use an object-level method with an object parameter to get the result you intended Activity 1: cleverSkater.skateAround Add a penguin to your scene. Create an Object-Level method that will have cleverSkater skate around the penguin. Use an Object Parameter and replace the object with the parameter. cleverSkater.skateAround Activity 2: Enhanced cleverSkater. Create an even better cleverSkater than the one you have. In addition to the skateForward, spin and skateAround methods, create skateBackward and jump class-level methods. In skateBackward, the skater should perform similar actions to those in the skateForward method, but slide backward instead of forward. In a jump method, the skater should move forward, lift one leg, then move upward (in the air) and back down to land gracefully on the ice and lower her leg back to its starting position. Save out your enhanced skater as EnhancedCleverSkater. Test your newly defined class by starting a new world with a frozen lake. Add an enhancedCleverSkater to the world. Also add a penguin and a duck. (a) Call each of the methods you have written. (b) Then call the skateAround method-to make the skater skate around the penguin and then the duck. (This will require two calls to the skateAround method.) And if there is time, activity 3: Lock Combination Create a world with a comboLock (Objects folder). Create four class-level methods-leftOne, rightOne, leftRevolution, rightRevolution-that turn the dial 1 number left, 1 number right, 1 revolution left and 1 revolution right, respectively. Then create a class-level method named open that opens and another named close that closes the lock. Hints: One position on the dial is actually 1/40 of a revolution. (Use the endGently style to make the motion more realistic.) Rename comboLock as TurningComboLock and save it as a new class.