Spritekit and swift 2 - overlapping sprites - sprite-kit

I need to create a road(one simple horizontal line) using SpriteKit, so i have four obstacles to put anywhere position on the road. These obstacles will be in a "shelf" and I have to drag them to the road.
How can i do that using SpriteKit and the new Swift2 ?
PS: I'm beginner on spriteKit.

So you meant to make some SKSpriteNode (road) as you say and put another SKSpriteNode onto that road. (Further I assume that you already have sprite nodes of the road and one obstacle)
define the property of your gameScene:
let roadAndObstaclesLayer: SKNode!
then in didMoveToView you have to add the road SKSpriteNode and obstacle SKSpriteNode to that layer. Your have to make either spawn obstacle method or you have to make position of your obstacles by yourself.
In general it would be like:
roadAndObstaclesLayer = SKNode()
roadAndObstaclesLayer.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetHeight(frame) / 4) // position of your layer
roadAndObstaclesLayer.addChild(road) //child road is added
roadAndObstaclesLayer.zPosition = 0 //zPosition of the layer in the scene
road.zPosition = 0 //zPosition of the child (road) in the layer
roadAndObstaclesLayer.addChild(obstacle) //child obstacle is added
obstacle.zPosition = 1 //zPosition of the child (obstacle) in the layer above child rode
addChild(roadAndObstaclesLayer) //child of the scene is added
As you understand this is an example of single obstacle that is on the road. Your obstacle and road would be placed at the same place but if you want to make some difference of the positions so you have to describe the position of obstacle separately.
As well you can place road and obstacle in different layers or in scene itself separately so their position would be independent according to each other. So in other words it depends on what do you want see at the end.
Hope it helps.

Related

High SpriteKit draw count with SKSpriteNode & texture atlas

I have a SpriteKit game in which there are balls that bounce around. Despite using a texture atlas (a .spriteatlas folder located in my .xcassets folder), I'm getting 2 draw calls for each ball.
Some facts:
The balls are SKSpriteNode's.
Each ball is a parent node that has a child node (an overlay with the opposite rotation of the ball to provide the illusion of lighting/shadow). So, we're talking about 2 nodes for each ball.
The balls' textures are preloaded up front and stored in memory via textureNamed(_:).
I have set view.ignoresSiblingOrder = true.
There is no SpriteKit background image that would make the balls render on top of another node; the background is transparent. So, the balls do not overlap other content.
zPosition is not set -- not on the parent and not on the child.
Based on what I've learned about how the scene is rendered, I would expect to see all the parent nodes rendered in a single pass, with additional passes for the child nodes (though, even that is suspicious since I have ignoresSiblingOrder set to true).
What am I not understanding, here? Why does my draw count increase so drastically as more balls are added to the scene?
Thank you!
UPDATE:
If I don't add the child nodes to the balls, the draw count for the balls is 1 regardless of how many balls there are. So, it's definitely an issue with the child nodes, not the parents.
I solved the problem by setting zPosition to the same value on both the parent and child.
parentNode.zPosition = 1.0
childNode.zPosition = 1.0
There are now a total of 2 draws for the balls regardless of how many balls there are.

Move spritenode in a circle with controls

How would I be able to move a SKSpriteNode along a circle while controlling the movement. For example, there are two buttons, left and right, and each one will make the SKSpriteNode move either clockwise around the circle or counter clockwise.
This is a great example of what I trying to accomplish. Notice how you move 'Trump'.
http://www.trumpsimulator.com/
This is what I have tried so far:
let circularMove = SKAction.followPath(circle.CGPath, asOffset: false, orientToPath: true, duration: 5)
Don't move the sprite node at all.
Place the node onto a parent node. (Call it a layer or something).
Now rotate the layer.
If you place the sprite off centre then it will follow a circle with a radius of the distance of the sprite from the centre of the layer.
Now you don't need to worry about paths or anything. Just update the rotation angle each update.

Fix joint with animations in SKScene

I have two Sprites nodes in an SKScene. Both of them where declare as physics objects inside the SKScene editor. There are not Dynamic, they are not allow rotation, nor pinned or affected by gravity.
One of the objects is move with a method moveBy until it is close to the second object. When that happens, I create a fix joint like this:
let towChopiJointFix = SKPhysicsJointFixed.joint(
withBodyA: chopi.physicsBody!,
bodyB: towTruck.physicsBody!,
anchor: CGPoint(x: 300.0, y: 300.0 ) )
scene!.physicsWorld.add(towChopiJointFix)
After the fix joint is created, I move the chopi sprite using moveBy to other position. The problem is that the second sprite (towTruck) is not moving with it. I thought that if you create a fix joint, both bodies will move together even if you move just one.
Any help?

