SpriteKit and swift game - swift

I am kind of new to SpriteKit and I am using Swift language.
Here is my issue..
In the attached image , the BasketBall and Ring is a SkSpriteNode type.
The physicsBody?.dynamic is set to false for the the Ring
The physicsBody?.dynamic is set to true for the the BasketBall.
as expected the ball moves around the screen and also bounces when hits the corner or bottom. The ball also bounces when it hits the Ring , which is good to some extent.
Now I would like to make the ball pass through the ring if it is at a proper location...just like in basket ball game. How to accomplish this ?

Related

sprite kit - partially mask sprite layer

i have a 2-d fruit ninja style iOS (swift 3) game where an animal eats the "enemy" sprite.
currently i can detect a collision between the mouth and the enemy and cause the enemy to disappear with an animation.
however, i would like to implement a more realistic effect where the enemy sprite disappears into the mouth by occluding an increasing portion of the enemy as it moves further into the mouth.
i tried using combinations of masks and layering, but they were hacky and compromise the UX. what would be the most elegant approach to achieving this effect?
current/non-preferred example:
ideal/preferred example:

Side scroller style scene with gravity

I've started learning sprite kit and I think I've got the basics but now I'm struggling with something.
I want to create a game that has a 'Streets of rage' type feel to it whereby the user can move up and down, but isn't jumping, they're still on a 2D plane. But I also want them to be effected by gravity e.g stairs etc. like the following picture.
Am I right in assuming that I should have my background image with colliders around the blue and brown edges, and then create a physicsbody collider located at the feet of my player/players so that it looks like they can move against the background, but when their feet reach the top it would stop?
Could I then place other obstacles like rocks etc on that path that they would be able to collide into, but that could also sit over the path and the sky? How would I handle the fact that these could be constantly colliding depending on the position?
I appreciate there isn't any code here, but I'm trying to understand the concept around this before I jump in coding a solution.
Thanks
I would use a categoryBitMask to separate the different planes of objects.
And I would play with collisionBitMask/contactTestBitMask depending from the plane the player is in, in addition to the z-order.
Thus you can have a rock that collides your player if they are both in the same line else the player would walk behind/front.

How to rotate a SKPhysicsBody in SpriteKit with Swift

I am trying to make a brick breaking game with SpriteKit in Swift. To make the paddle I am trying to make it so that there are two SKPhysicBody, one for each half. To control the angle of which the ball bounces off the paddle, I want to rotate the physics body. I tried to use applyAngularImpulse or just applyForce, but they did not work. Is there a way to rotate the physics body??
You can't set the rotation of a physics body. You can however set the rotation of the node that the physics body is attached to. So what you can do is make a dedicated node for your physics body. This node will be invisible, it will only exist for you to rotate.

Check for absence of contact

I'm developing a puzzle platform game using Sprite Kit (and its physics engine). My player's physics body consists of a large hitbox covering most of the sprite, and a wheel with a pin joint anchored at the bottom of the hitbox. Rotation is added to the wheel to make the player move across the screen.
I need to know if the player is on the ground, or has fallen off a ledge; I know how to check for a physics body contact, but is it possible to check for an absence of contact (i.e. when the wheel leaves the ground)? I can't rely on comparing the Y position from the last frame as there are sloped surfaces that the player climbs.
You can use the didEndContact:(SKPhysicsContact *)contact to check if the wheel is no longer contacting the ground. Same principal as the didBeginContact:(SKPhysicsContact *)contact just in reverse.

Preventing jump more than once

I am writing an iphone game using cocos2d and box2d engines.
I have a ball which can be jumped by tapping the screen. The jump is applied by using ApplyImpulse method on the ball's body.
Thing is I do not want the ball to jump more than once. (i.e. If the ball is in mid-air I do not want it to be jumped again when tapping the screen.)
Is there a way to know when an object is in mid-air ?
Is there a common solution to this problem ?
Thanks
You can raycast from the center of the ball, downwards. If it collides with anything at a distance greater than the ball's radius, that means it is in the air.
To raycast, check chapter 10 in the manual here:
http://box2d.org/manual.pdf
It's in c++ but you should find similar examples in objective-c.
Hope it helps.