你可以在没有SKSprite的情况下绘制纹理吗?

时间:2014-06-25 00:37:43

标签: ios sprite-kit

问题的原因是 - 在运行时合成纹理(并在其上制作精灵)或仅使用多个精灵更好吗?

示例:假设您有一个重复图案的小图像源,您需要许多精灵来填充视图(如在背景中)。

1 个答案:

答案 0 :(得分:3)

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

快速回答是否定的,如果没有SKTexture,您无法在屏幕上绘制SKSpriteNode

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

但是,我想回答给你一个实现最终目标的选择。

您可以使用SKNode作为容器,无论您需要多少SKSpriteNode来创建背景。然后使用SKView方法textureFromNode,您可以从SKTexture创建一个SKNode,您可以使用它为您的背景创建一个SKSpriteNode。< / p>

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

<强>更新

另外,今晚在进行一些研究时,由于我需要同样的功能,我发现了这个:

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];
相关问题