Methods (part a) Alice In Ac4on, Ch 2 Slides Credit: - - PDF document

methods part a alice in ac4on ch 2
SMART_READER_LITE
LIVE PREVIEW

Methods (part a) Alice In Ac4on, Ch 2 Slides Credit: - - PDF document

CS101 Lecture 21 Methods (part a) Alice In Ac4on, Ch 2 Slides Credit: Joel Adams, Alice in Action With thanks to John Magee for his guidance about these materials Objec4ves Build


slide-1
SLIDE 1

1

Methods ¡(part ¡a) ¡ Alice ¡In ¡Ac4on, ¡Ch ¡2 ¡

¡ ¡

Slides Credit: Joel Adams, Alice in Action With thanks to John Magee for his guidance about these materials

CS101 Lecture 21

Objec4ves ¡

  • Build ¡world-­‑level ¡methods ¡to ¡help ¡organize ¡a ¡

story ¡into ¡scenes ¡and ¡shots ¡

  • Use ¡dummies ¡to ¡reposi4on ¡the ¡camera ¡for ¡

different ¡points ¡of ¡views ¡within ¡a ¡scene ¡ ¡

  • Understand ¡how ¡an ¡object’s ¡posi%on, ¡
  • rienta%on, ¡and ¡point ¡of ¡view ¡are ¡described, ¡

changed ¡and ¡determined ¡

  • Documen4ng ¡your ¡code ¡with ¡comments. ¡
  • Understand ¡Flow ¡of ¡Control ¡with ¡methods. ¡

2

slide-2
SLIDE 2

2

Methods ¡

  • Methods ¡ ¡

– behavior-­‑producing ¡messages ¡(from ¡the ¡sender’s ¡view) ¡ – behaviors/ac4ons ¡in ¡response ¡to ¡requests, ¡messages ¡ (from ¡the ¡recipient’s ¡view) ¡

– E.g. ¡in ¡world.my_first_method: ¡whiteRabbit.pointat(camera) ¡ ¡

  • Conven4on ¡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 ¡addi4onal ¡behaviors ¡(last ¡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 ¡solu4ons ¡of ¡ ¡smaller ¡problems ¡into ¡a ¡solu4on ¡for ¡ the ¡original, ¡big ¡problem ¡

  • Hiding ¡complex ¡details ¡with ¡abstrac4on. ¡

¡

Alice in Action with Java 4

slide-3
SLIDE 3

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 ¡loca4on ¡ ¡ – Shot: ¡part ¡of ¡a ¡scene, ¡normally ¡from ¡one ¡fixed ¡camera ¡view ¡

  • Use ¡mul4ple ¡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 ¡
  • Crea4ng ¡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

slide-4
SLIDE 4

4

Alice in Action with Java 7

Methods for Scenes (continued)

Alice in Action with Java 8

Methods for Scenes (continued)

slide-5
SLIDE 5

5

Methods ¡for ¡Scenes ¡(con4nued) ¡

  • 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)

slide-6
SLIDE 6

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

slide-7
SLIDE 7

7

Methods ¡for ¡Shots ¡(con4nued) ¡

  • Implemen4ng ¡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 ¡organiza4on ¡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)

slide-8
SLIDE 8

8

World ¡and ¡Object ¡Methods ¡

  • World ¡method: ¡affects ¡behavior ¡of ¡all ¡objects ¡in ¡

a ¡world ¡

  • Object ¡method: ¡defines ¡behavior ¡for ¡a ¡single ¡
  • bject ¡(that ¡may ¡have ¡mul4ple ¡parts) ¡

– examples: flapWings()for ¡dragon, ¡hop() ¡for ¡a ¡ rabbit… ¡

Alice in Action with Java 15

Program ¡Documenta4on ¡

  • 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

slide-9
SLIDE 9

9

Alice ¡Tip: ¡Using ¡Dummies ¡

  • Review ¡ ¡ ¡

– Scenes ¡comprise ¡shots ¡ – Shots ¡are ¡filmed ¡with ¡the ¡camera ¡in ¡a ¡given ¡ posi4on ¡ – Alice ¡places ¡a ¡camera ¡object ¡in ¡every ¡world ¡

  • Two ¡techniques ¡for ¡shi_ing ¡posi4on ¡of ¡camera ¡

