creating a fancier fox and inheritance in alice 3 tutorial · 2017-06-26 · has new things it can...

11
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” Overview We will add procedures, functions, and properties to a fox and save it out to be used in other worlds. This is called “inheritance.” The modified fox has new things it can do, plus it inherits everything a standard fox can do. We will explore how Alice 3 uses classes to implement inheritance in another way. Start – Add a Fox Select any background (I picked snow). Add a fox to your world (in the Quadruped folder). Choose the default fox. Click “edit code”. Add a Fox Property Suppose our fox can only jump 1 meter, so we will code that in a property. Add a Fox Property

Upload: others

Post on 07-Aug-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · 2017-06-26 · has new things it can do, plus it inherits everything a standard fox can do. •We will explore how Alice

CreatingaFancierFoxandInheritanceinAlice3

JonathonKuounderthedirectionofProfessorSusanRodgerJune2017

AdaptedfromDr.Rodger’sAlice2tutorial,“CreatingaFancierChickentouseinseveralworlds– Inheritance”

Overview

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

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

AddaFoxProperty• Supposeourfoxcanonlyjump1meter,sowewillcodethatinaproperty.•AddaFoxProperty

Page 2: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · 2017-06-26 · has new things it can do, plus it inherits everything a standard fox can do. •We will explore how Alice

JumpHeightProperty

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

AddaFoxFunction•WewantafunctiontocheckourjumpHeightproperty• AddaFoxFunction

CheckJumpFunction

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

WritingcheckJumpHeight

• Draginan“If”blockandselecttrueasaplaceholder

Page 3: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · 2017-06-26 · has new things it can do, plus it inherits everything a standard fox can do. •We will explore how Alice

WritingcheckJumpHeight (cont.)• ChangetheconditionoftheifstatementtocheckifthejumpHeight propertyis1

WritingcheckJumpHeight (cont.)

•Dragintworeturnstatements•Hereisthefinalcode

AddingaFoxProcedure- Jump

•Wewanttowriteaproceduretomakethefoxjump•AddaFoxProcedure•Nameit“jump”

Writingjump

• Draginan“if”block,selecttrueasaplaceholder

Page 4: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · 2017-06-26 · has new things it can do, plus it inherits everything a standard fox can do. •We will explore how Alice

Writingjump(cont.)•ClicktheFunctionstabundertheFox•DragincheckJumpHeight overthetrue

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

TestJump

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

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

Page 5: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · 2017-06-26 · has new things it can do, plus it inherits everything a standard fox can do. •We will explore how Alice

AddingaFoxProcedure- goAround

•Wewanttowriteaproceduretohavethefoxcircletheelk.• AddaFoxProcedure• Nameit“goAround”

WritinggoAround

• Addaparametertorepresenttheobjecttocircle

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

objectToCircle Parameter

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

Page 6: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · 2017-06-26 · has new things it can do, plus it inherits everything a standard fox can do. •We will explore how Alice

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

TestinggoAround

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

NowSaveourFancyFox

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

FancyFoxinaNewWorld

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

Page 7: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · 2017-06-26 · has new things it can do, plus it inherits everything a standard fox can do. •We will explore how Alice

AddaBear

• AddabeartotheworldfromtheQuadrupedfolder• Positionthebearnexttothefox

TestjumpandgoAround

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

Inheritance

• Inheritanceisessentiallywhenanobjectisderivedfromaparentandinherits thecharacteristicsofthatparent•OurfancyFox inheritedthecharacteristicsofaregularfox•Alice3showcasesinheritanceinanotherwaytoo– classes!

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

Page 8: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · 2017-06-26 · has new things it can do, plus it inherits everything a standard fox can do. •We will explore how Alice

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

GallopProcedure

• Nametheprocedure“gallop”• Dragina“dotogether”

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

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

Page 9: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · 2017-06-26 · has new things it can do, plus it inherits everything a standard fox can do. •We will explore how Alice

Writinggallop(cont.)

• F

•Makeacopyofthelastline,andchangeitto“getFrontRightShoulder”

Writinggallop(cont.)

•Make2morecopiesofthelastline.Makethefollowingmodifications:

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

Writinggallop(cont.)

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

•Codeonnextslide.

Page 10: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · 2017-06-26 · has new things it can do, plus it inherits everything a standard fox can do. •We will explore how Alice

Writinggallop(cont.)Writinggallop(cont.)

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

Page 11: Creating a Fancier Fox and Inheritance in Alice 3 Tutorial · 2017-06-26 · has new things it can do, plus it inherits everything a standard fox can do. •We will explore how Alice

Testitout!

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

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