How to check if a SpriteNode touches another SpriteNote? - swift

So what I'm trying to do is a player which is able to jump when he's on the ground but when he's in the air he is not. And to be honest I have no idea how to accomplish this because I'm new to stuff like that.

I would have an instance variable in the player class like var isOnGround: Bool = true. I would then set up a physics body for the player and the ground using SKPhysicsBody. Then check for collisions in the didBegin(...) and didEnd(...) (contact methods) for the player and the ground. Set the isOnGround variable to true in didBegin(...) and set it to false in the didEnd(...) methods.
Here's an example of the different bit masks properties:
let playerCollisionNumber: UInt32 = 0x1 << 0
let groundCollisionNumber: UInt32 = 0x1 << 1
//node's own collision number
player.physicsBody.categoryBitMask = playerCollisionNumber
//makes the player's physics body interact/collide with the ground's physics body
player.physicsBody.collisionBitMask = groundCollisionNumber
//setting this allows you to be able to interject your own code when the player's physics body and the ground's physics body interact (in the didBegin and didEnd methods)
player.physicsBody.contactTestBitMask = groundCollisionNumber
Then do the same for the ground:
groundNode.physicsBody.categoryBitMask = groundCollisionNumber
groundNode.physicsBody.collisionBitMask = playerCollisionNumber
groundNode.physicsBody.categoryBitMask = playerCollisionNumber
You'll set the isOnGround variable in the didBegin() and didEnd() methods like this:
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
//in didBegin()
if contactMask == (playerCollisionNumber | groundCollisionNumber) {
player.isOnGround = true
}
//in didEnd()
if contactMask == (playerCollisionNumber | groundCollisionNumber) {
player.isOnGround = false
}

Related

mutliple nodes in GameScene.sks with same name

I'm trying to make a simple 2D Platformer game. Instead of importing the sprites, I decided to use the ones the SpriteKit engine provides: in GameScene.sks I dragged and dropped a sprite (simple, red square) which I called "wall". I copied and pasted it to have 2 walls the player could jump on. In the code part I wrote the following lines:
var wall = SKSpriteNode()
and then, in the "didMoveToView" function:
wall = self.childNodeWithName("wall") as! SKSpriteNode
wall.physicsBody = SKPhysicsBody(rectangleOfSize: wall.size)
wall.physicsBody?.affectedByGravity = false
wall.physicsBody?.dynamic = true
wall.physicsBody?.allowsRotation = false
wall.physicsBody?.pinned = true
wall.physicsBody?.categoryBitMask = physicsCategories.wall
wall.physicsBody?.contactTestBitMask = physicsCategories.player
since I've created a struct outside the class ("physicsCategories") holding the various categories bit masks, when I check for contacts it works just fine:
//CONTACT
func didBeginContact(contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case physicsCategories.player | physicsCategories.wall:
isGrounded = true
default:
print("Some other contact")
}
}
My problem is that this works only with one of the 2 sprites with the name "wall". Is there a way to reference in code ALL the sprites in the scene which have that same name? Like a tag or something like "self.childrenNodesWithName(...)", which I know doesn't exist unluckly.
Or maybe am I doing something wrong?
Thank you in advance.
I've not my mac with me, this is a screenshot from the web:

Swift/SpriteKit Collision Detection Between Many Objects