– Use ¡set ¡of ¡mo4on-­‑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 ¡posi4on ¡
  • Descrip4on ¡of ¡a ¡scene ¡that ¡will ¡use ¡dummies ¡ ¡

– Wizard ¡intervenes ¡to ¡prevent ¡trolls ¡from ¡taking ¡a ¡castle ¡ – Camera ¡changes ¡posi4on ¡for ¡each ¡of ¡three ¡shots ¡ – Story ¡conforms ¡to ¡structure ¡in ¡Figure ¡2-­‑11 ¡(less ¡Shot ¡4) ¡

  • Seang ¡up ¡the ¡first ¡shot ¡of ¡Scene ¡2 ¡ ¡

– Add ¡castle, ¡wizard, ¡and ¡trolls ¡to ¡build ¡the ¡scene ¡ ¡ – Click ¡more controls ¡bubon ¡and ¡then ¡drop ¡a ¡dummy ¡ – Go ¡to ¡object ¡tree ¡and ¡rename ¡dummy ¡scene2Shot1

18

slide-10
SLIDE 10

10

19

Dummies (continued)

20

Dummies (continued)

slide-11
SLIDE 11

11

Dummies ¡(con4nued) ¡

  • Seang ¡up ¡the ¡second ¡shot ¡of ¡Scene ¡2 ¡

– Using ¡camera ¡controls, ¡zoom ¡in ¡on ¡the ¡wizard – Press ¡the ¡drop ¡dummy ¡at camera ¡bubon ¡ – Rename ¡the ¡second ¡dummy, ¡scene2Shot2

  • Seang ¡up ¡the ¡third ¡shot ¡of ¡Scene ¡3 ¡

– First ¡dummy ¡will ¡be ¡reused ¡for ¡this ¡shot ¡

  • A_er ¡dummies ¡are ¡inserted ¡they ¡will ¡be ¡

programmed ¡ ¡

21 Alice in Action with Java 22

Dummies (continued)

slide-12
SLIDE 12

12

Using ¡setPointOfView() ¡to ¡ Control ¡the ¡Camera ¡

  • obj.setPointOfView(obj2)

– Changes ¡the ¡posi4on ¡of ¡obj ¡to ¡obj2 – Example: ¡camera.setPointOfView(aDummy)

  • Adding ¡code ¡to ¡the ¡first ¡shot ¡of ¡Scene ¡2 ¡

– Drag ¡a ¡doInOrder statement ¡to ¡the ¡edi4ng ¡area ¡ – Click ¡on ¡camera ¡object ¡in ¡the ¡object ¡tree ¡ – Drag ¡setPointOfView()to ¡the ¡edi4ng ¡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)

slide-13
SLIDE 13

13

Alice in Action with Java 25

Using setPointOfView() to Control the Camera (continued)

Using ¡setPointOfView() ¡to ¡ Control ¡the ¡Camera ¡(con4nued) ¡

  • Adding ¡code ¡to ¡the ¡second ¡shot ¡of ¡Scene ¡2 ¡

– Set ¡the ¡opacity ¡of ¡the ¡wizard ¡to ¡0 ¡in ¡proper4es ¡ pane ¡ – Drag ¡wizard’s ¡opacity property ¡to ¡edi4ng ¡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 ¡abribute ¡to ¡true

26

slide-14
SLIDE 14

14

27

Using setPointOfView() to Control the Camera (continued)

28

Using setPointOfView() to Control the Camera (continued)

slide-15
SLIDE 15

15

Summary ¡

  • Divide ¡and ¡conquer ¡approach: ¡decomposing ¡a ¡user ¡

story ¡into ¡scenes ¡and ¡shots ¡ ¡

  • Define ¡methods ¡to ¡support ¡modular ¡design ¡and ¡

provide ¡advanced ¡opera4ons ¡

  • World ¡methods: ¡messages ¡sent ¡to ¡the ¡world
  • Comments: ¡remarks ¡that ¡explain ¡program ¡

statements ¡ ¡

  • Flow ¡of ¡Control: ¡How ¡methods ¡modify ¡sequen4al ¡
  • execu4on. ¡
  • Dummy: ¡invisible ¡marker ¡with ¡posi4on ¡and ¡
  • rienta4on ¡(a ¡point ¡of ¡view) ¡

29