MakingObjectsMoveinUnison:UsingLists
ByDeborahNelsonDukeUniversity
ProfessorSusanRodgerJuly13,2008UpdatedJune2014byYossra Hamid
EditedforAlice3usebyAnhTrinhJuly2016
Overview•Thistutorialwillteachyouhowtouselists.Alistisasortofholdingplaceforarowofobjects.
•Withalist,youcanmakeseveralobjectsdothesamethingatthesametimewithouthavingtowriteoutcodeforeachone.Youcanalsohavethemdothesamethingoneobjectatatime.
•Wewillusealisttomakeagroupofstuffedtigerstoperformsomeactionsatthesametime,andthenoneatatime.
LoadingtheWorld
• Openanewworld,withanytemplate.• Saveitinadirectorythatyoucanfindagain.• AfteryouhaveopenedthefilegointothelayoutmodebyclickingontheSetupScenebutton(bottomrightcornerofanimationscreen).
• ClickontheCameraMarkersmenu.ClickAddCameraMarker.NamethecameravieworiginalPosition
AddingtheObjects
• GototheBipedclassesthenaddfourStuffedTiger objectstoyourworld.
• Addtwootherpeopletoyourworld.
• I�veaddedtheSnowMan andSnowWomanfromtheGallery.
Part1:MakingaList
• Wewillusealist tomakeallfourofthetigersjumpupanddowntogetherandthenkickandspinoneatatime.
• ClickonScenetabinthetopoftheeditor,andthenclickonAddSceneProperty… undertheunmanagedsection.
• Onceaboxappears,setthevaluetypetoGalleryClass,thenclickonBipedintheSelection columninthemiddle.Youwillsee6checkmarksintheobjectcolumnontheleft.Thatmeansthelistcancontainsanybipedcharacterswehaveadded.ClickOK.
Makingalist(cont1)
• Checktheisarraybox• Namethelistastigers.• Clickontheinitializer ->CustomArray… ->addthefourtigersin
ØSeethescreenshotonthenextslideforanillustration.
Part2:Writingthemethod• Dragaeachin_togetherintomyFirstMethod.• Foritemtype:ChooseGalleryClassandcheckonBipedinthemiddlecolumnlikepreviouslythenclickOK.
• Nametheitematiger• Forarray:choosethis.characters,itwillshowasthis.tigers
Writingthemethod(cont1)
Ø Eachin_togethermeansthatalloftheitemsinthelistwillperformtheinstructionsthatwewilladdatthesametime.
Eachin_together(cont 1)• Choose atiger inthedropdownmenuontheleft,dragturntofacetotheeachintogetherblock, andthenselectthis.camera.
• Clickonadddetail…tochangethedurationto0.25seconds.
• Enterinthecodebelowbyselectingthemovecommand,firstup1meter,thendown1meter.Makeeachcommand�sduration.25seconds.
• Playyourworld.Yourmethodshouldlooklikethis:
Step2: inorder• Nowdragaforeachin_underneaththeeachin_together block, chooseBiped asitemtype,onetiger astheitemname,andthis.tigers asthearray
Forallinorder(cont1)ØForeachin_meansthetigerswillperformalloftheinstructionsthatweaddinsideofit,onerightaftertheother(intheorderthattheywereaddedtothelist).
• Wewantthetigerstoeachturntheirleftlegandspinoneatatime.
• However,theonetiger isjustaplaceholder,sowecannottellonetiger toturnitsleftleg.
• Wewillhavetouseaclass-levelbuiltinfunctiontoturnonlyapartoftheobject.
Foreachin_(cont 2)
• Selectonetiger intheobjecttree.• Hoveryourmouseattherightarrowsignnexttoonetiger intheobjecttree,thenclickononetiger getLeftHip
• Draginturn,thenbackward,then¼revolution.Setthedurationto.25seconds.
Ø Seethescreenshotonthenextslideforanillustration.
• Selectonetiger fromtheobjecttreeagain.• Drag intotheForeachin_ againbelowtheleftHip command,andselectturn,then right,then 1revolution.Setthedurationto.25seconds.
Foreachin_(cont 2)• Thenchooseonetiger andselectonetigergetLeftHip likewedidpreviouslyanddraganotherturn,then forward,then ¼revolution.Setthedurationto.25seconds.
• YourForeachin_codeshouldlooklikethissofar:
Thecompletemethod• Hereisthecompletemethod.Don�tforgettocommentyourcode!
Playyourworldnow
Step4:Objectswithdifferentparts• Sofar,ourlistonlyhastigersinit.Addthetwootherpeopleobjectstothelist:
1) ClickonScenetabonthetopoftheeditor2) Underunmanagedsection, clickonthebox
nexttothetigerslist3) ClickonCustomArray… andselecttheobject
youwanttoadd.Ø Seethescreenshotonthenextslideforan
illustration.
Warning• Youcan’taddanon-bipedobjecttoalistofBipedobjects
• Addingthesnowman andsnowwoman tothelistworksbecausetheyareBipedandbothhaveleftHip asthelabelforoneoftheirbodyparts.
• IfyourcharacterdoesnothaveapartnamedleftHip youmightgetanerrorbecausetheworldissearchingforaleftHip parttomovethatisnotthere.
Recap• Listsareusefulforhavingmultiplecharactersperform
thesamesetofactions.• Eachin_together meanseverythinginthelistwill
performatthesametime.• Foreachin_meanseverythinginthelistwillperforman
actiononerightaftertheother.• Tofindthesub-partsofanobject,clickonthearrownext
totheobjectintheobjecttree.Makesureallofthecharactershavethesameparts,labeledthesameway.
• WARNING:Donotaddtoomanyobjectstoyourlist,becausedoingsowillslowyourworlddownsignificantly.