So I have two objects that should lose health points at an collision.
func addPlayer(xPos: CGFloat, yPos: CGFloat){
playerNode = SKSpriteNode(imageNamed: "player")
playerNode.physicsBody = SKPhysicsBody(circleOfRadius: width/2)
playerNode.physicsBody!.affectedByGravity = false
playerNode.physicsBody!.categoryBitMask = PhysicsCategory.Player
playerNode.physicsBody!.contactTestBitMask = PhysicsCategory.Wall | PhysicsCategory.Zombie
playerNode.physicsBody!.collisionBitMask = PhysicsCategory.Wall | PhysicsCategory.Zombie
playerNode.name = "Player"
player = Player(node: playerNode, healthPoints: 100, attack: 10)
playerNode.position.x = xPos
playerNode.position.y = yPos
playerNode.size = CGSize(width: width, height: width)
addChild(playerNode)
}
func addZombie(xPos: CGFloat, yPos: CGFloat){
zombieNode = SKSpriteNode(imageNamed: "zombie")
zombieNode.physicsBody = SKPhysicsBody(circleOfRadius: width/2)
zombieNode.physicsBody!.affectedByGravity = false
zombieNode.physicsBody!.categoryBitMask = PhysicsCategory.Zombie
zombieNode.physicsBody!.contactTestBitMask = PhysicsCategory.Zombie | PhysicsCategory.Player | PhysicsCategory.Wall
zombieNode.physicsBody!.collisionBitMask = PhysicsCategory.Zombie | PhysicsCategory.Player | PhysicsCategory.Wall
zombieNode.name = "Zombie"
zombie = Zombie(node: zombieNode, healthPoints: 50, attack: 5)
Zombies.append(zombie!)
zombieNode.position.x = xPos
zombieNode.position.y = yPos
zombieNode.size = CGSize(width: width, height: width)
addChild(zombieNode)
}
When a collision appears this function get activated:
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA.node as! SKSpriteNode
let secondBody = contact.bodyB.node as! SKSpriteNode
if(firstBody.name == "Player" && secondBody.name == "Zombie"){
changeHealthPointsForZombieWithNode(secondBody, points: player!.attack)
} else if(firstBody.name == "Zombie" && secondBody.name == "Player"){
changeHealthPointsForPlayer(secondBody, points: zombie!.attack)
print(player!.healthPoints)
}
}
func changeHealthPointsForZombieWithNode(node: SKSpriteNode, points: Int) {
for zombie in Zombies {
if zombie.node == node {
zombie.healthPoints -= points
print(zombie.healthPoints)
if(zombie.healthPoints <= 0){
zombieNode.removeFromParent()
}
return
}
}
}
func changeHealthPointsForPlayer(node: SKSpriteNode, points: Int) {
player!.healthPoints -= points
if(player!.healthPoints <= 0){
playerNode.removeFromParent()
gameOver = true
}
}
I want to subtract the health points of the zombie depending on the attack of the player and other way around. When the player hits the zombie the zombie should lose life points. When the zombie hits the player the player should lose life points. Every player/zombie got health points and an attack value. The Problem is that some zombies are killable and lose health and other (normally 1-3) are not able to lose health. These zombies who aren't able to lose health are the only one able to kill the player. Zombies that lose health can't deal damage(why?)? So there only able to do one thing(attack or lose health) although they should be able to do two things(attack and lose health).
Keep in mind that the two physics bodies described in the contact parameter are not passed in a guaranteed order. Inside the delegate function there is not such a thing as "A is the one colliding with B, not the other way around". There is only "A and B are colliding". That said, you have two options at least, depending on your game mechanics:
Make player AND zombie deal damage to each other every time they collide.
Make for player and zombie a subclass of SKSpriteNode with a property isAttacking, a boolean to determine when the entity should deal damage when detecting a contact with it. For the player this boolean might get "activated" every button/tap press. For the zombie, every once in a while, if close enough to the player.
Additionally, the way you implemented you will be getting doubled contact notifications. SpriteKit will detect that the player is in contact with the zombie, by the player contact mask and zombie category mask, and that the zombie is in contact with the player, by the zombie contact mask and the player category mask. You usually don't want this to happen. You should make only one of them detect it. I would suggest the player. This way there is no need to set the zombie contact mask. As long as you set zombie in the player's contact mask and the zombie's category mask, you will already have the detection (and once).

Swift: skaction not executed after contact between nodes is made?

Alright, so I have contact detection set up between 2 nodes - savior and chicken1. This is set up here:
//This is within GameScene class
var screenTouches = Bool()
enum ColliderType:UInt32 {
case Savior = 1
case Chicken1 = 2
}
savior.physicsBody?.categoryBitMask = ColliderType.Savior.toRaw()
savior.physicsBody?.contactTestBitMask = ColliderType.Chicken1.toRaw()
savior.physicsBody?.collisionBitMask = ColliderType.Chicken1.toRaw()
chicken1.physicsBody?.categoryBitMask = ColliderType.Chicken1.toRaw()
chicken1.physicsBody?.contactTestBitMask = ColliderType.Savior.toRaw()
chicken1.physicsBody?.collisionBitMask = ColliderType.Savior.toRaw()
//This is outside of Gamescene class
//Collision detection
func didBeginContact(contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == ColliderType.Savior.toRaw() && contact.bodyB.categoryBitMask == ColliderType.Chicken1.toRaw() ) {
chicken1.hidden = true
let chickenGrabbedLeft = SKAction.moveTo(CGPointMake(self.size.width * 0.1,self.size.height * 1.2), duration:0)
chicken1.runAction(chickenGrabbedLeft)
println("contact made")
} else if (contact.bodyA.categoryBitMask == ColliderType.Chicken1.toRaw() && contact.bodyB.categoryBitMask == ColliderType.Savior.toRaw()) {
chicken1.hidden = true
let chickenGrabbedLeft = SKAction.moveTo(CGPointMake(self.size.width * 0.1,self.size.height * 1.2), duration:0)
chicken1.runAction(chickenGrabbedLeft)
println("contact made")
}
}
When savior comes in contact with chicken1, I need it to look like chicken1 has disappeared. As it is, I have it so that chicken1 becomes hidden when it touches savior, but this isn't enough because savior still collides with it and the user can tell that the object is still there even if it isn't visible.
I don't want to delete chicken1 because I still need it to be present within the game. So I am now trying to get chicken1 to move back to its starting position (which is offscreen) when it touches savior. I did this by placing the SKAction in the above function.
It is not working. When savior touches chicken1, chicken1 still just gets hidden. It doesn't move. What should I do?
From your question, I assume that your function still runs even when chicken1 becomes hidden.
What you can do is to use a BOOL that changes to true when savior comes into contact with chicken1 and use that as a condition before running your action and change it back to false when you want it to no longer interact with those objects.

