Reputation:
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
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
Reputation: 2353
You should drag your image to the Resources
directory.
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:
Upvotes: 1