Is there a way to set offset between SKSpriteNode and it's physicsBody?

I am creating a game where user controls a cube (50x50px) and can collect more cubes while exploring and collected cubes added to a random side of the users current cubes.
User starting cube
http://s7.postimg.org/id95wms7r/cube.png
After user collected some more cubes
http://s13.postimg.org/oo9v4c9bb/morecubes.png
As you can already see from the second image, my problem is when I generate the body from texture, it has some offset with the original picture. Cube adding is random so you can get really weird shapes and those offsets are always different. My question is, is there a way to align that body with the parent sprite? Also I don't want to move the sprite because sprite is in the right place, I actually want to move the body.
This is how my code looks like. Both player and cubes are at CGPointZero in their own coordinate systems.
Player: SKNode
-- Cubes: SKSpriteNode
physicsBody = SKPhysicsBody(texture: cubes.texture, size: accumulatedSize)
physicsBody?.dynamic = true
physicsBody?.mass = 2.0
physicsBody?.allowsRotation = false
Instead of creating the SKPhysicsBody from the texture, create an SKPhysicsBody for each individual cube, then put all those individual bodies into an array, and then create the SKPhysicsBody from the bodies in that array using:
(SKPhysicsBody *)bodyWithBodies:(NSArray *)bodies
That will get you a more accurate body with the sprite.
"The shapes of the physics bodies passed into this method are used to create a new physics body whose covered area is the union of the areas of its children" (from the SKPhysicsBody Class Reference).

SpriteKit Physics Bodies Collisions in Multiple Layers

Okay, so I'm writing code for a space blaster game, and all seems to be going well, minus these crucial issues.
First, I added a spaceship (with PhysicsBody) as a child of self (GameScene).
Then I added a background layer as a node also as a child of self, behind the spaceship, and simulated movement by moving the background around behind the spaceship via joystick control.
I added a node with a boundary physics Edge Loop body so the background wouldn't move beyond the ship, and added that node as a child of the bgLayer.
I have objects for the spaceship to shoot at, but I want them to move around as the background does, so I added them as children of the bgLayer.
Of course, I needed a laser when the spaceship fires, so I added this as well as a child of the bgLayer.
_spaceship.physicsBody = (custom physicsBody code);
[self addChild:_spaceship];
_bgLayer = [SKNode node];
[self addChild:_bgLayer];
_bounds = [SKNode node];
_bounds.physicsBody = (physicsBody edgeLoop code);
[_bgLayer addChild:_bounds];
_otherObjects.physicsBody = (custom physicsBody code);
[_bgLayer addChild:_otherObjects];
_laser.physicsBody = (custom physicsBody code);
[_bgLayer addChild:_laser];
All is well, the background doesn't move beyond the spaceship,the other objects move as the background moves, and the laser fires when called upon.
My first big dilemma is when my laser fires, it does not move as the background moves, but the background moves behind it. I could probably make do but it looks funny if the laser always moves in parallel with the ship. This seems odd to me since I added the laser as a child of _bgLayer.
Second, my laser's physicsBody doesn't seem to recognize the EdgeLoop body, and sails right on through it. Also, my spaceship's physicsBody seems to recognize the EdgeLoop body, but it does not recognize the other objects that are children of _bgLayer.
Do Physics Bodies that are not children of the same layer recognize each other?
And why doesn't my laser behave similarly to other children of the same layer?
Moving the world by changing its position will affect
Children with physics bodies
Children without physics bodies
Moving the world by applying a force/impulse or by settings its velocity will affect
Children without physics bodies
Instead of moving the world by setting its velocity, you can add a camera (an SKNode with a physics body) to the world, move the camera by setting its velocity, and then update the position of the world to center the camera. You can center the camera in the didFinishUpdate method.
-(void)fireLaser
{
_laser = [SKSpriteNode spriteNodeWithImageNamed: [NSString stringWithFormat:#"blueLaser"]];;
_laser.zRotation = _spaceShip.zRotation;
CGVector rotationVector = RadiansToVector(_spaceShip.zRotation);
_laser.position = (Custom code for positioning the laser just in front of the spaceship);
_laser.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_laser.size];
[_bgLayer addChild:_laser];
_laser.physicsBody.velocity = CGVectorMake(rotationVector.dx * LASER_SPEED, rotationVector.dy * LASER_SPEED);
}
[self fireLaser] is called in touchesBegan when a particular SpriteNode is touched.
The laser fires beautifully, but does not scroll with the background, but rather moves in relation to the spaceship. Background scrolls with a PhysicsBody and a setVelocity method is called when the joystick moves, thus simulating spaceship motion, when in reality the spaceship never leaves the center of the screen. Physics categories prevent the background physics body from colliding with anything. LASER_SPEED is simply a static const float.