Moataz Hossam
Moataz Hossam

Reputation: 413

SKCropNode Get Cropped Part As SKSpriteNode

I applied a mask to SKCropNode And I see what i need now but the cropped node has the size of the full image i just want to access the cropped part not the full image can i get that into SKSpriteNode??

Here is my code

SKSpriteNode *pic = [SKSpriteNode spriteNodeWithImageNamed:@"test.png"];
pic.name = @"PictureNode";
SKSpriteNode *mask = [SKSpriteNode spriteNodeWithImageNamed:@"2.png"];
mask.size=CGSizeMake(50, 50);
mask.position=CGPointMake(0, 50);
SKCropNode *cropNode = [SKCropNode node];
cropNode.position=CGPointMake(160, 70);
[cropNode addChild:pic];
[cropNode setMaskNode:mask];
[self addChild:cropNode];

and here are the images and result :

mask Image

image

result

Thanks in advance

Upvotes: 1

Views: 2823

Answers (1)

Neslisah
Neslisah

Reputation: 46

As far as I understood, you are trying to get cropped part as a SKSpriteNode, if it is the case, you can manage it as follows:

    SKTexture *oldWholeTexture = [self.scene.view textureFromNode:cropNode];
    SKTexture *newTexture = [SKTexture textureWithRect:CGRectMake(140.f/364.f,56.f/365.f,80.f/364.f,125.f/365.f) inTexture: oldWholeTexture];
    SKSpriteNode *newNode = [SKSpriteNode spriteNodeWithTexture:newTexture];

CGRect parameter of textureWithRect takes unit values, so I normalized my values according my texture size. Bottom left is (0,0).

Upvotes: 2

Related Questions