简体   繁体   中英

SpriteKit SKCropNode Masks everything

I am trying to mask a node with SKCropNode class. I am doing it with my own init method in SKCropNode inherited class:

- (id)init {
    self = [super init];

    if (self) {
        self.maskNode = [[SKSpriteNode alloc] initWithColor:[SKColor blackColor] size:(CGSize){50,50}];
        SKSpriteNode *contentNode = [[SKSpriteNode alloc] initWithImageNamed:@"Spaceship"];
        [self addChild: contentNode];

        self.userInteractionEnabled = YES;
    }
    return self;
}

It works as expected: 矩形面膜

Now, I want to add background to the scene and a cropped spaceship with following code:

-(void)didMoveToView:(SKView *)view {
    /* Setup your scene here */

    //
    //background
    //
    SKSpriteNode * background = [SKSpriteNode spriteNodeWithImageNamed:@"background_game"];
    background.position = CGPointMake(CGRectGetMidX(view.frame), CGRectGetMidY(view.frame) - CGRectGetMinY(view.frame));

    [self addChild:background];

    TurretCropNode * cropNode = [[TurretCropNode alloc] init];
    cropNode.position = (CGPoint){view.frame.size.width/2,view.frame.size.height/2};
    [self addChild:cropNode];
}

I have just added few lines with adding background at the beginning (cropped node lines remain the same)

Here what I got now: 背景+蒙版

Is it an expected behavior? Why cropped spaceship disappeared?

I am guessing that the image as shown below is your designated background. Have you checked your nodes zPosition ?

If you want to show your spaceship infront of your background, you will have to adjust the zPosition to be higher than that of the background sprite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM