0

The reason for the question is -- is it better to composite a texture at runtime (and make a sprite on that), or just use multiple sprites?

Example: Say you have a small image source for a repeating pattern, where you need many sprites to fill the view (as in a background).

chrisp
  • 2,181
  • 4
  • 27
  • 35

1 Answers1

3

A SKTexture is just image data. A SKSpriteNode is a display object.

The quick answer is no, you cannot draw a SKTexture to the screen without SKSpriteNode.

This answer goes over that limitation : How do you set a texture to tile in Sprite Kit

However, I wanted to answer to give you an option to achieve your ultimate goal.

What you could do is use an SKNode as a container for however many SKSpriteNodes you need to create your background. Then using the SKView method textureFromNode you can create one single SKTexture from that SKNode, that you can use to create a single SKSpriteNode for your background.

Hopefully upcoming version of SpriteKit for iOS 8 has some better tiling options.

Update

Also, in doing some research tonight, since I had a need for this same functionality, I found this :

http://spritekitlessons.wordpress.com/2014/02/07/tile-a-background-image-with-sprite-kit/

Which is doing similar to what I was pondering doing. Gonna copy the code here, in case that page ends up gone :

CGSize coverageSize = CGSizeMake(2000,2000); //the size of the entire image you want tiled        
CGRect textureSize = CGRectMake(0, 0, 100, 100); //the size of the tile.         
CGImageRef backgroundCGImage = [UIImage imageNamed:@"image_to_tile"].CGImage; //change the string to your image name         
UIGraphicsBeginImageContext(CGSizeMake(coverageSize.width, coverageSize.height));         
CGContextRef context = UIGraphicsGetCurrentContext();         
CGContextDrawTiledImage(context, textureSize, backgroundCGImage);         
UIImage *tiledBackground = UIGraphicsGetImageFromCurrentImageContext();         
UIGraphicsEndImageContext();         
SKTexture *backgroundTexture = [SKTexture textureWithCGImage:tiledBackground.CGImage];         
SKSpriteNode *backgroundTiles = [SKSpriteNode spriteNodeWithTexture:backgroundTexture];         
backgroundTiles.yScale = -1; //upon closer inspection, I noticed my source tile was flipped vertically, so this just flipped it back.         
backgroundTiles.position = CGPointMake(0,0);         
[self addChild:backgroundTiles];
Community
  • 1
  • 1
prototypical
  • 6,731
  • 3
  • 24
  • 34
  • Thank you. That is an answer, and I can live with the presented alternative you gave. : (I wasn't expecting the same level of control as in DirectX, et. al. but I thought /maybe/ there was a bit more control available.) – chrisp Jun 25 '14 at 03:29
  • Updated answer with a solution I found. I was just getting ready to dig in with `CGImageRef` to create a solution and figured I'd google first in case someone had the same idea. bingo. – prototypical Jun 25 '14 at 03:50
  • For an impl that does not use so much memory, see http://stackoverflow.com/a/37768928/763355 – MoDJ Jun 11 '16 at 22:41