methods (part 1) alice in action, ch 2jmagee/cs120/slides/cs120.lect02.alice... · methods (part 1)...
TRANSCRIPT
![Page 1: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/1.jpg)
1
Methods (part 1) Alice In Action, Ch 2
Slides Credit: Joel Adams, Alice in Action
CS120 Lecture 02
31 August 2012
Objectives
• Build world-level methods to help organize a story into scenes and shots
• Use dummies to reposition the camera for different points of views within a scene
• Understand how an object’s position, orientation, and point of view are described, changed and determined
• Documenting your code with comments.
• Understand Flow of Control with methods.
2
![Page 2: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/2.jpg)
2
Methods • Methods
– behavior-producing messages (from the sender’s view) – behaviors/actions in response to requests, messages
(from the recipient’s view) – E.g. in world.my_first_method: whiteRabbit.pointat(camera)
• Convention for naming methods – Name should be a verb or verb phrase – Name should describe what the method does
• A method is a way to name a block of code.
3
Methods • Objects have predefined methods for common tasks • Methods may also be created by Alice developers
– Two main reasons for building your own methods • To provide an object with additional behaviors (Next week) • To organize your story and program into more manageable
pieces (Today)
• Divide and conquer methodology – Break a big problem into smaller problems – Solve each of the smaller problems – Combine the solutions of smaller problems into a
solution for the original, big problem
• Hiding complex details with abstraction.
Alice in Action with Java 4
![Page 3: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/3.jpg)
3
World Methods for Scenes and Shots
• User stories can be divided into scenes and shots – Scene: segment of a story, usually set in one location – Shot: part of a scene, normally from one fixed camera view
• Use multiple scenes and shots to create a program that
reflects the user story and has a modular design •
5 Two shots of one scene
Methods for Scenes
• Example: develop a user story with three scenes
• Creating the first new method – Select the world object
– Click the create new method in the details area
– Enter playScene1 in the New Method dialog box
• Check new method by sending say() to ground
– First test fails because my_first_method() is empty
6
![Page 4: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/4.jpg)
4
Alice in Action with Java 7
Methods for Scenes (continued)
Alice in Action with Java 8
Methods for Scenes (continued)
![Page 5: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/5.jpg)
5
Methods for Scenes (continued)
• How to fix the first bug – Click on the tab for my_first_method – Drag a doInOrder control to the top of the pane – Click on world in the object tree – Drag playScene1() into the doInOrder
statement
• Extend technique used to build playScene1() – Add two methods: playScene2(), playScene3()
– New method sends a say() message to the ground – New Methods are called in my_first_method()
Alice in Action with Java 9
Alice in Action with Java 10
Methods for Scenes (continued)
![Page 6: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/6.jpg)
6
11
Methods for Scenes (continued)
Methods for Shots
• Example of a scheme using scenes and shots – Level 1: my_first_method()
– Level 2: three methods for three scenes
– Level 3: four methods for four shots in Scene 2
12
![Page 7: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/7.jpg)
7
Methods for Shots (continued)
• Implementing the scheme – Test each shot in Scene 2 using a say() method
– Call the four shot methods from playScene2()
– Call three scene methods from my_first_method()
• Structure diagram reflects organization of user story
• Scene and shot messages are stored in the world
Alice in Action with Java 13
Alice in Action with Java 14
Methods for Shots (continued)
![Page 8: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/8.jpg)
8
World and Object Methods
• World method: affects behavior of all objects in a world
• Object method: defines behavior for a single object (that may have multiple parts)
– examples: flapWings()for dragon, hop() for a rabbit…
Alice in Action with Java 15
Program Documentation
• Standalone readme, manual…
• Comments: explanatory remark ignored by Alice – an integral part of code
– Used to describe what code does at various levels • the overall program, individual methods, blocks of
statements….
– Useful for collaborators and developers themselves
– Important part of programming • Also a component evaluated for your program grades
Alice in Action with Java 16
![Page 9: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/9.jpg)
9
Alice Tip: Using Dummies
• Review – Scenes comprise shots
– Shots are filmed with the camera in a given position
– Alice places a camera object in every world
• Two techniques for shifting position of camera – Use set of motion-related messages, such as move()
– Use an invisible marker called a dummy
17
Dummies
• Dummy: invisible marker with a point of view
• Dummies are used to change a camera’s position
• Description of a scene that will use dummies – Wizard intervenes to prevent trolls from taking a castle
– Camera changes position for each of three shots
– Story conforms to structure in Figure 2-11 (less Shot 4)
• Setting up the first shot of Scene 2 – Add castle, wizard, and trolls to build the scene
– Click more controls button and then drop a dummy
– Go to object tree and rename dummy scene2Shot1
18
![Page 10: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/10.jpg)
10
19
Dummies (continued)
20
Dummies (continued)
![Page 11: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/11.jpg)
11
Dummies (continued)
• Setting up the second shot of Scene 2
– Using camera controls, zoom in on the wizard
– Press the drop dummy at camera button
– Rename the second dummy, scene2Shot2
• Setting up the third shot of Scene 3
– First dummy will be reused for this shot
• After dummies are inserted they will be programmed
21
Alice in Action with Java 22
Dummies (continued)
![Page 12: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/12.jpg)
12
Using setPointOfView() to Control the Camera
• obj.setPointOfView(obj2)
– Changes the position of obj to obj2
– Example: camera.setPointOfView(aDummy)
• Adding code to the first shot of Scene 2 – Drag a doInOrder statement to the editing area
– Click on camera object in the object tree
– Drag setPointOfView()to the editing area • Select scene2Shot1 dummy as target and 0 duration
– Add say() statements for each of the trolls
– Add a comment to explain the purpose of the method
Alice in Action with Java 23
Alice in Action with Java 24
Using setPointOfView() to Control
the Camera (continued)
![Page 13: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/13.jpg)
13
Alice in Action with Java 25
Using setPointOfView() to Control
the Camera (continued)
Using setPointOfView() to Control the Camera (continued)
• Adding code to the second shot of Scene 2 – Set the opacity of the wizard to 0 in properties
pane – Drag wizard’s opacity property to editing area
• Set the opacity to 1 in the set() method
– Set the camera’s point of view to scene2Shot2 – Add a say() statement for the wizard
• Adding code to the third shot of Scene 2 – Reset camera’s point of view to scene2Shot1 – Point the three trolls at the wizard
• Set message’s onlyAffectYaw attribute to true
26
![Page 14: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/14.jpg)
14
27
Using setPointOfView() to Control
the Camera (continued)
28
Using setPointOfView() to Control
the Camera (continued)
![Page 15: Methods (part 1) Alice In Action, Ch 2jmagee/cs120/slides/cs120.Lect02.Alice... · Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture](https://reader033.vdocument.in/reader033/viewer/2022060207/5f03c9e97e708231d40ac6a1/html5/thumbnails/15.jpg)
15
Summary • Divide and conquer approach: decomposing a user
story into scenes and shots • Define methods to support modular design and
provide advanced operations • World methods: messages sent to the world • Comments: remarks that explain program
statements • Flow of Control: How methods modify sequential
execution. • Dummy: invisible marker with position and
orientation (a point of view)
29
Student To-Do
• No lab on Monday (Holiday)
• Hopefully we’ll do a lab on Tuesday
• First homework will go out on Tuesday.
– For now, practice on your own with Alice
• Readings:
– Ch 1, Ch 2
Alice in Action with Java 30