Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift SKSpriteNode with UIImage

I've a problem with creating an SKSpriteNode with an UIImage. I'm creating the assets I use for my App with PaintCode (PaintCode creates a Swift file with drawing methods and image methods which allow me to display the created images).

My problem now is that I want to create a simple SKSpriteNode using my PaintCode images but I don't know how to add them to the SpriteNode.

I tried something like this (line 18):

let Bottom = SKSpriteNode(texture: SKTexture(image: StyleKitName.drawBottom(frame: CGRect(x: 50, y: 50, width: 50, height: 50), isWinter: false)))

But it returns following error:

/Users/myMacName/Documents/App Development/AppName/AppName/GlobalFunctions.swift:18:44: Cannot find an initializer for type 'SKTexture' that accepts an argument list of type '(image: ())'

The StyleKitName.drawBottom function returns an UIImage. It would be nice if anyone could tell me how to create a SKSpriteNode with an UIImage.

I appreciate any help. Thanks.

like image 441
Blackhawk Avatar asked Aug 09 '15 16:08

Blackhawk


2 Answers

If you have a UIImage, you can make a sprite node with it likewise:

let Image = //do your setup here to make a UIImage
let Texture = SKTexture(image: Image)
let Sprite = SKSpriteNode(texture:Texture)

Im not sure where or how you called the methods, but I can assure that these are the correct initializers.

like image 141
MaxKargin Avatar answered Sep 20 '22 18:09

MaxKargin


if you want connect it from Scene Editor

    let movieImage = self.childNode(withName: "MovieImage") as! SKSpriteNode?
    let Image = UIImage(named: "imageName")
    let Texture = SKTexture(image: Image!)
    movieImage?.texture = Texture
like image 27
Ahmed Safadi Avatar answered Sep 20 '22 18:09

Ahmed Safadi