creating a fancier fox and inheritance in alice 3 tutorial · creating a fancier fox and...

42
Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017 Adapted from Dr. Rodger’s Alice 2 tutorial, “Creating a Fancier Chicken to use in several worlds – Inheritance”

Upload: others

Post on 11-May-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

CreatingaFancierFoxandInheritanceinAlice3

JonathonKuounderthedirectionofProfessorSusanRodgerJune2017

AdaptedfromDr.Rodger’sAlice2tutorial,“CreatingaFancierChickentouseinseveralworlds– Inheritance”

Page 2: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Overview

•Wewilladdprocedures,functions,andpropertiestoafoxandsaveitouttobeusedinotherworlds.• Thisiscalled“inheritance.”Themodifiedfoxhasnewthingsitcando,plusitinheritseverythingastandardfoxcando.•WewillexplorehowAlice3usesclassestoimplementinheritanceinanotherway.

Page 3: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Start– AddaFox• Selectanybackground(Ipickedsnow).• Addafoxtoyourworld(intheQuadrupedfolder).• Choosethedefaultfox.Click“editcode”.

Page 4: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

AddaFoxProperty• Supposeourfoxcanonlyjump1meter,sowewillcodethatinaproperty.•AddaFoxProperty

Page 5: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

JumpHeightProperty• Nameit“jumpHeight”• Setthevaluetypetodecimalnumber• Settheinitialvalueto1.0andclickOK

Page 6: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

AddaFoxFunction•WewantafunctiontocheckourjumpHeightproperty• AddaFoxFunction

Page 7: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

CheckJumpFunction• Nameit“checkJumpHeight”• SetthereturntypetoBoolean(TrueorFalse)• ClickOK

Page 8: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

WritingcheckJumpHeight• Draginan“If”blockandselecttrueasaplaceholder

Page 9: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

WritingcheckJumpHeight (cont.)• ChangetheconditionoftheifstatementtocheckifthejumpHeight propertyis1

Page 10: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

WritingcheckJumpHeight (cont.)

•Dragintworeturnstatements•Hereisthefinalcode

Page 11: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

AddingaFoxProcedure- Jump

•Wewanttowriteaproceduretomakethefoxjump•AddaFoxProcedure•Nameit“jump”

Page 12: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Writingjump• Draginan“if”block,selecttrueasaplaceholder

Page 13: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Writingjump(cont.)•ClicktheFunctionstabundertheFox•DragincheckJumpHeight overthetrue

Page 14: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Writingjump(cont.)• Undertheif,draginamoveupbyjumpHeight andamovedownbyjumpHeight• Undertheelse,draginasay“Iamsupposedtobeabletojump1meter,butIamnotcodedthatway!”

Page 15: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

TestJump• ClickthemyFirstMethod tab• Dragina“fox.jump”block• Click“run,”andthefoxshouldjump.Whatdoesitmeanifitdoesn’t?

Page 16: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

AddanElktotheWorld• Click“BacktoScene”• Click“SetupScene”• AddanElkfromtheQuadrupedfolder.• PositiontheElknexttothefox.

Page 17: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

AddingaFoxProcedure- goAround

•Wewanttowriteaproceduretohavethefoxcircletheelk.• AddaFoxProcedure• Nameit“goAround”

Page 18: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

WritinggoAround

• Addaparametertorepresenttheobjecttocircle

Page 19: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

objectToCircle Parameter• Nametheparameter“objectToCircle• Forvaluetype,select“GalleryClass…”

Page 20: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

objectToCircle Parameter

• Inthemiddlewindow,select“SModel”andclickOK.• Thisstructurewillbediscussedlater!

Page 21: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

WritinggoAround (cont.)• DraginaturnToFace andselectobjectToCircle• Draginaturn,selectleftand0.25• Draginaturn,selectrightand1.0.• Click“adddetail”andselectasSeenByobjectToCircle

Page 22: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

TestinggoAround

