setting background color hides SKEmitterNode - swift

How can I have a white background and use SKEmitterNode in SpriteKit?
The emitter disappears if I set the background color of the SKScene. On the default black background I can see the red particles (when I comment out "backgroundColor = .white" in my SKScene). If I use a CAEmitterLayer then I can see the particles with a white background, but would like to use SKEmitterNode. I've spent a couple hours trying to find any reference to this problem.
Code is very simple -- adds child to SKScene
if let particles = SKEmitterNode(fileNamed: "Sparks.sks") {
particles.position = position
scene.addChild(particles)
}

instead of the default .white, you may want to try setting the color with a color close to white... i.e. a UIColor where the RGB is .9,.9,.9

Related

how to paint/erase SKSpriteNode

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"
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?
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.

SpriteKit Swift make an entire scene darker, then change it back

Is there any way to make an entire scene, including all of its children nodes (not just the background color) darker in color? I am looking for an effect similar to that of this code:
node.color = SKColor.blackColor()
node.colorBlendFactor = 0.25
The above code shades the node a darker color, while keeping the original colors (except that those colors are just darker) and detail.
However, as far as I can tell, this code doesn't work on scenes, it only works on SKSpriteNode. Is there any way to darken a whole scene? The answer could be some sort of filter, a special way of colorizing a scene, or maybe there is just no way. Anything helps!
Thanks
When I need to do something similar to what you are asking, I simply add a SKSpriteNode on top of everything else as follows:
let dimPanel = SKSpriteNode(color: UIColor.blackColor(), size: self.size)
dimPanel.alpha = 0.75
dimPanel.zPosition = 100
dimPanel.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
self.addChild(dimPanel)
Just make sure the panel's zPosition is greater than everything you want to make dimmer.
This way, you can also use the SKAction.colorizeWithColor to change the panel to whatever color you want.
To add the panel and then remove it, you could start the alpha at 0, then do:
dimPanel.runAction(SKAction.sequence([
SKAction.fadeAlphaTo(0.75, duration: 3),
SKAction.fadeOutWithDuration(3)
]))
The answer could be some sort of filter...
Indeed. While #TrentSartain's answer is quite practical, it's not the only way. SKScene inherits from SKEffectNode, so you can apply a Core Image filter (or filter chain) to the entire scene as a way to darken it or add other visual effects.
Once you get into Core Image filters, you have all kinds of options for darkening — you can get a different effect from ramping the parameters to CIExposureAdjust than with CIToneCurve, for example. Or you could combine several filters, so your scene (for example) fades to grayscale and blurs as it darkens.
Note: there are no SKActions for animating Core Image filters, but you can change the parameters of a CIFilter object over time in your update function or a customActionWithDuration:actionBlock: block.

SpriteKit - Ignore transparency when selecting node

I have an SKSpriteNode that has a texture assigned like this:
node.texture = SKTexture(imageNamed: "Oval")
Users can select this object and drag it around. I use the following to identify when it is being selected in the touchesMoved function.
var touchedNode = allObjects.nodeAtPoint(location)
The problem is that almost half the surface area of this image file is transparent. However, nodeAtPoint responds to touches on the transparency.
Does anyone know of a way to ignore the transparency?
In your touches Moved you can get the color of the pixel of the node you touched and then using an if statement you can ignore the node if the color is transparent (has an alpha of 0). Check here to see how to get the color
SpriteKit: How can I get the pixel color from a point in SKSpriteNode?

How to colorize an SKTexture that is created with imageFromFile?

I have created png images of several shapes, such as hexagons and stars, and they are all white color.
I want to colorize these SKSpriteNodes or SKTextures so that I can assign any color rather than creating shape images for each color I need and loading those textures into memory. I need the images to be high resolution, so having several images and loading them into memory is not a good option since I receive memory warnings from XCode. Loading them into memory as they are needed also does not work because they cause my game to lag for about 0.25 seconds.
So, is there any way I can create 1 SKTexture and colorize the image texture of an SKSpriteNode whenever I need it to change colors?
You can change color of white texture like this (assuming that you are using pure white textures):
//Colorize to red
let yourNode = SKSpriteNode(imageNamed: "textureName")
yourNode.colorBlendFactor = 1
yourNode.color = UIColor.redColor()
About colorBlendFactor:
The value must be a number between 0.0 and 1.0, inclusive. The default
value (0.0) indicates the color property is ignored and that the
texture’s values should be used unmodified. For values greater than
0.0, the texture is blended with the color before being drawn to the scene.
More about this topic on StackOverflow.
Hope this helps.

Change spritekit particle color

Is it possible to change the color of a particle after creating in the spritekit editor? I tried setParticleColor but it doesn't appear to do anything. Basically I want to have one particle file and a way to programmatically change the color on the fly...
The particleColor property isn't working because of the color ramp settings in the Particle Editor. These are actually stored in the particleColorSequence, which ignores all other particle color properties.
So, to make it work, you need to override the particleColorSequence setter and make it nil first. Then, you need to set the particleColorBlendFactor to fully blend your chosen color with the particle texture (full blending is 1.0). From then on, any explicit particle color setting should work:
emitter.particleColorSequence = nil;
emitter.particleColorBlendFactor = 1.0;
emitter.particleColor = [SKColor redColor];