user13501445
user13501445

Reputation:

How do I add an image in Playgrounds for iPad?

I’m writing a simple 2D game using SpriteKit with Playgrounds for iPad. I’m trying to add a menu button using the following code:

SKSpriteNode(imageNamed: )

How do I point to an image? I’ve tried adding an image to the project using the little plus in the top right-hand corner, but when I do that I get the error “cannot convert value of type ‘UIImage’ to expected argument type ‘String’”. So, how do I point to an image?

Upvotes: 0

Views: 552

Answers (2)

Alex Zavatone
Alex Zavatone

Reputation: 4323

Select the Assets.xcassets file and click on the + in the lower left of the main window to add an image. Select New Image Set, name your image and drag files into the image slots.

Then this code will work.

let node = SKSpriteNode(imageNamed: "image.png")

But this will also work.

let node = SKSpriteNode(imageNamed: "image")

Good luck.

Upvotes: 0

mikro098
mikro098

Reputation: 2353

You should drag your image to the Resources directory.

enter image description here

Then you should use name of the file which is String. That's why you got this error.

let image = UIImage(named: "image.png")
// or
let node = SKSpriteNode(imageNamed: "image.png")

When you initialize UIImage like that you will see the preview of the image:

enter image description here

Upvotes: 1

Related Questions