Search code examples
ios7sprite-kitskshapenode

Fill SKShapeNode with pattern image


I'm trying to fill a SKShapeNode with an Image/pattern but I'm still unsuccessfull.

Can you help me solving this or giving me an alternative? I want to create a collidable custom shape (from any SpriteKit kind) filled with a pattern image.

I've tried the following:

UIBezierPath *path = [[UIBezierPath alloc] init];
    [path addArcWithCenter:CGPointMake(0.0, 0.0) radius:50.0 startAngle:0.0 endAngle:(M_PI*2.0) clockwise:YES];
SKShapeNode *shape = [[SKShapeNode alloc] init];
UIImage *patternImg = [UIImage imageNamed:@"pattern"];
shape.path = path.CGPath;
shape.fillColor = [[SKColor alloc] initWithCGColor:[[UIColor alloc] initWithPatternImage:patternImg].CGColor];

and also:

shape.fillColor = [[SKColor alloc] initWithPatternImage:[[UIImage alloc] initWithCGImage:[UIImage imageNamed:@"Basketball"].CGImage]];

This works (but it isn't what I'm looking for):

shape.fillColor =  [SKColor redColor];

Thank you!


Solution

  • i had the sample problem in my game, finally my solution was to add a SKSpriteNode as a child of the SKShapeNode and it worked fine.

    SKSpriteNode* node = [[SKSpriteNode alloc] initWithImageNamed:@"bombIcon.png"];
    node.name = @"bomb";
    node.size = CGSizeMake(10, 10);
    [self.bombNode addChild:node];
    

    Where self.bombNode is a SKShapeNode.

    Hope it helps