1
votes

A little bit stuck on how to paint/draw an effect like an alpha Chanel onto an SKSpriteNode node i've started off with setting up the two images I need (ps if there is another way to do this is sprite-kit id love to know how to paint the masks

1)The hidden picture - SKSpriteNode *hiddenimageNode
2)The overlay that gets scratched away SKSpriteNode *myOverlay

3)And finally a mask node comprising of

UIImage *image;
SKTexture *maskTexture= [SKTexture textureWithImage:image];
maskNode = [SKSpriteNode spriteNodeWithTexture:maskTexture];

all of these are placed inside of a node "cropNode" [SKCropNode node]; this works more like a static image (that of a circle moving at touch location and not quite what I'm after, I'm hoping to be able to scratch away the entire image)

this works fine but its Not quite the look I'm after

Pictures: Dragging finger from pos1 to pos02, while "erasing purple layer to reveal a smileyface"

Not quite the look i'm after, dragging finger from pos1 to pos02

enter image description here

is there a way to make it look like I'm erasing the sprite? nubie coder

//Updating project... So since then I have tried using this code https://github.com/oyiptong/CGScratch

and have added it to my SkScene by creating a subview then placing the UIView (Scratchview into it)the erasing is working however the erasing is not happening where the touches are occurring, any ideas why this might be happening?

enter image description here

1
Not sure how to implement it in your case but maybe SKShapeNode can help you.Heyfara

1 Answers

1
votes

If you are doing this in iOS 8, then your best bet is to just use SKSpriteNodes as your masking nodes, there is a weird bug with other kinds of nodes that causes distortion.

If you are doing this with iOS9 +, then SKShapeNodes are fixed.

I am going to explain the concept for iOS 9. To get this to work in iOS 8 is a real pain, since subtraction does not subtract alpha in iOS 8.

Now for your mask nodes, you only have 2 options for drawing, On and Off based on the alpha level of the pixels in your mask image. So what you want to do is incorporate subtraction alpha blending to create your desired effect.

let bigcircle = SKShapeNode(circleOfRadius: 80)
bigcircle = .whiteColor()

let littlecircle = SKShapeNode(circleOfRadius: 40)
littlecircle.position = CGPoint(x: 10, y: 10)
littlecircle.fillColor = .whiteColor()
littlecircle.blendMode = .Subtract
bigcircle.addChild(littlecircle)

maskNode = bigcircle

What this code is doing is making a big white circle with a radius of 80 points, and drawing a white circle inside of it at 40 points. Since we are using subtraction blending, it is going to take the new color and subtract it from the old (in our case white(1,1,1,1) - white(1,1,1,10 = transparent(0,0,0,0)) and get us a nice hole in our mask that will end up being cropped out of the layer over your smiley face.