Adding a sprite without using textures
Mostly in a game, we add texture to our sprite, but we can also make a sprite without using textures. A texture property is an optional property in the SKSpriteNode
class. If texture is nil, that means we have no texture to stretch, so the contract parameter is ignored. Let's open our GameScene.swift
file and make a variable of SKSpriteNode
, just below the backgroundNode
declaration:
var spriteWithoutTexture : SKSpriteNode?
Now, with the preceding declaration, we have declared spriteWithoutTexture
as optional. Since we have declared it optional, texture need not require a value. Now under didMoveToView
, add following function:
func addSpriteWithoutTexture(){ spriteWithoutTexture = SKSpriteNode(texture: nil, color: UIColor.redColor(), size: CGSizeMake(100, 100)) addChild(spriteWithoutTexture!) }
After that, call this function inside didMoveToView()
, below the addBackGround()
function:
addSpriteWithoutTexture()
Now tap on play and see what...