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

Post on 07-Aug-2020

4 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

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

JumpHeightProperty

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

AddaFoxFunction•WewantafunctiontocheckourjumpHeightproperty• AddaFoxFunction

CheckJumpFunction

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

WritingcheckJumpHeight

• Draginan“If”blockandselecttrueasaplaceholder

WritingcheckJumpHeight (cont.)• ChangetheconditionoftheifstatementtocheckifthejumpHeight propertyis1

WritingcheckJumpHeight (cont.)

•Dragintworeturnstatements•Hereisthefinalcode

AddingaFoxProcedure- Jump

•Wewanttowriteaproceduretomakethefoxjump•AddaFoxProcedure•Nameit“jump”

Writingjump

• Draginan“if”block,selecttrueasaplaceholder

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.

AddingaFoxProcedure- goAround

•Wewanttowriteaproceduretohavethefoxcircletheelk.• AddaFoxProcedure• Nameit“goAround”

WritinggoAround

• Addaparametertorepresenttheobjecttocircle

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

objectToCircle Parameter

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

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”

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.

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.

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.

Writinggallop(cont.)Writinggallop(cont.)

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

Testitout!

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

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

top related