iOS Swift SpriteKit Achieving a sprite "explosion" effect - ios

In my game I would like that when a collision occurs, the designated sprite would undergo an "explosion" or "glass break" effect, in which the sprite is split up into random pieces which are then moved at a random rate, speed, and angle. I would imagine that something like this may require using particles or at the very a least texture atlas.
I found a little bit on this, but the questions/explantations were catered for Objective-C. I am fairly new to iOS development and have solely used swift, so I can't really translate from one language to another. Thanks.

I would suggest you to try using the SpriteKit Emitter class for this. Add a new SpriteKit Particle Effect file to the project and configure the type of explosion there. You do not need any code, to configure it as Apple has very conveniently provided an editor window for us to easily change the values.
Once you are satisfied with the way the emitter looks, you can then open the Game Scene (assuming that is where this collision would be detected) and type:
let explosionEmitterNode = SKEmitterNode(fileNamed:"the file name")
sprite.addChild(explosionEmitterNode)
Here sprite is the actual node to which you would like to add the emitter effect to. Or you could add it to the scene directly and set its position as:
let explosionEmitterNode = SKEmitterNode(fileNamed:"the file name")
explosionEmitterNode.position = CGPointMake(200,300)
addChild(explosionEmitterNode)

Related

SKEmitterNode: modify properties like velocity and angle for individual particles?

The SKEmitterNode in SpriteKit lets you change particle properties, but it's not clear how you change properties for specific particles.
For instance, if we want particles to radiate in a circle shape, it seems we need to dictate the angle and speed for each particle -- not specify values for particles as a group.
Is this possible?
Put another way, is it possible to use the SKEmitterNode to create animations like the one from this video at the 0:22 mark: https://www.youtube.com/watch?v=wYy2G0lVTAM
you can do:
The image is a little star.
setting:
You have no control over individual particles, you can only determine what they are like when they are born. (think of it like a test-tube baby, you can specify what genes you want the baby to have, but after that, the baby will grow however it feels like)
At some point apple may get SKActions working on the particles so that you can do this kind of stuff, but I wouldn't hold my breath on it working anytime soon, they seem to have no care in the SpriteKit platform, just introducing new broken things to get people excited. ( I am cringing on how buggy ARKit will be)

Swift unity raycast equivalent

I want to check if there is an object below my player in my endless 3D runner, something like ray casting in unity. Is there a equivalent or does someone know how to do it differently maybe?
Checking for collision I tried but since it's a ball, making it have a dynamic physics body makes it go all over the place.
SCNNode exposes -hitTestWithSegmentFromPoint:toPoint:options:.
In the Fox: Building a SceneKit Game with the Xcode Scene Editor sample obstacles and the ground are static physics bodies and -[SCNPhysicsWorld rayTestWithSegmentFromPoint:toPoint:options:] is used to achieve what you're looking for.

Updating geometry in SpriteKit (or SceneKit)

We are porting a game to SpriteKit and I've run in to a bit of a problem. Some objects in our game have triangle-strip trails attached to them. The vertex buffers of the trails are continuously updated as the objects move in the world to create a seamless and flowing effect (there are constraints on how many vertices are in the buffer and how often we emit new vertices).
In the previous implementation we simply updated the affected vertices in the corresponding buffer whenever we emitted new vertices. In SceneKit it seems I am unable to update geometry sources, unless I use geometrySourceWithBuffer:vertexFormat:semantic:vertexCount:dataOffset:dataStride:. To do this however, it seems I need a Metal device, command queue and encoder, to be able to submit commands to update my buffer before it renders.
Is there any way I can do this with a normal SCNView, or do I have to do everything manually with a CAMetalLayer, creating my own metal device etc?
In essence, everything we need, bar this trail geometry is available in SpriteKit, so I was hoping there was some way I could get a hold of the metal device and command queue used by the SKView and simply use that to upload my geometry. This would make things a lot simpler.
I've used a particle system to get a similar effect. The particle emitter is attached to the moving object, with particles set to fade out and eventually die. You might need to set the emitter's targetNode to your scene, depending on the effect you want.
emitter = SKEmitterNode()
emitter.name = marbleNodeNames.trail.rawValue
emitter.particleTexture = SKTexture(imageNamed: "spark")
emitter.particleAlphaSpeed = -1.0
emitter.particleLifetime = 2
emitter.particleScale = 0.2
marbleSprite.addChild(emitter)

Attaching arbitrary information to an SKSpriteNode

I'm making a game with Sprite Kit in which I generate many instances of an SKSpriteNode. When I detect a collision between two sprites, I can easily get a lot of data about the colliding sprites (position, rotation, scale, etc.).
My question: Is there a way to embed arbitrary information, like a string or integer, into the sprite? Say I wanted to timestamp each sprite with the time it was generated, and then display that when it collides with something.
UIButton come to mind, where you can define button.tag, and use that information later.
SKNode has a userData dictionary where you can store data.
Subclass SKSPriteNode and add properties. This will allow you to attach whatever information you would like to your sprite while retaining the built in SKSpriteNode functionality.

Create a level terrain similar to Tiny Wings using SpriteKit

I would like to create a textured terrain in SpriteKit similar to the one in the game Tiny Wings. Using cocos2d, I was able to follow Ray W's tutorial and get the look I wanted...
http://www.raywenderlich.com/32954/how-to-create-a-game-like-tiny-wings-with-cocos2d-2-x-part-1
Is plotting points to make triangles to fill with a repeating texture (or something similar) possible using SpriteKit?
Thanks in advance.
yeah you can create a game like tiny wing with sprite kit
step create serval CGMutablePath using http://dazchong.com/spritekit/ tool its a online SKPhysicsBody Path Generator and save them in a NSMutableArray array suppose you have only one level in your game for multiple level create multiple NSMutableArray array with multiple random saved CGMutablePath items
be sure all CGMutablePath have static physics body don't make it dynamic now
inside your init function add all paths one by one in a order that first path ends start of second path and so on make by this you can make a huge loop of serval path which are connecting with each other.
don't make more paths because too many physics body makes your game slow
now inside update function move all item of your array from right to left be sure
when you reach last item of your NSMutableArray array add it again at the begging of your looping algo.
-(void)update:(CFTimeInterval)currentTime {
}
make a tiny bird and give it a dynamic physics body
set collision and contact category of tiny bird as well as your loop paths
now inside didBeginContact function you can get contact between your tiny bird and path .now you can apply a little Impulse or you can get contact.collisionImpulse between your bird and path apply apply that value to the bird acc to your game
-(void)didBeginContact:(SKPhysicsContact *)contact
{
//contact
CGPoint contactPoint=CGPointMake(0, 0);
}
So it seems what I need to use (or at least I had in mind) are vertex shaders, but those are not currently supported in Spritekit. Right now, only fragment shaders are allowed from what I could find.
I did however see that you can create an SKShapeNode and set its texture property. The only problem with this is there is no option to "tile" that image throughout the SKShapeNode.
I did find this which seems to solve that issue (although I have not tried it out).
https://gist.github.com/man1/413642637ebd0b00fe2b
Seems like quite a work around!
In the end, I decided to use Cocos2d for this project as it supports both vertex and fragment shaders. With a bit of trial and error, I got it to work wonderfully.

Resources