Articles, Code and Video Tutorials for ARKit, Sprite Kit & SceneKit

A Basic Introduction to ARKit with SpriteKit - Part 2

Go beyond just adding anchored emoji with the Xcode Starting Template

In the previous article,  we looked at the two blocks of code responsible for adding an emoji to our augmented reality scene using Xcode’s new AR Starting Project with Sprite Kit.

In this article we’ll talk about how to change what gets added to the ARSKView, dropping the space invader emoji, and using an SKSpriteNode instead.

In your ViewController.swift declare this variable…

Now when we create and present the scene, we’ll save a reference to it, like so…

Now when we add something to the ARSKView we’ll check with theScene for properties we can use to change what gets added. For example…

Don’t try to run this code yet, as theScene doesn’t yet have a property named theImage.

Switch to the Scene.swift file…

Declare a variable named theImage

Now just be sure to add an image to the Assets catalog, named “Target”.

At this point you should be able to run the project and see whatever image you added anytime you tap the screen.

You could also move the anchor creation code into a separate function than the touchesBegan function. Notice below the addAnchorWithName function has a String parameter used to change theImage before adding the anchor…

So if you wanted to randomize the image that got added to the ARSKView you could do this…

Or for a little more a refined approach….