3

In SpriteKit development, how is one supposed to get access to the SKView?

For example; lets say I create a custom SKNode class called Player for use throughout my game:

class Player: SKNode {

  /*...*/

}

In this class, if I wanted to generate a SKSpriteNode from an SKShapeNode (in order to improve performance) I need to use the SKView.texture() method. But I don't have access to the SKView that my Player is residing in unless I pass the view to the Player from whoever is instantiating it.

Is there any way to get the current view that an SKNode is attached, so that I'm not having to pass references to the view around my application? Is this just an anomaly of the framework? What is the preferred way of dealing with this problem?

I'm using the Swift language.

Update

Ron Myschuk's answer is correct and I've accepted it as it is probably most useful to the community. However my use-case for wanting to access the view from within a node was in order to use the texture method to create an SKSpriteNode from an SKShapeNode.

Here's a description of the method I wanted to gain access to:

Renders a portion of a node’s contents and returns the rendered image as a SpriteKit texture.

https://developer.apple.com/documentation/spritekit/skview/1519994-texture

This method seems useful outside of the view. For example, when initialising/populating SKNode's that have not yet been added to the display tree.

My solution to this specific problem was simply to create an SKView on the fly and use it for the purposes of my needs, allowing it to be garbage collected by the engine.

let view = SKView()
let texture = view.texture(from: someSkShapeNode)
let sprite = SKSprite(texture: texture)

// we don't need view anymore

Hope this helps someone.

shennan
  • 10,798
  • 5
  • 44
  • 79
  • "how is one supposed to get access to the SKView" Through the view controller – El Tomato Feb 22 '18 at 01:10
  • I know this problem/pain of building textures in SK. Here's some of my findings, in needlessly long verbiage: https://stackoverflow.com/questions/40084701/create-use-skview-as-in-a-factory-static-class, https://stackoverflow.com/questions/40130888/skeffectnode-to-an-sktexture – Confused Feb 24 '18 at 13:44

2 Answers2

2

providing that you've added an instance of player to your scene and providing that you are not doing his in the initializer of Player you could use the .scene property

self.scene?.view
Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32
  • But how can I be sure the node is added to a scene? Is there a didMove equivalent for SKNode that I could utilise? – shennan Feb 22 '18 at 00:34
  • scene will return nil if it isn't added to the scene yet, so just do a simple nil check – Ron Myschuk Feb 22 '18 at 00:38
  • Of course; but the code won’t be executing at that point. Unless I can use the scene in the class’ constructor or respond to an event when the instantiated class has been added to a scene, I still won’t be able to utilise this technique. – shennan Feb 22 '18 at 00:42
  • you're conflicting your own description. you said "Is there any way to get the current view that an SKNode is attached" your node is not attached in the class init yet. – Ron Myschuk Feb 22 '18 at 00:52
  • 1
    I guess I was hoping for more of a convention for dealing with the problem of wanting to use (what appears to be) a utility method of the SKView, without having to somehow initiate from a mode further up the display chain. Reading between the lines, it sounds like you’re saying that is not possible. – shennan Feb 22 '18 at 08:33
  • 1
    I've decided to accept your answer as it does answer the main portion of my question and will probably best serve the community. I have updated my question with a solution to my specific use-case. – shennan Feb 22 '18 at 13:17
0

You can build a SKTexture from UIImageor CGImage and you can create images from every complex path/drawing you want (see SKTexture from path for example or Drawing and Creating images).

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • I can’t seem to find an example of creating an SKTexture from a path in either of your links. Can you add the sample code to your answer? – shennan Feb 22 '18 at 11:01
  • There is an example of creating an image, then use the appropriate constructor from SKTexture. – Jean-Baptiste Yunès Feb 22 '18 at 11:06
  • Could you add the sample code to your answer as per SO best practise? – shennan Feb 22 '18 at 11:10
  • As far as I can tell from your links, there is no example of creating an UIImage without having to specify a file or fill it with pixel data. So an example of creating a texture from a vector path is not anywhere in your links... – shennan Feb 22 '18 at 13:12
  • Ok may be this one? http://brianbroom.com/2013/11/26/custom-drawing-in-spritekit.html – Jean-Baptiste Yunès Feb 23 '18 at 10:18