2015-02-06 84 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 
}