How to mask sprites in cocos2d? - ios

I've read Ray Wenderlich's tutorial on sprite masking: http://www.raywenderlich.com/4428/how-to-mask-a-sprite-with-cocos2d-2-0. However, my understanding of openGL is poor, and this tutorial doesn't produce the effect I desire. What I would like to do is to mask one sprite/layer with another, and move the sprite behind the mask to create a "shiny" animation, like so:
Additionally, how could I make the mask dynamic (ex: changing the text string), and how could I make a sprite or layer count its children as part of the mask?

I think you con do that with an animation, put the "reflection sprite" under the "mask sprite" and then move the "reflection sprite" from left to right.

I think this Github repository will help you.
https://github.com/tonybeltramelli/Cocos2D-Mask-Shader

Related

Fit borders to "real" image, cut of white borders SpriteKit objective c

I am learning Spritekit right now and I want to detect a collision between two images.
Just a fun picture as an example :
The image is still a rectangle. How can I fit it that this rectangle will fit to the original face? I don't want the collision early when it hits the rectangle of the Image, I want it to collide when it actually hits the black lines of the face.
I hope you can understand my problem.
Thanks for any help.
EDIT:
There are two issues here:
You probably want to set up your SKPhysicsBody to be a path around your SKSpriteNode. For example, something like the following is close:
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-40, -22, 80, 39)];
node.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path.CGPath];
That's probably not quite right, so feel free to tweak those values, but hopefully it illustrates the idea: Come up with a path that defines your boundary, and then set the physicsBody accordingly.
If you want to see the physics outlines, set showsPhysics on the SKView.
You probably want to mask your image so that the corners of the image are transparent (i.e. have an alpha of zero):
So, when that's on a colored background it looks like:
By masking the corners, you don't have to worry about the white corner covering up something else on the scene.
If its an image thats got white borders all around it, u can edit it in Preview and use Alpha to get rid of all of the white around it.
I think in that case, the sprite's collision method will collide with the black lines.

Animate color of stroke's ends using CGPath

Quick diagrams! I'm trying to implement this:
I have this working almost 100% currently (though this is a slightly different stage of the animation):
Everything looks good minus the fade effects at the end of the stroke. Is that possible using a simple CGPath? I'm animating strokeStart and strokeEnd to get the current effect. I've tried using CAGradientLayer as a mask on the layer, but that adds a gradient over the entire layer, not just the ends. Overriding drawRect isn't possible since I'm doing this dynamically with animations.
Any thoughts about how to achieve this effect? I have no idea which direction to go.
Depending on how important opacity is and how complex the rest of the animation is, one option would be to make two blurred tail objects that follow the ends of the path as it is animating.
Roundabout solution, sorry I can't think of another versatile way!

How to dissolve nodes in SpriteKit?

I have 2 SKSpriteNode objects, and I want to crossfade them.
One of the easiest way is to create a fadeOut SKAction and a fadeIn SKAction, and apply them to the SKSpriteNode objects. But the problem of this approach is that during the action both of them have the alpha under 1.0, which looks not so good.
For example, I'd like to dissolve the red square SKNode to a green round SKNode
If just fade out the red square and fade in the green round during the action it'll look like this
So you can see background through these 2 objects. I hope to dissolve these 2 objects like this:
In UIKit I can use UIView.transitionWithView but in SpriteKit I only found a similar method for presenting scenes (SKViewObject.presentScene: transition:). So is there anyway to make a dissolve transition for SKNodes?
There's little you can about the background coming through when adjusting the alpha settings on both nodes. A possible hack could be to temporarily insert a solid node in front of your background but behind both other nodes until the action is done. At which point you remove it.
The node should be in the shape of both nodes cross-section (pardon the sloppy artwork on my part):
I searched a lot for this question and I nearly decided to use a shader to do this kind of dissolve (since you can directly edit the pixel in a shader), but then I found that there's an unusual way to solve this problem. It may not be useful for every situation, but if your background doesn't do things like scroll or zoom, this approach may be the easiest. In simple words, just create a screenshot for the current screen and display it at the top, then change your node to the sprite you need, and at last fade out the screenshot you took.
So at first you have to make sure all the nodes are in the correct node tree. Then use SKViewObject.textureFromNode(rootNode) to create the screenshot, make a sprite node from this texture, and add it to your screen. Then, you can create the fade out SKAction to fade out this screenshot sprite. You may also remove it when the action ends.
Using this approach, during the fade out the screen will just look like this:

iOS Circular Slider

I want to create a circular slider like below.
But i want two functionalities in addition.
1) I want to start the slider from any point,but in fig. it starts from 0.
2) I want to include multiple sliders in a single circular black plot.
I'm sharing the link of this project:
https://www.cocoacontrols.com/controls/circularsliderdemo
Can anyone help me to do these functionalities.
Thanks in advance.
Take a look at CAShapeLayer. You could create a path that is a full circle, and use the strokeStart and strokeEnd properties to only draw part of the circle. You could use core animation to animate between the beginning and the end.
There is an open source custom gesture recognizer on Github that is a one finger gesture recognizer. That would be a good start for detecting and responding to the twirl gesture that such a control would need. EDIT: It's called KTOneFingerRotationGestureRecognizer (link)
Those are some ideas to help get you started.
I have a project on github called iOS-CAAnimation-group-demo That includes a "clock wipe" animation. The clock wipe works by setting up a shape layer as the mask layer for an image view, installing a full-circle arc that's wide enough to completely fill a rectangular area, and then animate the strokeEnd property of the shape layer to reveal/hide the image view. The clock wipe is much more complex than what you need, but it would give you the seed of what you want. You'd use a shape layer with a much thinner line width, and you would use it as a content layer, not as a mask.

ccDraw. Fill screen everywhere except on sprite

So I am trying to get a very basic "flashlight"-style thing going in one of my games.
The way I was getting it to work, was having a layer on top of my game screen, and this layer would draw a black rectangle with ~ 80% opacity, creating the look of darkness on top of my game scene.
ccDrawSolidRect(ccp(0,0), ccp(480,320), ccc4f(0, 0, 0, 0.8));
What I want to do is draw this rectangle EVERYWHERE on the screen, except for around a cone of vision that will represent the "light source".
What this would create would be a dark overlay on top of everything except for the light, giving it the illusion of a torch/light/flashlight.
The only way I can foresee this happening is by using ccDrawSolidPoly(), but since the position of the light source changes, so would the vertices for the poly.
Any suggestions on how to achieve this would be great.
You can use ccDrawSolidPoly() and avoid having to manually update vertices. For this you can create a new subclass of CCNode representing your light object, and do your custom shape drawing in its -(void)draw method.
The ccDraw...() functions will draw relative to the local sprite coordinates, so you can then move and rotate your new sprite to suit your needs and cocos2d will do the vertices transformations for you.
Update: I found out that you might be better off subclassing CCDrawNode instead of CCNode, as it has some facilities for raw OpenGL drawing (OpenGL's vertexArrayBuffer and vertexBufferObject internal variables and a buffer for vertices, their colors and their texCoords). If your stuff is very simple, maybe subclassing the plain CCNode is enough.
Could a png be used instead as a mask, as the layer above
Like that binocular vision you sometimes see in cartoons?
Or a filter similar to a photoshop mask that darkens as it grows outwardly to wards the edge of the screen
Just a thought anyway...
A picture of more of what your trying to explain might be good too

Resources