In SpriteKit, SKCropNode has no effect over a SKShapeNode

前端 未结 3 1250
面向向阳花
面向向阳花 2020-12-31 14:15

I\'ve been trying to apply a mask to a SKShapeNode using SKCropNode, and so far without success. Thinking that it\'s a SpriteKit bug - Here is the code snippet:



        
3条回答
  •  囚心锁ツ
    2020-12-31 15:02

    Your title is correct. I've also discovered having a ShapeNode in the CropNode's hierarchy affects the children above it as well. I created a quick experiment. You can create a new game project and try it yourself. By commenting out one of the addChild:shapeContent lines you can see how it affects the cropping of the spaceship image.

    As DoctorClod points out the current solution seems to be making sure all children of the cropNode are SpriteNodes.

    -(void)didMoveToView:(SKView *)view {
    
    SKSpriteNode* colorBackground = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(800, 600)];
    
    SKSpriteNode *spaceshipImage = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
    
    SKShapeNode* shapeContent = [SKShapeNode shapeNodeWithRect:CGRectMake(0, 0, 200, 100)];
    shapeContent.fillColor = [SKColor greenColor];
    
    SKSpriteNode *maskShape  = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(500, 100)];
    
    SKCropNode *cropNode = [SKCropNode new];
    
    [cropNode addChild:colorBackground];
    [cropNode addChild:shapeContent];     // comment this one out
    [cropNode addChild:spaceshipImage];
    [cropNode addChild:shapeContent];     // or comment this one out
    
    [cropNode setMaskNode:maskShape];
    
    cropNode.position = CGPointMake(CGRectGetMidX (self.frame), CGRectGetMidY(self.frame));
    [self addChild:cropNode];
    }
    

提交回复
热议问题