2015-02-06 74 views
3

使用SpriteKit,我将如何平铺水平重复自身以填充SKSpriteNode宽度的SKTexture?这是我迄今为止 - 只是延伸纹理。如何平铺Sprite节点的纹理?

var header = SKSpriteNode() 
    let headerTexture = SKTexture(imageNamed: "inGameHeader-1.png") 
    header = SKSpriteNode(texture: headerTexture) 
    header.position = CGPoint(x: 0, y: CGRectGetMaxY(self.frame)-39) 
    header.size.width = CGRectGetWidth(self.frame) 
    header.size.height = 150 
    header.anchorPoint = CGPoint(x: 0,y: 1) 
    addChild(header) 
+0

我认为当前没有简单的方法来平铺纹理,但是您可以在单个绘图阶段生成许多精灵。这给你类似的结果... – Whirlwind 2015-02-06 01:31:40

+0

嗯,我会怎么做呢?我真正需要做的是重复1px宽度的渐变来填充标题背景。我只是觉得这会比使用600px宽度的渐变效果更好,只是重复1px。 – Chris 2015-02-06 05:39:57

+0

正如我所说目前没有简单的方法来做到这一点,但您不必担心,Spritekit有办法优化东西:https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/DesigningGameswithSpriteKit/DesigningGameswithSpriteKit.html#//apple_ref/doc/uid/TP40013043-CH7-SW7 – Whirlwind 2015-02-06 10:53:08

回答

0

基于使用SpriteKit着色器的工作实现可在this answer找到。请注意,您需要导入标题以在Swift中使用它。

0

这是我的解决方案:将图像发送到此函数,其中包含精灵的大小,接着是大小。这将返回平铺的SKTexture。

-(SKTexture *)tiledTextureImage:(UIImage *)image ForSize:(CGSize)size tileSize:(CGSize)tileSize{ 

// First we create a new size based on the longest side of your rect 
int targetDimension = (int)fmax(size.width,size.height); 
CGSize targetSize = CGSizeMake(targetDimension, targetDimension); 

// Create a CGImage from the input image and start a new image context. 
struct CGImage *targetRef = image.CGImage; 
UIGraphicsBeginImageContext(targetSize); 
CGContextRef contextRef = UIGraphicsGetCurrentContext(); 

// Now we simply draw a tiled image 
CGContextDrawTiledImage(contextRef, CGRectMake(0,0, tileSize.width,tileSize.height), targetRef); 
UIImage *tiledTexture = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

// Finally create a texture and return it. 
return [SKTexture textureWithImage:tiledTexture]; 
} 
0

你知道你可以的UIButton或UIImageView的由resizableImageWithCapInsets做到这一点,所以,这里是我的解决方案:

-(SKTexture *)textureWithImage:(UIImage *)image tiledForSize:(CGSize)size withCapInsets:(UIEdgeInsets)capInsets{ 
//get resizable image 
image = [image resizableImageWithCapInsets:capInsets]; 
//redraw image to target size 
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f); 
[image drawInRect:CGRectMake(0, 0, size.width-1, size.height-1)]; 
[[UIColor clearColor] setFill]; 
//get target image 
UIImage *ResultImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
// get texture 
SKTexture * texture = [SKTexture textureWithImage:ResultImage]; 
return texture; 
} 
0

下面是斯威夫特3和4的静态函数没有使用延长为SKTexture类实际上并没有指定的初始化器。

注意这不会很高效。最好在使用前预先加载纹理,或者进入自定义着色器的世界。

static func generateTiledTexture(size: CGSize, imageNamed imageName: String) -> SKTexture? { 
    var texture: SKTexture? 

    UIGraphicsBeginImageContext(size) 
    let context = UIGraphicsGetCurrentContext() 

    if let image = UIImage(named: imageName), let cgImage = image.cgImage { 
     context?.draw(cgImage, in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height), byTiling: true) 

     if let tiledImage = UIGraphicsGetImageFromCurrentImageContext() { 
      texture = SKTexture(image: tiledImage) 
     } 
    } 

    UIGraphicsEndImageContext() 

    return texture 
}