关闭

关闭

关闭

封号提示

内容

首页 GameMaker--Object Inventory System.doc

GameMaker--Object Inventory System.doc

GameMaker--Object Inventory Sys…

上传者: zpw759 2011-12-05 评分 0 0 0 0 0 0 暂无简介 简介 举报

简介:本文档为《GameMaker--Object Inventory Systemdoc》,可适用于游戏领域,主题内容包含ObjectInventorySystemForGamesByGenePoschmanWhiledevelopingahiddenobjectgam符等。

ObjectInventorySystemForGamesByGenePoschmanWhiledevelopingahiddenobjectgameIneededanobjectinventoryarraythatcouldbepassedfromroomtoroomIneededittobedynamicsothatIcouldadditemstoitandremoveitemsandusetheminpuzzlesAdditionallyIneedittobefairlyopenendedandhaveaviewerthatallowedmetoseesomenumberatatimeandbeabletomovebackandforthacrossthearrayfromlefttorightIcouldn’tfindonesoIcreatedoneInordertodevelopanobjectinventoryarrayquicklyIcreatedagamethatperformedallthebasicprocessesofinteractingwithanobjectinventoryarrayIrealizedthatothersmightneedthissoIdecidedtoshareitPartTwoGameProgramming:HereissomedocumentationandabreakdownoftheprogrammingIdidalongwithsomeexplanationsDevelopmentPlanning:InitiallyIneededtosetuproomswithobjectsthatcouldbedynamicallycreatedornotdependingonwhatIwantedtodoParticularlyimportantwereroomsthatwouldberevisitedfromotherlocationssuchthatdependinguponwhattookplaceitmightchangewhatobjectsareloadedandwhenAnadditionalfeatureofthisexampleshowsonemethodtosetuparoomwithobjectsthatcanberemovedfromaroomandwillnotreappearinaroomoncetheobjecthasbeenremovedNamesandspritesinthisprogramareprimitive,mostlybecauseIwaslazyandwasmoreinterestedingettingtheinventorydesigned,developedandworkingInlargergamesIusegroupsandanamingconventionsthatweredevelopedfrom“GameMaker’sApprentice”AlthoughIsupplysomeimagesyouwillwanttohavethegameinGameMakertofollowalongSprites:ThereareafewpointsofinterestforthegamewithregardstospritesSizeforobjectsininventoryneedstobeconsistentoratleasthaveamaximumthatallinventoryobjectsfitinThesprites’widthandheightdoesaffectsomenumbersforinventorycontrolviewandIwilladdressthatattheappropriatetimeFornowthemaximumforthisgameiswidth=,andheight=PointoforiginandtheboundingboxarealsoimportantforviewingobjectsIsetpointoforigintox=y=,andboundingboxtofullIfyouchangetheseyouwillneedtoadjusttheviewsoftheroomsTherewillbemoreonthisRooms:StartTherearetwoobjectshereOneobjectisforloadingagameandtheotherisforgoingtothenextroomTheobjectforloadingagameistotestanddemonstratethattheinventoryissavedcorrectlyandloadedcorrectlyIntheothertworoomsthereisanobjecttosavethegameThenextbuttongoestothesecondroom,there’sasurpriseWhatissignificantaboutthisroomistheCreationcode:{globalitemsPassed=globalinventoryCount=globalobjflgobject=trueglobalobjflgobject=trueglobalobjflgobject=trueglobalobjflgobject=trueglobalobjflgobject=trueglobalobjflgobject=trueglobalobjflgobject=trueglobalobjflgobject=trueglobalobjflgobject=true}IamusingglobalvariablessotheyareavailabletoallroomsTheVariable“globalitemsPassed”containsthenumberofobjectsthatarepassedtothenextroomThevariable“globalinventoryCount”containsthenumberofobjectsthatareintheobjectinventoryarrayEachtimeanitemisputintoinventory,isaddedto“globalinventortCount”Thearray“globalobjflgnum”isaflagthatindicatesifanobjectwillappearintheroomproperorplayareaTheresponse“true”indicatesanobjectistobecreatedinaroomTheresponse“false”indicatesthattheobjectisnottobecreatedinaroomThisisonewaytodeterminethedynamiccreationofobjectsinaroomInthisroomIamsettingthefoundationforobjectstobecreatedintheothertworoomsAflagcanbedynamicallychangedbeforearoomstartstodeterminewhetheranobjectshouldbepresentornotThisflagdoesnotrelatetoobjectsininventoryonlytoobjectsintheroomproperorplayareaTokeepitsimpleinthisexample,oneobject,oneflagWhenanobjectispickedupfromaroomandputintoinventory,theflagissettofalsesowhentheplayerleavesandreturnstotheroomtheobjectisnolongercreatedintheroom,becausetheflagisfalseTheobjectmaystillbeininventoryifithasnotbeenusedRoom:Thisisthesecondroom,sorryabouttheconfusioninthenamesInthisroomthereisalotgoingonThereareanext,aSaveGame,UseHere,andanInventoryobjectalongwithseven“play”objectsthataredynamicallycreatedThespacingoftheseobjectssucksalittle,andyoumaywanttoplaywiththatalittlelaterThe“next”objectwilltakeyoutothenextroomThe“SaveGame”objectwillsavethegameThe“UseHere”objectisablackboxwhereyoucanuseanobjectyouhavetakenfrominventoryThe“Inventory”objectwillshowyoutheinventoryyoucurrentlyhaveorhideitAllthe“Inventory”objectdoesisturnthevisibilityoftwoviewsto“true”or“false”SeeviewandviewfortheroomIntheroomsettingstheroomheightispixels,butactuallytheroomispixelsinheightBetweenyandyinheightaretwoarrowsthatcontroltheviewingoftheinventoryarrayhorizontallyTheinventoryarrayresidesbetweenyandyinheightandasfarhorizontallyasitcanwithinthelimitsofGameMakerEveryroomthatusestheinventoryneedsthesethreeviewsasdefinedinthisroomTheheightofspritescontrolstheheightofthesecondandthirdviews,andthewidthofspritesuptointhisexamplecontrolthewidthofthethirdview,thoughnotthewidthofthearrayThebuttonsinthesecondviewmustbeoutsidethewindowofthethirdviewInthisexampletheyneedtobemorethanpixelsapartThisiswhyspritewidthandheightcontroltheinventoryarrayCreationCodeforthisroom:{globalstreetCnt=globalinventoryCount=globalitemsPassedglobalinventoryOne=ifglobalobjflgobject=true{instancecreate(,,object)}ifglobalobjflgobject=true{instancecreate(,,object)}ifglobalobjflgobject=true{instancecreate(,,object)}ifglobalobjflgobject=true{instancecreate(,,object)}ifglobalobjflgobject=true{instancecreate(,,object)}ifglobalobjflgobject=true{instancecreate(,,object)}ifglobalobjflgobject=true{instancecreate(,,object)}}Thevariable“globalstreetCnt”isthenumberofobjectsputintoaroomItrelatesmoretothehiddenobjectaspectofthegameandisnotreallyusedinthisexample,otherthanwhenanitemisclickedonissubtractedfrom“globalstreetCnt”NothingmorehappenstoitinthisexampleForallrooms“globalitemsPassed”isaddedto“globalinventoryCount”,thishelpscontrolthepassing,viewing,andloadingoftheinventoryThevariable“globalinventoryOne”startsoutasthenumberoftheobjectsthatcanbeintheviewwindowwidthWhenthe“globalinventoryCount”exceeds“globalinventoryOne”thenthearrowsbecomeactivetomoveviewbackandforthpasttheviewerTheseriesofifstatementsarewhatcreatetheobjectsintheroomIf“globalobjflgobjectID”istrue,theobjectiscreated,whentheflagisfalsetheobjectisnotcreatedWhenanobjectisputintoinventorythen“globalobjflgobjectID”issettofalseExceptforspecificswithregardstoobjectsinplayandthe“back”objectratherthanthe“next”object“Room”thethirdroomissetupprettymuchthesameas“Room”Objects:showInv:Iamshowingthisobjectfirstbecauseitdealsmorewiththeviewsthanwiththeinventory,andifyoucan’tseetheinventorythenhowdoyoumanipulateitBythewaytheinventorydoesnotneedtobevisibleforobjectstoendupthere,butifyouwishtoretrieveanobject,thenyouneedtoseetheinventoryOnceanobjecthasbeenloadedintoinventoryandyoutakeitout,youneedtoseetheinventoryinordertoputitbackAsimpleleftpressandachecktodetermineiftheviewsarevisible,iftheyarenotmakethemvisible,andiftheyare,makethemgoawayNoteveryobjectneedstogointoinventory,althoughinthisexamplewiththeexceptionofthenavigationobjects,allobjectscangointoinventoryObjectsthatcangointoinventoryhaveaparent,andintheexamplethatparentisobjinvobjinv:Icouldhavedonethiswithascript,butsincethisobjectcontrolsallofitschildren,itseemedlikeagoodideatodoitthiswayAfterallwhatisaGUIinterfaceforifnottomakestufffunandinterestingIamstartingwithanexplanationofthe“LeftPressed”eventbecauseit’sthefoundationandthentheothereventsmakemoresenseWhenclickingonanobject,ifitslocationislessthany=thenitisabovetheinventoryanditisintheroomYes,Iknowthattheinventoryarrayisaty,butthescreenisonlyandbetweenyandytheviewoftheinventoryarrayappearsIftheconditionismetandanobjecthasobjinvasaparent,thenitisanobjecttobeplacedintoinventorySubtractfromthevariable“globalstreetCnt”,ahiddenobjectrequirement,notimportantforthisexampleSetthevariable“selfinvPos”tothe“globalinventoryCount’Thisisthepositiontheobjecthasintheinventoryarray,anditisneededwhenreturningtheobjecttothearrayAddto“globalinventoryCount”Byfirstmovingthesubscriptoftheobjecttoitsownvariable,multipleobjectscanbeclickeduponbeforethecodeofapreviousobjecthascompleted,trustmeIranintothisanditwasabastardtodebugThe“sleep”formillisecondsisnecessaryforquickfingeredplayerstomakesurethereisaslightdelayTrustmeForfunremoveitandseewhathappenswhenyouplayfastfingeredSetthevariable“globalInventoryNameselfinvPos”=“idobjectindex”Thisloadstheobjectinventoryarraywiththeobjectandthetransfertothelogicarrayiscomplete,nowtocompletethetransfertothephysicalandvisualarraySetthevariable“globalobjflgidobjectindex”tofalseTheobjecthasbeenpickedupfromtheroom,andtheflagissetsoitwontberecreatedintheroomwhentheplayerreturnsThedirectionandgravitywasjustafunwaytoshootobjectsintotheinventoryItmeantsettingup“OutsideRoom”eventtoplacetheobjectinthevisualinventoryarrayforviewTheobjectisjumpedtox=selfinvPos*y=RememberViewisy=,andx=selfinvPosisthesubscriptoftheposition,isthewidestanyspritecanbeEachitemismovedintoinventorypixelsafterthepreviousobjectsxIfmouseyisgreaterthan,thatmeanstheobjectisalreadyininventoryandyouwanttopickituptouseitThecursormustbethedefaulttodothisThisIdidputintoscriptform,itwasjustquickertodoitthatway{cursorsprite=objectgetsprite(idobjectindex)globalactiveCursor=idobjectindexglobalcurItemPos=selfinvPoswindowsetcursor(crnone)instancedestroy()}Thefirstcommandsetsthecursortousetheobject’sspriteThenextcommandsetsglobalactiveCursortotheidobjectindexThishastobedonebecausetheinstancefortheobjectisdeletedandifyouwanttorecovertheobjectyouhavetohaveitstoredsomewhereThenextcommandsavestheobjectspositionwithinthearraySetglobalcurItemPostoselfinvPosDropthearrowfromthecursorDestroytheinstanceoftheobjectthatwasjustpickedupIcannoweitherusetheobjectintheroom,orIcanreturnittoinventoryhencetheEndStepeventTheEndStepEvent:Toreturnanobjecttotheinventorythetestsarearightmousepress,mouseyneedstobegreaterthanandthecursorcannotbethedefaultcursorScriptcode:instancecreate(globalcurItemPos*,,globalactiveCursor)Thisiswheretheobjectissetuptoberecreated“globalcurItemPos”*isx,isyand“globalactiveCursoristheobjecttobereturnedBecauseofthe“OutsideRoom”eventthenexttwovariablesmustbesetintoplaceAndthenofcourseresetthemousetodefaultAtthispointwecanputobjectsintoinventorywecanpickobjectsupfrominventoryandwecanputthembackintoinventoryOhandwecanseetheinventoryWhatyouchoosetodowithyourobjectsisuptoyou,forthebenefitofthisexample,Ijustmakethemgoawayintoablack,orratheragreenboxUseHere:Thisisasimpleblackboxthat“uses”anobjectTesttoconfirmthatthemousecurserisnotthedefaultIfitistruesetthemousecursortothedefaultSetthevariable“globalinventoryNameglobalcurItemPos”tofalsewhichremovestheobjectfromtheinventoryarrayThisprovidesthebasicsforwhatyoumustdowhenyouuseanobjectnextandback:Theobjects“next”and“back”performtwoactions,firstisascripttocleanuptheglobalinventoryarray,andthesecondistonavigatetoanotherroomThescript“scriptpassinv()”consistsofcodetocleanuptheglobalinventoryNamearrayIsetthisupasascriptbecauseIwillbecallingitfromabunchofdifferentobjectswhichnavigatefromoneroomtoanotherscriptpassinv():{globalsubFrom=globalsubTo=repeat(globalinventoryCount){ifglobalinventoryNameglobalsubFrom=false{globalsubFrom=globalsubFrom}else{globalinventoryNameglobalsubTo=globalinventoryNameglobalsubFromglobalsubFrom=globalsubFromglobalsubTo=globalsubTo}}globalitemsPassed=globalsubTo}SetupacoupleofsubscriptsforfromandtopositionswithinthearrayThisistopassbytheemptyspaceswithinthearrayofobjectsthatwereusedintheroomCallrepeatusingthenumberofcurrentobjectsintheinventoryasthecontrolnumber“globalinventoryCount”Movetheobjectswithintheglobalarray,skippastemptyspacesincreasing“globalsubFrom”byandnotincreasing“globalsubTo”ThiswillfillthespaceswithobjectsfurtherdownthearrayEverytimeanobjectfromthearraywasused“globalinventoryNameitsarrayposition“wassettofalseThiscleansupthearrayfromroomtoroomOncethecleanuphasbeenaccomplished,set“globalitemsPassed”tothesubscriptofthelastobjectmoved,“globalsubTo”loadInventory:ThereisahiddenobjectineveryroomthatisusedtocreatetheinventoryfortheroomanditisloadInventorySetupaloopcountertoprovidetheobjectspositioninthenewroom’sinventoryobjectarrayCalltherepeatcommandusingthe“globalinventoryCount”Setavariable“invobject”=aninstancecreate()usingtheglobalinventoryarrayasthesourceand“loopSub*”and“”asthexandycoordinatesloadingtheobjectstobeusedinviewBelowisawaytodothiswithascriptandGUIIusedascript,buteitherworksScript:{invobject=instancecreate(loopSub*,,globalinventoryNameloopSub)invobjectinvPos=loopSub}GUI:BothoftheseaccomplishthesametaskIpreferredthescriptbecauseIcansaveitinatextdocumentInasensetheGUIinterfacewasjustanothermeansofusingscriptingRightArrow:ViewingtheinventoryissimplyamatterofadjustingthepointersoftheviewsoyoucanseetheobjectswhilenotgoingbeyondthearrayineitherdirectionConfirmthenumberofobjectsininventoryisgreaterthanthe“viewingnumber”,inthiscase,The“viewingnumber”canbeadjustedbasedonspritesize,gamewindowwidthandhowmanyobjectstobeviewed,makesuretoadjusttheviewintheroomandthecodethataccessesitThevariable“globalinventoryOne”orwhatevervariableyouwanttouse,willneedtostartoutasthe“viewingnumber”Once“globalinventoryCountisgreaterthan“globalinventoryOne”,thevariableviewxviewisadjustedbythemaximumwidthoftheobject’ssprite,inthiscase,ThevariableglobalinventoryOneisincreasedbyindicatingthatthepositionofviewhasbeenshiftedEachtimetheobjectisaccessedwithaleftmousepressthisprocesswillcontinueuntilviewhasreachedthelastobjectinthearrayLeftArrow:Theleftarrowiscontrolledentirelyby“globalinventoryOne”ThearrowisonlyactivewhileglobalinventoryOneisgreaterthanthenumberofobjectstobeheldintheviewWhenpressedthevariableviewxviewisreducedbythemaximumvalueoftheobjectsspritewidthwhichinthiscaseis,andglobalinventoryOneisreducedbySaveandLoad:TheobjectssaveandloadarepresentfortestinganddebuggingpurposesonlyThiscoverseverythingIcanthinkof,althoughIamsuretherewillbequestionsContactmethroughGameMakerStationandIwilltrytobeasresponsiveaspossibleIfyouusethesystem,pleasecreditmeasfollows:GenePoschmanRenaissancePlayer

用户评论(0)

0/200

精彩专题

上传我的资料

每篇奖励 +2积分

资料评价:

/12
0下载券 下载 加入VIP, 送下载券

意见
反馈

立即扫码关注

爱问共享资料微信公众号

返回
顶部