Comparing objects and changing color · Comparing objects and changing color This is an...

40
Comparing objects and changing color This is an modification of the July 2009/June 2015 changing color tutorial by Susan Rodger, Erin Taylor, and David Yan By Natalie Huffman Under the direction of Susan Rodger Duke University June 2017 Functions, parameters, properties, and random numbers What is a function? A function is a subset of code that can accept inputs (called parameters) and return outputs Functions help simplify code Examples of built-in functions are getHeight and getDistanceTo In this tutorial, we will write our own functions

Transcript of Comparing objects and changing color · Comparing objects and changing color This is an...

Page 1: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Comparingobjectsandchangingcolor

ThisisanmodificationoftheJuly2009/June2015changingcolor

tutorialbySusanRodger,ErinTaylor,andDavidYan

ByNatalieHuffmanUnderthedirectionofSusanRodger

DukeUniversityJune2017

Functions,parameters,properties,andrandom

numbers

Whatisafunction?

• Afunctionisasubsetofcodethatcanacceptinputs(calledparameters)andreturnoutputs

• Functionshelpsimplifycode• Examplesofbuilt-infunctionsaregetHeightandgetDistanceTo

• Inthistutorial,wewillwriteourownfunctions

Page 2: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Parts

• Part1:createfunctionthatcomparestwoobjectsandreturnstheshorter

• Part2:createaseagullfunctionthatchangesthecoloroftheseagullwhenitisclickedon

• Part3:createasnowmanfunctionthatturnsthesnowmanarandomcolorwhenitisclickedon

• Part4:createafunctionthatreturnsthecolorofthesnowman,asastring

PART1

• CreateanAliceworldandaddobjects• Createascenefunctionthatcanbeusedtocomparetheheightofobjects

• Createparameters

Page 3: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

StartanAliceworldwithgrassandaddlioness(quadruped),bunny(biped),seagull(flyer),andsnowman(biped)

Clickon“editcode”andaddaScenefunction

Clickon“unset”underreturntypeandselect“GalleryClass”

Page 4: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Select“SJointedModel”fromthelistNoticethatallfourobjectshavechecksnexttotheirnames,meaningtheyareincluded

inthiscategory

ClickOK,andnamethefunction“objectThatIsShorter”

Page 5: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Whatdoesthisfunctiondo?

• Functionscalculateandreturnananswer• Becausewechose“SJointedModel”,ourfunctionmustreturnan“SJointedModel”,whichisacategoryallfourofourobjectsfallinto

• Ourfunctionwillcomparethesnowmanandtheseagullandreturntheshorterone

Draginanifstatementandchoose“true”

Page 6: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Clickonthedropdownmenuby“true”andchoose

Relational(DecimalNumber)à???<???à1à1

Youcanchooserandomnumbersinsteadofones,wewillreplace

them

Selectthis.seagull inthelefthanddrop-downmenu,andclickonthe

functionstab

• Ifyouhover yourmouseoverfunctions,Alicewillputboxesaroundplacesyoucandragthemto• Dragthis.seagull getHeight ontothefirst1.0

Page 7: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Repeatforthesnowman’sgetHeightfunction.Yourcodeshouldlooklike

thecodebelow

Wewanttoreturntheshorterobject,soiftheifstatementistrue,weshould

returnseagullOtherwise,wewanttoreturn

snowman

Page 8: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Nowweneedtocallourmethod

• ClickonmyFirstMethod.Rightnowitshouldonlyhaveadoinorderblock

• Clickontheseagullanddragin“say”• Havetheseagullsay“Iamshorter!”

Butwewanttheshorterobjecttosaythat,nottheseagull

Select“objectThatIsShorter”fromthisàFunctionsanddragitontothe

seagull

Page 9: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

• Runyourcode.Theshorterobjectshouldsay“Iamshorter!”.• Tryresizingthesnowmanandtheseagullsotheotherisshorterandrerun.• Butwhatifwewanttotestanobjectotherthantheseagullorthesnowman?Parameters

GobacktotheobjectThatIsShorterfunctionandclick“AddParameter”

Page 10: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Undervaluetype,selectGalleryClass,thenclickonSJointedModel

