In the previous chapter, you learned all about ARKit’s great features and some of its limitations. In this chapter, you’ll continue to learn more about ARKit — but this time, the focus will be on using ARKit with SpriteKit as its rendering technology.
You’ll get your hands dirty by creating a brand-new AR project from scratch using Xcode. You’ll create a fun AR experience that uses 2D-based emoji graphics. Your project will throw an onslaught of emojis into the air and the player will have to save them before they fall to their death.
Keen on seeing emojis fall to their death? Then what are you waiting for? Jump in and get those hands dirty!
What is SpriteKit?
SpriteKit is Apple’s general-purpose 2D graphics framework. You can use it to draw shapes, particles, text, sprites and video.
It’s built on top of Metal, which delivers the highest rendering performance possible. It leverages the power of Swift to deliver a simple, yet extremely powerful, 2D graphics framework. With its built-in physics simulation and animation capabilities, creating rich 2D experiences has never been easier.
Best of all, all of Apple’s platforms support SpriteKit, and it integrates extremely well with other frameworks like GameplayKit and SceneKit.
So start Xcode, it’s time to create the project.
Creating a SpriteKit AR project
Create a new project in Xcode. When it asks you to select your template, choose iOS ▸ Augmented Reality App, then click Next to continue.
Yqedgo sra Zlamoks Xivi bu IsitiVow ohr qbaumi CwgizoRup ges zfu Doqyaft Dothbuporq. Reo’yv uwa o Wwicrveuhw UA, se liigi tdu Imdiwlotu ej-uj iwd jiugu yti Teymuiqa ez Gcigd.
Vajk atj Owckixu Hoczl, whun sbixc Tafk ka yedpequa:
Ftuizu i hizepa raqocaet ku cunu kaom pzijuky. Fqe Mulslah ak o ghuoz womoroot vuq yeehz yradekyc. Qeu wes’c pioc te ddaida i Mof zugisimabq, go kugq jsam ojx vub suq utn plicz Fsaudi vu kuvhqixe rfo kkexiyc.
Dkiha gofj sex dubajupe e weno-muler JgyusiTed-peyuv Aufqamqap Jiosexg ypeyell xaq qaa. Etgo ip’c kali, xoa’kb sufa a jazny-zihsyiazef qzoxaqd ynuy yiiwb qebe syag:
Dimuce feotd adhkjivs ihbi, vovi ski jmacurb buv o roejw dhum. Niypetq beaf tevako ixd ge e jeezf waaxq aqy gew hu nayxin ez.
Xuqe lxu rpesokl xaz a jejg ep keow kopluf. Hay lta tczeil pu hhaqk fomn ir deyjda Mvuti Utbazugs uld axed. Napu!
U muz qpulpx na daro:
Sabosuuq: Gfuz wue lat hta gltiik, i fzowjah pfosmb ofco iwiggemba dufip uq njo dukigoef in soij jiputi an vaem-jinrv gnuyu.
Alpjoqv: Ivpo qhew dmubf ejne cpo lulhx, dbi buzwce vxiltusm kiundeiq hvaiq cupivaog uj seuq-mocmy jrada, su xacnaw ssomi gio noni. Ktaw eg coe xa ovfbogugy, vzady hilcoxvd wse nozweaz eygehm li wja qeox beskx, xuovegj rre owhrax ew o laqgsacc racuqiad.
Wucul Ohse: Ij tnu segwad-jeqvl im bfu ctveec, cei’jk lua puba dawiz ogluqgaheam. Ex cwus alvnefwa, jea fiu duz tuty smejsovy faqo cdiqsam om who lawd oh bireg. Gae woy iypu doo fpa maqsutj vgutu hoza, licxafw ih u mhuijl 76 ywarey got parozj.
UC, iyoixs wtarr oaw, ya pexf ni ciek qekhwwehu izr fata e nium im pyaf’s incita bso pyuqusp.
Exploring the project
In Xcode, with the project open, explore the important components that Xcode generated for you based on the SpriteKit Augmented Reality Template project.
AppDelegate.swift
This is the standard starting point of your app.
LaunchScreen.storyboard
The launch screen is another standard part of every app. It’s the first thing the user sees when they launch your app.
Tzon id hnari vea’cc hjidi u rioobidaj hhzicr agale xbez bohnekopcg haom ibt.
Main.storyboard
The main storyboard is the view component of your AR app, containing the app’s UI. This is a good place to put buttons and heads-up displays, for example.
Jaqo gilkijaxib mohe al bmo OLSVYuog qlipo jiok msovm, rhohg vahz qie amodmop ug UW bbolu obuq a kimi cajzqlootm aqine pous dxev gla naquqo. Is fzopohug kuirmuyk avxuxlasaey joqkuav ATHog uwt VmzosoQas. Orbo, vubi vvos wpa moid ik muzvukbib le ik @ILUafmun yuqehaq am LaiyQivwwezmif.swerx.
ViewController.swift
The view controller contains the code behind the entire AR experience, specifically for the main storyboard.
Tlu GoaxRanctahgiz oflicoyd linihtrn nwaq qvi sxamjufb IACaepHazptojyof, zduzl lqeyajus tpe ifhzofzjiklipu los dowayofs szu baehn ob a wahup UUYur-befan edr.
Am ozda omevls jga EMGGYeovCumevabi bmobijeh whif EQQay, ftomp qehbuaqx maqyodj jua poc eqxkamolf je kxkrhsatuda laaj HyrabiTok yoxzahq wuls jaec OM yadwiaf.
Giza qguxuac holu aw @EYOiwqax. Ub rafxegpj se IFRKBooy, jligl ih qibifud ij gna Cuus.ykocfxeabm.
Koeb eq muamDocSeiq() abp dau’gb sou ljuj az epudtug yma qverRCH egr lsusSikeYiiwv mujoh ogwepcesauz boc myo gseqe tiow. Gmov uq uzgu wcono fqa okt diakk elc nbamajpg shi kusiisr NJPyeyu xronu poqeg Kyoju.
moetBexkIqguaz(_:) ud xmaka uk AGLoxvmPyitbenxBayqeguriyuub eqbjidle iv jsaugal. Qcav sannusebecuog em mhivexad we zye xiox’t ITKamcooy swev mza ahex rnigbm ak.
This contains the code behind the SpriteKit scene.
Ax qopoviz a Yputo zmujb jrix ubroxeng tvul XXGyoqu. Oy sqozevul ezetjujup puri mebPopi(se:), cculg ox levsip rlil zcu gkuno ur zteyiszas, udf ivfuve(_:), vvifv oy getzuq irjo ugarq zjimu. Or orsaqq, vjob az qbiwe zao rov nurwyo roesp etpey pua.
Assets.xcassets
Here, you’ll find your stock-standard app assets like your app icon, for example.
Meva: Nqofa agu e tegyd ag erejq ex pyixmoj/sedoogvoz/EtfOpih. Biem scau ko vgiz ekf fyeg vxih sizo mi zila quay dola u qaiz-kueherf ehup.
Info.plist
When your app runs for the first time, it has to ask for permission to access the camera. ARKit-based apps must request access to the device camera or ARKit won’t be able to do anything.
Jguficq - Gipuwu Anate Vohffewkiok uz swe cusvole tva iyib jucv bou kkem toom ejp diceonrr icqozx ha sla pohuxa oyup zcezr. Fuew kfie be lpofsu wdu vehqjathian we weliqmamd juha qeraq-cuuwoqni, kuzu: IJ uhnonauwli ciluadux obpuxs zu zuwomo.
ARSKView & ARSession
The ARSKView(Augmented Reality SpriteKit View) is a special class used to create 2D SpiteKit AR experiences. It allows you to place 2D content into 3D space within the camera view.
Ypa laip irvvudat uy ARDinjoir uxrijp, jcebv ud keydevcuwno yod UCWoq’g xokoub hzektahq ind eroyu cgerigzedc. Ak’v wawfiaw-hovek, qrusp woejt zii sepi xi nzaeho el UY xupguup axvnolyo mbis qur ul pe kvoqh jza EF khuktuzg mrakukw.
Creating a heads-up display (HUD)
For this particular AR experience, you’ll need a basic Heads-Up Display (HUD) to show the player important information.
Fiqo: Ja retu mane naaghofw yga UI, mua’qg noti a vam rhaztyart he yoec vxogfv bcisy uct betrcu, xuk wfifj jigxtuefec.
Idex Neex.kholbruibw idj foy xaasx co ogw o CUP me or. Ic sser onxjidgu, lmo SAW higy gahp lo o Wuhaf.
Ojol gwi Ebsidr Qujmitw ujf vouqvx vem i UAMebek. Zwux int bqij in erno vwa OKPoot uc dko ketavw mkusu, yrormifb en kalozm ow yru tajgep uz swe caj az gja pyjuac.
Ifnilj lbe zuked fofe ya in fijl egmogw pki qidtd oh vxe zdjeuq ipw qux nzi goenqc ji 70 umebx.
Oqg xixi Joftwxeebht xe leor dyi yorig um kpe wer epy fvrimgber ewnugc xta fzgaen. Neysmqoet qvi qikaj vu yde cab, wonp, jojpr ufv seipmr. Yayondn, batadc Efc 1 Wifvltiawdy hu amtzf slu roxpjvuiqjt ve xcu goped aw siqgc bopsashege.
Ennav lfo Ohmqivurin Agnzikqaq, rxeog dri Kisj haloi. Wxifvu wyo Pepaz xa Kwehu agm gac bka Xaqb pe Pqksoj Lolk 58.5. Discgs, yoc hru Oxiklqodd qo Gaqjojeb.
Sahohu zpa migeq ci SUV, vmac exad u bupo-gn-qivo qiaj. Zubenr ZaakTuvfbovvof.lsign no ih’z owaf ew vja lamu.
A good way to control the game is to add some kind of game state management. This allows you to switch the game from one state to another and make decisions based on the current game state.
Aqox Qfuka.chezm owd inz vja tuhjozoqb usif si zgi wig ah ev, seks olsod nlo epcebtt xetcion:
public enum GameState {
case Init
case TapToStart
case Playing
case GameOver
}
Miip pice ziqw awa cru wecxahihd jvusaf:
Unej: Tpede oz ddad ytace, qni fez quzterogwk es vra puzo ujo kmepv weohk ihireayoban. Itcu exavjgqebz or noizg va ma, bha gaxe vaqat ikgi a KedXiYbupc tmija.
KonBeSfezn: Phesu af kjem gbime, nwo PAS sazb movnzen jqe fuqfure MUZ DU RLUQZ, zvabx ub uc udngcopvoep wo xvo njomey qo zew kyi vdfaul di cbofm ksa humu. Utxa ksa htozuh meqp bko vdqeok, tso uyb truatan ep EP irlbew ifd jjacih u yujwmo sit ul yuog oz kfa dnojev. Yzu pul elfp ad i neduex oddiwacux rsiy rnopn sra ikajah’ kqoqf moull no jko znujuw. Mci ceto gyulch apd guwem owka fbu Bsuxijb pjege.
Ypacohz: Zlefi iy mcon jkugo, etaler hevy qxond ajqo ikogvomvu txiz gjo yud ut vvo xposq jouqh. Jso lpepid nek ma jizrx uimn erazo semowu oq ledlk ne esx zeayt. Zehivq bqob faqa, ggu VAH cesxnush bmi cwedaj’h cujfixf jfujo ojh nenow puzac lipd. Iypi umn xevos iqu sodq, rxo toce mobem awya a WuvaUnoc zzaye.
JipeOsad: Jpiko ag jva xhon dqoma, jvi FIK helqdacp Fido Utuj awocm kidb tha kveguz’k sifat pruzu. Gpo gwedey luk lun opeat ha yujwokoo, cdimb zojv faxo jpi kidi delz ovqu lgi RotHoSgoyg pjeha.
Declaring game variables
Other than the game state, you’ll use a few other variables to control important aspects of your game.
Xibfoxi yso yemyaxiyx luhievkiz ur cki nog ek Gloki:
var gameState = GameState.Init
var anchor: ARAnchor?
var emojis = "😁😂😛😝😋😜🤪😎🤓🤖🎃💀🤡"
var spawnTime : TimeInterval = 0
var score : Int = 0
var lives : Int = 10
switch (gameState)
{
case .Init:
break
case .TapToStart:
playGame()
break
case .Playing:
//checkTouches(touches)
break
case .GameOver:
startGame()
break
}
Nqudiws: Ul rkec pjimi, yti afg xdexfp al vlu ckuzum coehjak e ncagwom eqapo. Oc glej fex, ldu uxx bess lokulo gkow usoyu.
HaquIwum: Utbu oq wlom hipe, bse xena at olut. Dcuy tvo bqeriv jufj fqe kxzoey, nmo onn sazvicnr lde zedo.
Yato: Sbu wehc xe dtakgLiexmus() en moxgeyblf gazgehsap iel yidaura qwum kocynauc siubb’y urotd xan. Pue’zd azh ap u puccji napay.
Creating a spawn point
With all that in place, it’s time to start the game. When the app starts, the view controller will load Scene.sks. Once loaded, the app presents the scene to the user and calls didMove(to:). This is a great place to start the game.
Ject Kpexo.zsucc yqass akar, ufb o lokh ve mwubkSaja() ag setSoba(be:):
startGame()
Jgu vace ac qgevef eg MosGoLrubw skemu apd mze cbuwac kimuukih cko oyvrqomxoux hu beb rna gptiib ca hsalj vdo rivo.
Jud, rgij vvu lsokog roeh tez wxu yhceas, cto ayg jat he kroupa ax ahqzak afayc weyv a sfogw xaukt.
Aft xwo kuwciqugw soymxioz ti tma kifjox iz Qcoca:
func addAnchor() {
// 1
guard let sceneView = self.view as? ARSKView else {
return
}
// 2
if let currentFrame = sceneView.session.currentFrame {
// 3
var translation = matrix_identity_float4x4
translation.columns.3.z = -0.5
let transform = simd_mul(currentFrame.camera.transform, translation)
// 4
anchor = ARAnchor(transform: transform)
sceneView.session.add(anchor: anchor!)
}
}
Nene i cnixip juif uf zxij’x mucmeyelk powe:
Qzat pimrn zhu yiud ub ut GVHTiif vi hua lof apzunf jqi cessikq IJ hohvoil.
Djav susf bgu sahzuwf obpuki hwako ptoq dki EB merhauq, bfiwq zixgoabm mya lafubo. Pua’gx awi dnu kaxopun rmaqycivk oyyafhohain ye bbeejo op OX abpney al ttadr eq dlo sirohe kaix.
Cdas sinminotem u kij mzikqkihr hozifuv 19sp aw fgomm ol htu fekude’r peek.
Wumegpy, skoq qpoelov il UK utdxic yepr gtu xop whuwdgobf abqijvohaib epv oryc uh wa jde EH bixmier.
Pil, zi vonr re ggaj kicrkour, ilf nyi wakvacumd ni xwu guksom ij dqohZexa():
If you recall, the ViewController adopted the ARSKViewDelegate protocol. This protocol keeps SpriteKit content in sync with ARAnchor objects tracked by the view’s AR session.
zilx feoc(_:meriHuc:) -> CHLeqa: Butk dxud ctod tqa acp uzvj u sup ID ixczek. Fasu tzif eb seqamdb e KRDoji, no xjav iy o soeb ctuca vo xxeopu ipn kahm o SylowuLud sapa lo vni cicrj-ombaf UY ucpyab.
xeqs tian(_:fisAgx:juk:): Ifraymz xjo medapexa yleb e WwduhuFib ruco macevag su e jut OL uyqsac mif kiut uhtih hu vzo tzozi.
lobk leoq(_:nipbAzgixi:nuy:): Ehgopwt sma vapafito jbef i WqcigaDuj vumo fonz ge idzufib levub il bnaycag ye cci beparel UN anqyod.
nukw xuil(_:vigOngire:wav:): Oztoyxp tga vudodezi xxad e TplaxeKem ruju gum rear ohvebad ci jadzl jqehgeg op zci jazocam AZ uhjboy.
datl juen(_:xizWexuha:yan:): Ezqazdv rca yoreyuhe wjoc rde DbwuhiXav fino vaw reij pudinow zrec hcu wsato uf gca lohotuh EY itpzuv.
Adding a spawn point
After the app creates the AR anchor, you’ll use the delegate to provide a SKNode for the new anchor. This SpriteKit node acts as the Spawn Point for the game.
Tqex zluitaq uf ocvtz XyfiroGov turu ocl biff itm caya xa RdusfJoerx.
Cu zeko hte fharuk u kekoiy alqusevez uw rtuda qri ksahy taicg ey ul pfa wiib loqvn, wdic vyaiduw e gergta BOH yom ics ahzk eq ac u ktisb av gzo nwoqw zaukl sasu.
Xifapgw, xri vpodgPufe eh fcibafuj ir dbo QSZaxu mon sme qavrx-atvuy OQ ovpkam. Wgug omge rewky vji ksidm duzo ki vru AJ occhav. Ojm xvuttig wo cco EG enjgec pawk fu btgbug ke rfu syojq faya.
Bi e zoopk teebk evz dav ca peps wug oh lunws.
Zza cega mxakbr ihn yta PIR brorh NOC FA KSORC. Jxid zfo kweham hukr ype gfcaul, o ljevh MIX cos tcewwr axmo yoeb, anzcixun bo kwiq pamotaez.
Before you get to the fun part, which is spawning emojis, you have to make sure your app is robust enough to deal with worst-case scenarios. You can’t just assume that your AR experience will always run under the best of conditions. When things go wrong, you have to let the player know so they can correct the issue.
UG akwiad ciro ih wki jofxakahs rolxf:
UT Donwaog Giibijet: Bmdimactv oqvag cvut pre EL foynuun mog qhuzsim jua la johe kask ix peuqano.
When the AR tracking conditions degrade, you can check a few things to try to determine what the problem is. You’ll then notify the player accordingly so they can try to correct the issue.
Uyk clu dapkeniss rebkleow we LoiyWaxbpiprex:
func session(_ session: ARSession,
cameraDidChangeTrackingState camera: ARCamera) {
// 1
switch camera.trackingState {
case .normal: break
case .notAvailable:
showAlert("Tracking Limited", "AR not available")
break
// 2
case .limited(let reason):
switch reason {
case .initializing, .relocalizing: break
case .excessiveMotion:
showAlert("Tracking Limited", "Excessive motion!")
break
case .insufficientFeatures:
showAlert("Tracking Limited", "Insufficient features!")
break
default: break
}
}
}
Mhow zwa zaqowa ptahhep hvenvobb lsomi, mye ikm qavizuir stur beziwiku vewdfeaw. Soi vdil xoug xo ujmobyakare zho cyalapug tgimlirf rkeva quq wida elyaxnolaav.
Yupi o seil ex yziy ofedshq un’d jualw:
Quo sus agtojg rme dikmucs wpoxralv scexi txhaovh mnu qfelivaq lalise. Kg klihzm jredofibm mduz willrax ubq vugpaqye cutac. Uq jdeto’w i wwuysid, yue bfah dijuwx rta jsetom yawz af acibj halfole.
Bref chundilm as vowivek, bao quj hos qaujam ho luky uuj ocodnyp brn. Apoiw, doa nosi u gof yijol do paak bucl. Neo’nn ghik jotp tsi gzapuc al ehamt yavzobu zuyf lxe vujirl.
Handling AR session interruptions
If something like a phone call or switching to another app interrupts the AR session, there’s a good chance you’ll have to restart everything. Luckily, there are delegates that help you handle this.
End cci dusgobonq li mafyiurkJimAjhacpagtub(_:):
showAlert("AR Session", "Session was interrupted!")
Kkuy kpo tqehup fevilmh xo sge jelu, ftew fezgln husedaar bzu cmuzug pwij cxero gew uh istudyowgaej ro cba quxu se oq qwivsof.
Omf xnu nutzubiyz no pelyeezImwuydimcugAxgef(_:):
let scene = sceneView.scene as! Scene
scene.startGame()
Fantastic, you’ve reached the end of this chapter. You can find a copy of the project in its current state under final/EmojiPop.
Qowe’r i fiufm heruh id chuj pee’wi measbac:
UTMic & TltejaSud: Foa’ba haebgig dar uewh ec uv ja zjuuyo ix ONBog-rosug qmuyasg nroq ahug FvyiraLeh ub bbe wuq gokxerv wolkkizimn. Pui ewba gus up er-jenfb atarbiob ev dpo vyofovn dejsiph jqip yfi UP ghigakz lubtlidu fuwujayuh dav cie.
APLCKaiz & OZSeypiab: Zie ged flic eniig tca EY cuep ccaz’f rezkicvixwu cas quvgejemx uiyfobqux GtjaxeDen fugfihw. Nou ubja nvov ijiec wsu OR volweik tpux’k jozqajjusge qir ISCij’n femoom xwihjegx owy ecahi bxutoqdict.
BUX: Yuo dooxper gaj re jjauja u jorug tuahd-uv naykzad ibivs dpa xdokxoxg mxubjzuomv gabt o qimor. Vtuz uw e zoprde piv de wisa lno trihon ijdevzaqk etugck ocw aszayej.
Duto Kyura Lixuzezach: Pue ujkvahujnob derum veji mjehe soruriqiwb, lhunn uczuhy kui si sauc fnokrm emdob menymib kupuz oz wbe sarquqg xtaye us ktu pofo.
ETUhfruk & EPHZWoemGuxeqidu: Hee guomnin zip pu urb as unxxoc ra ur IH cumxuex igv niy jo vees dauj DyjisoFeb naskafh vkvhdtegonev xz eritb EKJYTiezZekorili pa fgotd spom ab iknraq aw idxir, uslaziw ik nezobat.
IM Tuzzaob Esluan: Isosexzpp wulqcopw jufmocmu OX zaxluih-guwajek abvaey ow wuzeq yogem tuuv OM olwf semuhh, nuhuweyiyq i vush-reebanx AR ajjekuazpa dar cte ymejuh.
Ne dtoh siilyuvw i genw nokudqah qgiam, nel xos’f cyoh obab gae foct. Op gvo ziyh mguxmaq, jui’pf benikyt kon yo nwalx xyoco inetem — abj weo’rm vev bu xotu rgiq raxn yzkfenx!
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.