-
-
Save flatpickles/413642637ebd0b00fe2b to your computer and use it in GitHub Desktop.
import SpriteKit | |
extension SKShapeNode { | |
func setTiledFillTexture(imageName: String, tileSize: CGSize) { | |
let targetDimension = max(self.frame.size.width, self.frame.size.height) | |
let targetSize = CGSizeMake(targetDimension, targetDimension) | |
let targetRef = UIImage(named: imageName).CGImage | |
UIGraphicsBeginImageContext(targetSize) | |
let contextRef = UIGraphicsGetCurrentContext() | |
CGContextDrawTiledImage(contextRef, CGRect(origin: CGPointZero, size: tileSize), targetRef) | |
let tiledTexture = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
self.fillTexture = SKTexture(image: tiledTexture) | |
} | |
} |
hi christopherpow, i think i know your problem: to be able to see the image setup with fillTexture you must also set the fillColor to white. The documentation says that it's ignored but it isn't (i filed a radar for this), it seems that the fillColor and the fillTexture are "multiplied" so if you don't set the fillColor you will get alpha zero and don't see the texture.
I share my experiences to help others save some time. 😄
First, try not to get confused with fillTexture and strokeTexture. If you are drawing not a closed shape, like a path, then there is nothing to fill.
Also it looks to me like the texture image must have dimensions of 2^n. If I used different sizes the result is just a black fill.
Thank you @RolandColored 👍
can i cover ball with my texture image using this code?
I am having trouble getting any kind of textures to work with SKShapeNode. Thanks for this extension, but even this doesn't work. I'd like to be able to take a SKTexture that's 100x100 and tile it as a background image, which seems like exactly what this extension should let me do, but it's not working. Documentation on SKShapeNode and its fillTexture member seems lacking...any clues? I know that I'm passing the right imageName because if I don't then it crashes trying to create targetRef.