Swift: Contact detection between 2 nodes?

Alright, so I've been following various other SO links and trying to figure this out- I need to have contact detection between 2 nodes. Not collision detection, which I learned results in the nodes bouncing each other around. I don't want them to knock each other around, I just want to know when they touch.
Right now I have physics bodies for the 2 nodes, savior and chicken1 as well as the ground (ground) upon which savior sits. These are all set up here:
savior.physicsBody?.dynamic = true
savior.physicsBody?.allowsRotation = false
savior.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(savior.size.width, savior.size.height))
chicken1.physicsBody?.dynamic = true
chicken1.physicsBody?.allowsRotation = false
chicken1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(chicken1.size.width, chicken1.size.height))
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height*2))
ground.physicsBody?.dynamic = false
I need to set up contact detection between savior and chicken1. There seem to be various ways to do this, but this is what I put together:
//Contact detection
self.physicsWorld.contactDelegate = self
savior.physicsBody?.categoryBitMask = saviorCategory
savior.physicsBody?.contactTestBitMask = animalCategory
savior.physicsBody?.collisionBitMask = 0
chicken1.physicsBody?.categoryBitMask = animalCategory
chicken1.physicsBody?.contactTestBitMask = saviorCategory
chicken1.physicsBody?.collisionBitMask = 0
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody
var secondBody : SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask
{
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == 0 && secondBody.categoryBitMask == 1 {
println("they made contact")
}
}
This code results in savior falling right through ground and going right through chicken1, with no contact detection because even when savior and chicken1 touch, nothing happens.
I need savior and ground to continue to collide, but I don't want savior and chicken1 to collide, just touch.
The program needs to execute something when they touch.
It's a mess but how can I fix this?
EDIT:
Here is what I have, animalCategory has been changed to chickenCategory for clarity and no contact is detected. Also savior still falls through ground.
self.physicsWorld.contactDelegate = self
var screenTouches = Bool()
let saviorCategory: UInt32 = 0x1 << 0
let chickenCategory: UInt32 = 0x1 << 1
savior.physicsBody?.categoryBitMask = saviorCategory
savior.physicsBody?.contactTestBitMask = chickenCategory
savior.physicsBody?.collisionBitMask = chickenCategory
chicken1.physicsBody?.categoryBitMask = chickenCategory
chicken1.physicsBody?.contactTestBitMask = saviorCategory
chicken1.physicsBody?.collisionBitMask = saviorCategory
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody
var secondBody : SKPhysicsBody
if contact.bodyA.categoryBitMask == chickenCategory && contact.bodyB.categoryBitMask == saviorCategory {
println("contact made")
savior.hidden = true
}
else if contact.bodyA.categoryBitMask == saviorCategory && contact.bodyB.categoryBitMask == chickenCategory {
println("contact made")
savior.hidden = true
}
}
Ok so it seems like all you want is for just detection that they touched, but you don't want them to push each other. I would in the didBeginContact where ever you put your collision detection between them , make their physicsbody = nil, so that when they collide it knows that they collide and when you put this it makes them go through each other. And if you want to put code to do something else just put that code before you make the physicsbody nil. Also if you want to put back their physicsbody, just put it back.
For anyone who is still stuck on this, if you want to detect that two sprites are touching each other without them actually having a physical effect on each other (i.e. causing movement), then the key is to set the collisionBitMask property of the physicsBody to 0:
node.physicsBody!.collisionBitMask = 0
This means that you will receive events in didBeginContact but the interacting objects will not cause an actual physical effect on each other.
This is how to do it:
Set up your category bit masks:
let saviorCategory: UInt32 = 0x1 << 0
let chickenCategory: UInt32 = 0x1 << 1
let groundCategory: UInt32 = 0x1 << 2
Set up the physics bodies:
func didMove(to: View) {
savior.physicsBody?.categoryBitMask = saviorCategory
savior.physicsBody?.contactTestBitMask = chickenCategory
savior.physicsBody?.collisionBitMask = groundCategory
chicken1.physicsBody?.categoryBitMask = animalCategory
chicken1.physicsBody?.contactTestBitMask = saviorCategory
chicken1.physicsBody?.collisionBitMask = groundCategory
ground.physicsBody?.categoryBitMask = groundCategory
ground.physicsBody?.contactTestBitMask = 0 // No contact detection for ground
ground.physicsBody?.collisionBitMask = UInt32.Max // Everything collides with the ground
physicsWorld.contactDelegate = self
// Rest of didMoveToView
}
Note: It isn't actually necessary to define chicken as contacting saviour and saviour as contacting chicken; you only need to define that one contacts the other, but it can make the code more readable to say that each contacts the other.
Implement didBegin:
func didBeginContact(contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case chickenCategory | saviourCategory:
print("Collision between chicken and saviour")
let saviour = contact.bodyA.categoryBitMask == saviourCategory ? contact.bodyA.node! : contact.bodyB.node!
saviour.hidden = true
default :
//Some other contact has occurred
print("Some other contact")
}
}
Don't forget to set your class as an SKPhysicsContactDelegate
Check out the examples here:
Attack button in SpriteKit

