Transcript
Page 1: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

MakingObjectsMoveinUnison:UsingLists

ByDeborahNelsonDukeUniversity

ProfessorSusanRodgerJuly13,2008UpdatedJune2014byYossra Hamid

EditedforAlice3usebyAnhTrinhJuly2016

Overview•Thistutorialwillteachyouhowtouselists.Alistisasortofholdingplaceforarowofobjects.

•Withalist,youcanmakeseveralobjectsdothesamethingatthesametimewithouthavingtowriteoutcodeforeachone.Youcanalsohavethemdothesamethingoneobjectatatime.

•Wewillusealisttomakeagroupofstuffedtigerstoperformsomeactionsatthesametime,andthenoneatatime.

Page 2: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

LoadingtheWorld

• Openanewworld,withanytemplate.• Saveitinadirectorythatyoucanfindagain.• AfteryouhaveopenedthefilegointothelayoutmodebyclickingontheSetupScenebutton(bottomrightcornerofanimationscreen).

• ClickontheCameraMarkersmenu.ClickAddCameraMarker.NamethecameravieworiginalPosition

AddingtheObjects

• GototheBipedclassesthenaddfourStuffedTiger objectstoyourworld.

• Addtwootherpeopletoyourworld.

• I�veaddedtheSnowMan andSnowWomanfromtheGallery.

Page 3: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Part1:MakingaList

• Wewillusealist tomakeallfourofthetigersjumpupanddowntogetherandthenkickandspinoneatatime.

• ClickonScenetabinthetopoftheeditor,andthenclickonAddSceneProperty… undertheunmanagedsection.

• Onceaboxappears,setthevaluetypetoGalleryClass,thenclickonBipedintheSelection columninthemiddle.Youwillsee6checkmarksintheobjectcolumnontheleft.Thatmeansthelistcancontainsanybipedcharacterswehaveadded.ClickOK.

Page 4: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Makingalist(cont1)

• Checktheisarraybox• Namethelistastigers.• Clickontheinitializer ->CustomArray… ->addthefourtigersin

ØSeethescreenshotonthenextslideforanillustration.

Page 5: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Part2:Writingthemethod• Dragaeachin_togetherintomyFirstMethod.• Foritemtype:ChooseGalleryClassandcheckonBipedinthemiddlecolumnlikepreviouslythenclickOK.

• Nametheitematiger• Forarray:choosethis.characters,itwillshowasthis.tigers

Page 6: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

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:

Page 7: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

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.

Page 8: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Foreachin_(cont 2)

• Selectonetiger intheobjecttree.• Hoveryourmouseattherightarrowsignnexttoonetiger intheobjecttree,thenclickononetiger getLeftHip

Page 9: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

• Draginturn,thenbackward,then¼revolution.Setthedurationto.25seconds.

Ø Seethescreenshotonthenextslideforanillustration.

Page 10: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

• 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:

Page 11: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Thecompletemethod• Hereisthecompletemethod.Don�tforgettocommentyourcode!

Playyourworldnow

Page 12: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

Step4:Objectswithdifferentparts• Sofar,ourlistonlyhastigersinit.Addthetwootherpeopleobjectstothelist:

1) ClickonScenetabonthetopoftheeditor2) Underunmanagedsection, clickonthebox

nexttothetigerslist3) ClickonCustomArray… andselecttheobject

youwanttoadd.Ø Seethescreenshotonthenextslideforan

illustration.

Page 13: Making Objects Move in Unison: Using Lists...Jul 13, 2008  · For each in_(cont2) • Then choose onetigerand select onetiger getLeftHiplike we did previously and drag another turn,

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.


Top Related