•ClickonthemyFirstMethod tabanddragina“fox.goAround”block• Select“elk”•Click“run”

Page 23: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

NowSaveourFancyFox

•ClickontheFoxtab•Click“SavetoClassFile”•Nameit“fancyFox”

Page 24: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

FancyFoxinaNewWorld• OpenanewAliceworldwithgrass,setupscene• AddafancyFox fromtheMyClassestab•Wecanseealloftheinformationtransferring.Click“Finish”

Page 25: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

AddaBear

• AddabeartotheworldfromtheQuadrupedfolder• Positionthebearnexttothefox

Page 26: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

TestjumpandgoAround

• GotothemyFirstMethod tabanddragin“fox.jump”and“fox.goAround”bear.• Click“Run”

Page 27: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Inheritance• Inheritanceisessentiallywhenanobjectisderivedfromaparentandinherits thecharacteristicsofthatparent•OurfancyFox inheritedthecharacteristicsofaregularfox•Alice3showcasesinheritanceinanotherwaytoo– classes!

Page 28: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Classes•Rememberthis?• Everythinginheritsfromeverythingnestedaboveitself.•AFoxinheritsfromQuadruped,SQuadruped,SJointedModel,etc.

Page 29: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Classes(cont.)• ThismeansthatweshouldbeabletowriteaprocedureforaQuadruped,andtheFoxandBearshouldbothbeabletodoit• Let’stestitout!• AddaQuadrupedProcedure

Page 30: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

GallopProcedure• Nametheprocedure“gallop”• Dragina“dotogether”

Page 31: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Writinggallop•CreateanewDecimalNumberparametercalled“distance”•Draginamove,selectforward,distance.Click“adddetail”andsettheanimationStyle toBEGIN_AND_END_ABRUPTLY

Page 32: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Writinggallop(cont.)• Inthedropdown,select“this.getFrontLeftShoulder”• Draginamove,andselectdown,and1asaplaceholder.• Inthedropdownontheleft,select“this.”Gotothefunctionstab,anddragina“getHeight”blockoverthe1• Selectmathanddivideby4.• Setanimationstyletoabrupt.• Codeonnextpage.

Page 33: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Writinggallop(cont.)

• F

•Makeacopyofthelastline,andchangeitto“getFrontRightShoulder”

Page 34: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Writinggallop(cont.)

•Make2morecopiesofthelastline.Makethefollowingmodifications:

Page 35: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Writinggallop(cont.)• Dragina“this.move”block.Selectupand1asaplaceholder.Replacethe1withthefunction:“this.getHeight.”Usethemathdropdowntodivideby7

Page 36: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Writinggallop(cont.)

•Makeacopyoftheentire“dotogether”block.• Inthiscopy:• Changeevery4toa2.• Changeevery3toa1.5.• Changeeveryuptodown.• Changeeverydowntoup.

•Codeonnextslide.

Page 37: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Writinggallop(cont.)

Page 38: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Writinggallop(cont.)

•Makeanothercopyofthefirst “dotogether”block.•Removethelast“this.move”upblockinthiscopy.• Thefullcodeforgallopisonthenext2slides.

Page 39: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017
Page 40: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017
Page 41: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

Testitout!

•Putablockfor“bear.run”and“fox.run”intoyourmyFirstMethod andclickrun.• Tryitwithotherquadrupeds!•Doesitworkwithanobjectfromthebipedorflyerclasses?Why?•Onlyobjectsthatinheritfromquadrupedswillbeabletoaccessourgallopprocedure.

Page 42: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · Creating a Fancier Fox and Inheritance in Alice 3 Jonathon Kuo under the direction of Professor Susan Rodger June 2017

SomethingtoRemember• Supposeyouwanttowriteamethodinwhichanobject’sopacityischanged.• AnSModel’s opacitycanbechanged• ButanSThing’s cannot• Thereareotherinstanceswherecomplexityisaddedinsubclassesthatisnotaccessibletoparentclasses.