2014-06-25 116 views

回答

3

A SKTexture只是图像数据。 A SKSpriteNode是一个显示对象。

快速回答是否定的,您不能在没有SKSpriteNode的情况下画一个SKTexture到屏幕。

这个答案越过这一限制:How do you set a texture to tile in Sprite Kit

不过,我想回答,给你一个选项,以实现自己的终极目标。

你可以做的是使用SKNode作为你需要创建背景的许多SKSpriteNode的容器。然后使用SKView方法textureFromNode,您可以从该SKNode创建一个单一的SKTexture,您可以使用该单一的SKSpriteNode为您的背景创建。

希望即将推出的SpriteKit for iOS 8版本有一些更好的平铺选项。

更新

而且,在做一些研究,今晚,因为我有需要此相同的功能,我发现这一点:

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

这是做类似于我琢磨着做。要在这里复制代码,以防止页面结束:

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]; 
+0

谢谢。这是一个答案,我可以接受你提供的替代方案。 :(我并没有期待与DirectX等同样的控制水平,但是我认为/可能会有更多的控制可用。) – chrisp

+0

我找到了一个解决方案的更新答案。我只是准备好用CGImageRef来创建一个解决方案,并且认为如果有人有同样的想法,我会先谷歌。答对了。 – prototypical

+0

非常感谢您的更新。 – chrisp

相关问题