Swift SpriteKit Physics Collision Issue

I am making a simple physics based game. Everything is working normally with exception to collision detection. It feels like the didBeginContact method is being ignored.
I have tried several ways of configuring the "PhysicsCategory" struct (even using enum) and several formations of the bodyA/bodyB contact statements.
I am all out of ideas. I can get the 2 objects to collide but they just bounce off each other. There are no errors and nothing logged to the console. I hope that I have made a trivial mistake that I am overlooking.
Below is all the pertinent code. In case it matters... setupPhysics() is being called in didMoveToView
PhysicsCategory Struct
struct PhysicsCategory {
static let None: UInt32 = 0
static let Fish: UInt32 = 0b1
static let Bird: UInt32 = 0b10
static let BottomEdge: UInt32 = 0b100}
Physics Setup Method
//MARK: - Physics Methods
func setupPhysics() {
/* Physics World */
physicsWorld.gravity = CGVectorMake(0, -9.8)
physicsWorld.contactDelegate = self
physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
/* Bottom Collision Rect */
let bEdge = CGRect(x: CGPointZero.x, y: CGPointZero.y, width: size.width, height: size.height * 0.005)
let bottomEdge = SKShapeNode(rect: bEdge)
bottomEdge.physicsBody = SKPhysicsBody(edgeLoopFromRect: bEdge)
bottomEdge.physicsBody!.categoryBitMask = PhysicsCategory.BottomEdge
bottomEdge.physicsBody!.collisionBitMask = PhysicsCategory.Fish
bottomEdge.physicsBody!.dynamic = false
gameLayer.addChild(bottomEdge)
/* Fish */
fish.physicsBody = SKPhysicsBody(circleOfRadius: blowfish.size.height / 2.1)
fish.physicsBody!.allowsRotation = false
fish.physicsBody!.categoryBitMask = PhysicsCategory.Fish
fish.physicsBody!.collisionBitMask = PhysicsCategory.Bird | PhysicsCategory.BottomEdge
/* Left Random Bird */
randomLeftBird.physicsBody = SKPhysicsBody(rectangleOfSize: randomLeftBird.size)
randomLeftBird.physicsBody!.affectedByGravity = false
randomLeftBird.physicsBody!.allowsRotation = false
randomLeftBird.physicsBody!.categoryBitMask = PhysicsCategory.Bird
randomLeftBird.physicsBody!.collisionBitMask = PhysicsCategory.Fish
/* Random Right Bird */
randomRightBird.physicsBody = SKPhysicsBody(rectangleOfSize: randomRightBird.size)
randomRightBird.physicsBody!.affectedByGravity = false
randomRightBird.physicsBody!.allowsRotation = false
randomRightBird.physicsBody!.categoryBitMask = PhysicsCategory.Bird
randomRightBird.physicsBody!.collisionBitMask = PhysicsCategory.Fish
}
didBeginContact Setup
func didBeginContact(contact: SKPhysicsContact!) {
let collision: UInt32 = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
if collision == PhysicsCategory.Fish | PhysicsCategory.Bird {
println("COLLISON WITH BIRD!")
updateLives()
} else if collision == PhysicsCategory.Fish | PhysicsCategory.BottomEdge {
println("EPIC FAIL!")
}
}
I don't see any use of contactTestBitMask in your code. That one controls whether you get contact delegate messages — collisionBitMask just controls whether they collide (bounce off).
These are separate so that you can get contact delegate messages even for categories that don't bounce off each other, but it means you also can have collisions that don't send messages. (That can be a good thing if you don't want game logic for every kind of collision.) Any that you do want contact messages for you need to explicitly request.