Fillinthenameasobject1andcheckthebox(thistellsusthatwecalledthisfunctionsomewhereandwewillhavetogoandfixit,sincenowwewantthemainfunctionto

givethisoneaparameter.

Page 11: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Repeattheprocessforanotherparameter,namedobject2

Nowtoactuallyusetheparameters:

• Dragobject1over“this.seagull”andobject2over“this.snowMan”

Page 12: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

NowgobacktomyFirstMethod

• Yourcodeshouldlooklikethis:

• Noticethetwored“unset”places?Thosearewhereweneedtoselecttwoobjectstocompare

• Whenwecheckedthatboxwhencreatingparameters,wewerepromisingtocomebackandfillthesetwoplacesinwithrealobjects

Selecttwoobjectsandrunyourcode!Youshouldtryitmultipletimesto

makesureitworkswithalltheobjectsinyourworld

Page 13: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

PART2

• Createaseagullfunction• Createaseagullvariabletokeeptrackofcolor• Changetheseagullcolorinresponsetoaclick– Iftheseagulliswhite,wewantittobeblue– Ifitisblue,changetored– Ifred,changetogreen– Ifgreen,changebacktowhite

First,wewanttocreateanewseagullfunction

• Gotothedropdownmenutotherightofthesceneimage

• SelectseagullàAddseagullfunction

Page 14: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Wewantthisfunctiontochangetheseagull’scolor,sosetreturntypeto

OtherTypesà Color

NamethefunctionchangeColor andclickOK

Page 15: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Iftheseagulliswhite,wewanttoturnitblue

• Howdoweknowwhatcolortheseagullis?• InAlice3,youCANNOTcomparecolors• Instead,wewillcreateaseagullvariable(calledProperties)thatwillholdthenameofthecolorthattheseagullcurrentlyis

• WordvariablesarecalledStrings

• Gotothedropdownmenuagain

• SelectSeagullàAddSeagullProperty

Page 16: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

• Setthevaluetypeto“TextString”• Namethevariable“seagullColor”• Clickinitializer andfillin“White”– Thiswillbethewordthepropertyissetaswhentheworldbegins

Thepropertypopupshouldlooklikethis

Page 17: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

NowgotothechangeColor function

• Ifyouhavetroublefindingit,selectthis.seagull fromthedropdownmenuonthefarleft

• Clickonthefunctionstab• Atthetopyoushouldseethis.seagullchangeColor

• Clicktheeditbuttontotheleftofit• Draginanifstatementandselect“true”

ClickontrueandselectTextStringComparisonà ???contentEquals ???àthis.seagullColoràCustomTextString

Thiswillletussay“ifthewordinthis.seagullColor(whichis“White”right

now)isequaltoacustomstring”

Page 18: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Aboxwillpopupforyoutofillinthecustomstring

• Wewanttocheckiftheseagulliswhite,sofillinWhite

• MakesuretokeepthecapitalizationthesameasintheseagullColor property!!

Iftheseagulliswhite,wewanttochangeittoblue

• Whenwecreateaproperty,Alice3automaticallymakesaprocedureandafunctiontogoalongwithit:setPropertyNameandgetPropertyName

• SoundertheProcedurestabyouwillseesetSeagullColor andundertheFunctionstabyouwillseegetSeagullColor

Page 19: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Iftheseagulliswhite,wewanttochangeittoblue

• DraginsetSeagullColor fromtheprocedurestabandselectCustomTextString

• Fillin“Blue”• WestillwanttoreturnaColortype,sodraginthereturnstatementfromthebottombar,andselectBLUE

• (pictureonnextslide)

Yourcodeshouldlooklikethis

Page 20: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Weareonefourthofthewaythere

• Iftheseagulliswhite,wewantittobeblue• Ifitisblue,changetored• Ifred,changetogreen• Ifgreen,changebacktowhite

Draganotherifblockundertheelseandrepeatasbefore

• Thistime,selectTextString Comparisonà ???contentEquals ???à this.seagullColoràCustomTextString andfillin“Blue”

• DraginsetSeagullColor andfillin“Red”• ReturnRED

Page 21: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Draganotherifstatementundertheelse

• Thistime,selectTextString Comparisonà ???contentEquals ???à this.seagullColoràCustomTextString andfillin“Red”

• DraginsetSeagullColor andfillin“Green”• ReturnGREEN• Undertheelse– DraginsetSeagullColor andfillin“White”– ReturnWHITE

Page 22: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Weonlywantthistohappeniftheseagullisclickedon

• GototheinitializeEventListeners function• ClickonAddEventListener

SelectMouseàaddMouseClickOnObjectListener

Page 23: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Thislisteneristruewheneveryouclickonanyobject,butwewantittobe

trueforonly theseagull

• ClickonadddetailàsetOfVisualsàCustomArray

Addthis.seagull totheVisualarray

Page 24: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Draginseagull’ssetPaint methodandselectBLACK

• Thiswillchangetheseagull’scolortoblack• ButwewanttowantchangeittothecolorreturnedbychangeColor

• DragthechangeColor functionoverBLACK

Runyourworld!Whenyouclickontheseagull,itshouldcyclethroughcolors

Page 25: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

PART3

• Createasnowmanfunction• Createasnowmanvariabletokeeptrackofcolor

• Changethesnowmancolorinresponsetothevalueofarandomvariable– Ifthenumberis1,wewantittobered– Ifitis2,changetoblue– If3,changetogreen– If4,changetowhite

Startbycreatinganewsnowmanfunction

• Clickonthedropdownmenu• SnowmanàAddSnowmanfunction

Page 26: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Likebefore,setthereturntypetoOthertypesà Color

NamethefunctionchangeColorRandom

Page 27: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Wewanttoclickalocalrandomnumbervariable

• Selectvariablefromthebottombar• Dragintothefunction

• Forvaluetype,selectWholeNumber• Forname,fillincolorNumber• Forinitializer,pick1

Page 28: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Butwedon’twantthevaluetobeone,wewantthevaluetobearandom

number• Under1,selectRandomànextRandomIntegerFromAUpToAndIncludingB ???,???à1àCustomWholeNumber

• Wewantthenumberstobebetween1and4,sowhenthecustomnumbercalculatorpopsup,pick4

• Seenextslideforimages

Page 29: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Yourcodeshouldlooklikethis

Nextdraginan ifblockandselecttrue

Undertrue,selectRelational(WholeNumber)à???==???à

colorNumberà1

Thisbasicallysays“ifcolorNumber =1”

Page 30: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

IfcolorNumber is1,wewanttoreturnred

Repeatforthefollowing

• IfcolorNumber==2returnBlue

• IfcolorNumber==3returnGreen

• ElsereturnWhite

• Giveitatrybeforeyoulookatthecodeonthenextslide!

Page 31: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Wewantthesnowmantochangecolorwhenweclickit

• GotoinitializeEventListeners• PickmouseàobjectListener• AdddetailàsetofVisuals

àcustomArray

Page 32: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Addthis.snowMan totheVisualarray

DraginsetPaint andselectBLACK,thendraginchangeColorRandom over

it

Page 33: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Runyourcode!

• Doesitwork?• Howwouldweknow?• Whatifwehavethesnowmansaytherandomnumber,sowecancheck

GobacktochangeColorRandom• Draginsayandselect“hello”• Thenunder“hello”,select???+???àCustomTextStringàWholeNumberà colorNumber

• BecausecolorNumber isaNumbertypeandnotaStringtype,itdoesnotcomeupasanoptiontosay

• ButifwesayawordandthentackoncolorNumber,Alicewilltotheconversion

• IlefttheCustomTextString blank,butyoucouldalsofillinsomethingalongthelinesof“Therandomnumberis”.Justmakesureyouendwithanextraspace!

Page 34: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Runyourcodeandmatchupthenumberthesnowmantellsyouwith

thecolorheturns

Whatifwewanttofind outwhatcolorthesnowmanturns?

Page 35: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Part4

• Writeafunctiontoreturnthecurrentcolorofthesnowman

Addanewsnowmanfunction

Page 36: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

MakethereturntypeTextString andthenamegetNameOfColor

Nowweneedtocreatethevariable.GotoSnowmanàadd snowman

property• Valuetype=TextString• Name=snowmanColor• Initializer =White

Page 37: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

ForthesnowmanColor variabletohavetherightvariable,wehaveto

changeitwiththecolor• GotochangeColorRandom and,beforethereturnstatement,draginsetSnowmanColorandtype“Red”

• Dothesamefortheotherthreecolors

Page 38: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

GobacktogetNameOfColor

• Allweneedtodoisreturnthepropertywejustmade

• DraginareturnandselectsnowmanColor

Nowtohaveacharactersayit

• Sincewealreadyhavethesnowmansayingtherandomnumber,wewillhavethebunnysaythecolor

• Notethateventhoughthefunctionisthesnowman’s,otherobjectscanuseit

• GototheinitializeEventListeners function

Page 39: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

Wewantthebunnytosaythecoloreverytimethesnowmanturnscolor• DragasaystatementdirectlyunderthesnowmansetPaint line

• Pick“hello”• Thenunderhelloselect???+???àCustomTextStringà “hello”

Page 40: Comparing objects and changing color · Comparing objects and changing color This is an modification of the July ... A box will pop up for you to fill in the custom string • We

• Forthecustomtext,type“Thesnowmanis”• Makesuretoleaveaspaceafter“is”!• ThenselectsnowmanfromtheleftdropdownmenuanddraggetNameOfColor over“hello”

Runyourprogram!

• Clickonthesnowmanandwatchthebunnytellyouhiscolor!