Working with texture objects
When a sprite is created, Sprite Kit creates a texture also. But sometime we require texture to do some complex work, such as:
- Changing the sprite
- Animation
- Using the same texture between multiple sprites
- Rendering a node tree into a texture like a screen shot
To make this simple, Sprite Kit provides us the SKTexture
class. We can make an object of this class and use it as we want.
Open your MenuScene.swift
file and make a reference of SKTexture
:
let testingTexture : SKTexture Now initialize it inside init code block init(size:CGSize, playbutton:String, background:String) { PlayButton = SKSpriteNode(imageNamed: playbutton) Background = SKSpriteNode(imageNamed: background) MyPlayButton = SKSpriteNode(imageNamed: playbutton) testingTexture = SKTexture(imageNamed: playbutton) super.init(size:size) }
Let's make a function call, generateTestTexture
, and call it from didMoveToView
:
override func didMoveToView(view: SKView) { addChildToScene...