Reputation: 133
Trying to create a tiled background image for a top down game. The SKScene is 8000,8000 and instead of creating a couple very large sprites I am trying to tile it to improve performance.
var coverageSize = CGSizeMake(8000,8000);
var textureSize = CGRectMake(0, 0, 100, 100);
var backgroundCGImage = [UIImage imageNamed:@"bg"].CGImage; //this line returns several errors.
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;
backgroundTiles.position = CGPointMake(0,0);
[self addChild:backgroundTiles];
Upvotes: 0
Views: 270
Reputation: 16847
one way to do tiles is you create a node used to draw your tiles
You draw the amount of tiles that fits the screen into this special tile
use:
var texture = self.view!.textureFromNode(drawingTile, crop: CGRectMake(-column.width, -row.height, self.frame.width + column.width , self.frame.height + row.height));
To convert this node to a texture now remove all nodes from the drawing node
add in the a new node to the drawing node using this texture.
we now have our background in one node.
when you need to scroll, shift the drawing node, then you draw the 1 row and 1 columns worth of tiles that are missing. Do this only when you need your drawing mode has shifted so much that you have a gap on the screen to fill in. Use the above code again to create a new drawing mode texture. Rinse, repeat
Upvotes: 1