collision detection not working fully - ios

import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let balls = [
SKSpriteNode(imageNamed: "blueball.png"),
SKSpriteNode(imageNamed: "greenball.png"),
SKSpriteNode(imageNamed: "realredball.png"),
]
let redRectangle = SKSpriteNode(imageNamed: "redrectangle.png")
let blueRectangle = SKSpriteNode(imageNamed: "bluerectangle.png")
let greenRectangle = SKSpriteNode(imageNamed: "greenrectangle.png")
let blueBallCategory :UInt32 = 0x1 << 0
let greenBallCategory :UInt32 = 0x1 << 1
let realRedBallCategory :UInt32 = 0x1 << 2
let redRectangleCategory : UInt32 = 0x1 << 3
let blueRectangleCategory : UInt32 = 0x1 << 4
let greenRectangleCategory : UInt32 = 0x1 << 5
override func didMove(to view: SKView) {
spawnBallsandRectangles()
physicsWorld.contactDelegate = self
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
physics()
}
func didBegin(_ 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 == blueBallCategory && secondBody.categoryBitMask == blueRectangleCategory {
print("alive")
}
else if firstBody.categoryBitMask == greenBallCategory && secondBody.categoryBitMask == greenRectangleCategory {
print("alive")
}
else if firstBody.categoryBitMask == realRedBallCategory && secondBody.categoryBitMask == redRectangleCategory {
print("alive")
}
else if firstBody.categoryBitMask == blueBallCategory || firstBody.categoryBitMask == greenBallCategory && secondBody.categoryBitMask == redRectangleCategory{
print("dead")
}
else if firstBody.categoryBitMask == realRedBallCategory || firstBody.categoryBitMask == greenBallCategory && secondBody.categoryBitMask == blueRectangleCategory {
print("dead")
}
else if firstBody.categoryBitMask == realRedBallCategory || firstBody.categoryBitMask == blueBallCategory && secondBody.categoryBitMask == greenBallCategory {
print("dead")
}
}
func spawnBallsandRectangles() {
let ball = balls[Int(arc4random_uniform(UInt32(balls.count)))]
ball.position = CGPoint(x: -200, y: 250)
ball.size = CGSize(width: 70, height: 70)
redRectangle.position = CGPoint(x: 0, y: -600)
redRectangle.size = CGSize(width: 200, height: 20)
blueRectangle.position = CGPoint(x: -200, y: -600)
blueRectangle.size = CGSize(width: 200, height: 20)
greenRectangle.position = CGPoint(x: 200, y: -600)
greenRectangle.size = CGSize(width: 200, height: 20)
self.addChild(ball)
self.addChild(redRectangle)
self.addChild(blueRectangle)
self.addChild(greenRectangle)
}
func physics(){
for ball in balls{
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height/2)
}
redRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
redRectangle.physicsBody?.affectedByGravity = false
redRectangle.physicsBody?.isDynamic = false
blueRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
blueRectangle.physicsBody?.affectedByGravity = false
blueRectangle.physicsBody?.isDynamic = false
greenRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
greenRectangle.physicsBody?.isDynamic = false
greenRectangle.physicsBody?.affectedByGravity = false
balls[0].physicsBody?.categoryBitMask = blueBallCategory
balls[1].physicsBody?.categoryBitMask = greenBallCategory
balls[2].physicsBody?.categoryBitMask = realRedBallCategory
balls[0].physicsBody?.contactTestBitMask = redRectangleCategory
balls[1].physicsBody?.contactTestBitMask = redRectangleCategory
balls[1].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[2].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[0].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[2].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[0].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[1].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[2].physicsBody?.contactTestBitMask = redRectangleCategory
redRectangle.physicsBody?.categoryBitMask = redRectangleCategory
blueRectangle.physicsBody?.categoryBitMask = blueRectangleCategory
greenRectangle.physicsBody?.categoryBitMask = greenRectangleCategory
}
}
I have set up collision detection so that if a ball collides into a rectangle that match colors, the program prints ("alive"). And the program is supposed to print ("dead") if the colors don't match. It detects collision if the colors match and it prints ("alive") but it doesn't print ("dead"). Could someone please help ,thanks.

You might find that structuring your didBegin() like this might help to get rid of all those else ifs:
func didBegin(_ contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case blueBallCategory | blueRectangleCategory:
print("Alive! Blue ball has hit blue rectangle.")
case greenBallCategory | greenRectangleCategory:
print("Alive! Green ball has hit green rectangle.")
case redBallCategory | redRectangleCategory:
print("Alive! Red ball has hit red rectangle.")
default :
print("Dead! Some other contact has occurred")
}
}
What do you want to happen if a ball hits a rectangle of a different colour?
EDIT:
From this code in your physics() function:
balls[0].physicsBody?.contactTestBitMask = redRectangleCategory
balls[1].physicsBody?.contactTestBitMask = redRectangleCategory
balls[1].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[2].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[0].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[2].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[0].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[1].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[2].physicsBody?.contactTestBitMask = redRectangleCategory
each time you assign a new value to the contactTestbitMask for a specific physics body, you are overwriting the previous value, not adding to it. Thus the only lines that matter are:
balls[0].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[1].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[2].physicsBody?.contactTestBitMask = redRectangleCategory
So balls[0] (blue ball) only contacts blue rectangles, balls[1] (green ball) only contacts green rectangles and balls[2] (real red ball) only contacts red rectangles. So didBegin() will not be called if a ball contacts a rectangle of a different colour, which is why you never see your 'dead' message.
Another Edit:
If you want all balls to trigger didBegin() when then contact any rectangle, then your contactTestBitMasks for the balls should include ALL the rectangles:
for ball in balls {
balls.physicsBody?.contactTestBitMask = blueRectangleCategory | greenRectangleCategory | redRectangleCategory
}
or if the balls already have some contacts defined and you just want to add the rectangles to their collision bit masks:
for ball in balls {
balls.physicsBody?.contactTestBitMask |= (blueRectangleCategory | greenRectangleCategory | redRectangleCategory)
}

Related

My code detects collisions with a sprite but doesn't collide? - Swift

Here is my didBeginContact:
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
let ballWasContacted = firstBody.categoryBitMask == PhysicsCat.Ball || secondBody.categoryBitMask == PhysicsCat.Ball
let wallWasContacted = firstBody.categoryBitMask == PhysicsCat.Wall || secondBody.categoryBitMask == PhysicsCat.Wall
let wallCheckWasContacted = firstBody.categoryBitMask == PhysicsCat.wallSpace || secondBody.categoryBitMask == PhysicsCat.wallSpace
let scoreWasContacted = firstBody.categoryBitMask == PhysicsCat.Score || secondBody.categoryBitMask == PhysicsCat.Score
let boundaryWasContacted = firstBody.categoryBitMask == PhysicsCat.Boundaries || secondBody.categoryBitMask == PhysicsCat.Boundaries
if boundaryWasContacted {
print("I collided with the boundary.")
}
Here is my boundary sprite node:
let boundary = SKSpriteNode(color: SKColor.whiteColor(), size: CGSize(width: 50, height: 150))
boundary.name = "boundary"
boundary.position = CGPoint(x: self.frame.width / 1.37, y: self.frame.height / 2)
boundary.physicsBody = SKPhysicsBody(rectangleOfSize: boundary.size)
boundary.physicsBody!.categoryBitMask = PhysicsCat.Boundaries
boundary.physicsBody?.contactTestBitMask = PhysicsCat.Ball
boundary.physicsBody?.collisionBitMask = PhysicsCat.Ball
boundary.physicsBody!.dynamic = false
boundary.physicsBody!.friction = 0
boundary.physicsBody!.restitution = 1
boundary.zPosition = 5
self.addChild(boundary)
And finally here is my ball:
Ball = SKSpriteNode(imageNamed: "Ball")
Ball.size = CGSize(width: 38, height: 38)
Ball.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
Ball.physicsBody = SKPhysicsBody(circleOfRadius: Ball.frame.width / 2)
Ball.physicsBody?.categoryBitMask = PhysicsCat.Ball
Ball.physicsBody?.collisionBitMask = PhysicsCat.Wall
Ball.physicsBody?.contactTestBitMask = PhysicsCat.Wall | PhysicsCat.Score | PhysicsCat.wallSpace | PhysicsCat.Boundaries
Ball.physicsBody?.affectedByGravity = false
Ball.physicsBody?.dynamic = true
Ball.physicsBody!.allowsRotation = true
Ball.zRotation = CGFloat(M_PI)
self.addChild(Ball)
When I come in contact with the boundary, it prints "I collided with the boundary", but I go straight through it. What exactly am I missing from my code to get the actual collision to occur between the ball and boundary?
Alter you didBeginContact to do something on contact.
This question has been asked and the solution in this thread is by applying an Impluse in your didBeginContact.
Currently, you only have a print statement happening.
Try adding the relevant code like in this answer.
https://stackoverflow.com/a/29433778/6407353

Keeping an Object from moving beyond a certain point

Im am making a game in sprite kit that requires the user to move the player across the x axis to catch things. I am trying to make it impossible for the player to move off the screen because that looks bad. I have tried to do this with an if statement saying if it moves past a certain point teleport it back to the correct point but am not sure how to do this. That might be the right way to do this but i'm not sure. Please tell / show me what I should do to make this work. I will post the code below.
import SpriteKit
struct physicsCatagory {
static let person : UInt32 = 0x1 << 1
static let Ice : UInt32 = 0x1 << 2
static let IceTwo : UInt32 = 0x1 << 3
static let IceThree : UInt32 = 0x1 << 4
static let Score : UInt32 = 0x1 << 5
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var timeOfLastSpawn: CFTimeInterval = 0.0
var timePerSpawn: CFTimeInterval = 1.2
var scorenumber = Int()
var lifenumber = Int()
var SpeedNumber : Double = 0.5
var person = SKSpriteNode(imageNamed: "Person1")
let Score = SKSpriteNode()
var ScoreLable = SKLabelNode()
let BackGround = SKSpriteNode (imageNamed: "BackGround")
override func didMoveToView(view: SKView) {
self.scene?.backgroundColor = UIColor.blueColor()
physicsWorld.contactDelegate = self
self.scene?.size = CGSize(width: 640, height: 1136)
lifenumber = 0
SpeedNumber = 1
BackGround.size = CGSize(width: self.frame.width, height: self.frame.height)
BackGround.position = CGPointMake(self.size.width / 2, self.size.height / 2)
BackGround.zPosition = -5
self.addChild(BackGround)
Score.size = CGSize(width: 648, height: 1)
Score.position = CGPoint(x: 320, y: -90)
Score.physicsBody = SKPhysicsBody(rectangleOfSize: Score.size)
Score.physicsBody?.affectedByGravity = false
Score.physicsBody?.dynamic = false
Score.physicsBody?.categoryBitMask = physicsCatagory.Score
Score.physicsBody?.collisionBitMask = 0
Score.physicsBody?.contactTestBitMask = physicsCatagory.IceThree
Score.color = SKColor.blueColor()
self.addChild(Score)
person.zPosition = 1
person.position = CGPointMake(self.size.width/2, self.size.height/10)
person.setScale(0.32)
person.physicsBody = SKPhysicsBody (rectangleOfSize: CGSize(width: 40, height: 50))
person.physicsBody?.affectedByGravity = false
person.physicsBody?.categoryBitMask = physicsCatagory.person
person.physicsBody?.contactTestBitMask = physicsCatagory.Ice
person.physicsBody?.collisionBitMask = physicsCatagory.Ice
person.physicsBody?.dynamic = false
ScoreLable = SKLabelNode(fontNamed: "Zapfino")
ScoreLable.position = CGPoint(x: self.frame.width / 2, y: 1000)
ScoreLable.text = "\(scorenumber)"
ScoreLable.fontColor = UIColor.yellowColor()
ScoreLable.fontSize = 100
ScoreLable.fontName = "Zapfino "
self.addChild(ScoreLable)
self.addChild(person)
}
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == physicsCatagory.person && secondBody.categoryBitMask == physicsCatagory.IceThree || firstBody.categoryBitMask == physicsCatagory.IceThree && secondBody.categoryBitMask == physicsCatagory.person{
scorenumber++
if scorenumber == 20 {
timePerSpawn = 1.0
}
if scorenumber == 40{
timePerSpawn = 0.89
}
if scorenumber == 60{
timePerSpawn = 0.6
}
if scorenumber == 80{
timePerSpawn = 0.5
}
if scorenumber == 100{
timePerSpawn = 0.4
}
if scorenumber == 120{
timePerSpawn = 0.3
}
ScoreLable.text = "\(scorenumber)"
CollisionWithPerson(firstBody.node as! SKSpriteNode, Person: secondBody.node as! SKSpriteNode)
}
if firstBody.categoryBitMask == physicsCatagory.Score && secondBody.categoryBitMask == physicsCatagory.IceThree ||
firstBody.categoryBitMask == physicsCatagory.IceThree && secondBody.categoryBitMask == physicsCatagory.Score{
lifenumber++
if lifenumber == 1{
//person.texture
person.texture = SKTexture (imageNamed: "Flower#2")
}
if lifenumber == 2{
person.texture = SKTexture (imageNamed: "Flower #3")
}
if lifenumber == 3{
self.view?.presentScene(EndScene())
}
}
}
func CollisionWithPerson (Ice: SKSpriteNode, Person: SKSpriteNode){
Person.removeFromParent()
}
func spawnThirdIce(){
var Ice = SKSpriteNode(imageNamed: "Ice")
Ice.zPosition = 2
Ice.setScale(0.9)
Ice.physicsBody = SKPhysicsBody(rectangleOfSize: Ice.size)
Ice.physicsBody?.categoryBitMask = physicsCatagory.IceThree
Ice.physicsBody?.contactTestBitMask = physicsCatagory.person | physicsCatagory.Score
Ice.physicsBody?.affectedByGravity = false
Ice.physicsBody?.dynamic = true
let MinValue = self.size.width / 8
let MaxValue = self.size.width - 20
let SpawnPoint = UInt32(MaxValue - MinValue)
Ice.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height)
self.addChild(Ice)
let action = SKAction.moveToY(-85, duration: 2.0)
let actionDone = SKAction.removeFromParent()
Ice.runAction(SKAction.sequence([action,actionDone]))
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let previousTouch = touch.previousLocationInNode(self)
let ammountDragged = location.x - previousTouch.x
person.position.x += ammountDragged
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if (currentTime - timeOfLastSpawn > timePerSpawn) {
spawnThirdIce()
self.timeOfLastSpawn = currentTime
}
}
}
You might do something like:
if node.position.x > scene.width {
node.position.x = scene.width
}
if node.position.x < 0 {
node.position.x = 0
}

Not detecting Collisions properly in Swift

I'm making a game that spawns cubes from the top of the screen, when they are touched they disappear. I'm setting an objects position to the point touched by the user. It runs the function DidBeginContact , but it doesn't seem to detect my two views:
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB
print("didbegincontact")
if ((firstBody.categoryBitMask == PhysicsCategory.RedSquare) && (secondBody.categoryBitMask == PhysicsCategory.touchlocation)) || ((firstBody.categoryBitMask == PhysicsCategory.touchlocation) && (secondBody.categoryBitMask == PhysicsCategory.RedSquare)) {
collisionwithredsquare(firstBody.node as! SKSpriteNode)
print("Done")
}
}
It's printing "didBegincontact" so I know that the function is working but the if statement is not.
Here's all the code:
import SpriteKit
struct PhysicsCategory {
static let RedSquare : UInt32 = 1
static let touchlocation : UInt32 = 2
static let BlueSquare : UInt32 = 3
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var animator : UIDynamicAnimator?
let blueSquare = SKSpriteNode()
let redSquare = SKSpriteNode()
var scorenumber = 0
var ground = SKSpriteNode()
var touchpoint = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
physicsWorld.contactDelegate = self
physicsWorld.gravity = CGVector(dx: 1.0, dy: -9.0)
/* let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
// 2. Set the friction of that physicsBody to 0
borderBody.friction = 0
// 3. Set physicsBody of scene to borderBody
self.physicsBody = borderBody */
touchpoint = SKSpriteNode(imageNamed:"Spaceship")
touchpoint.physicsBody?.categoryBitMask = PhysicsCategory.touchlocation
touchpoint.physicsBody?.contactTestBitMask = PhysicsCategory.RedSquare
touchpoint.physicsBody?.affectedByGravity = false
touchpoint.xScale = 0.5
touchpoint.yScale = 0.5
self.addChild(touchpoint)
let width = UInt32(self.frame.size.width)
let X = arc4random() % width
ground = SKSpriteNode(imageNamed: "Ground")
ground.setScale(1.0)
ground.position = CGPoint(x: self.frame.width / 2, y: 5)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.size)
ground.physicsBody?.affectedByGravity = false
ground.physicsBody?.dynamic = false
ground.zPosition = 3
self.addChild(ground)
//INizialize
animator = UIDynamicAnimator(referenceView: self.view!)
//Add Gravity
//animator?.addBehavior(gravity, sprite)
var test = arc4random() % 90
blueSquare.size = CGSize(width: 100, height: 100)
blueSquare.position = CGPoint(x: CGFloat(X), y: 1000)
blueSquare.zRotation = 34
blueSquare.name = "bluecube"
blueSquare.physicsBody = SKPhysicsBody(circleOfRadius: 20)
blueSquare.physicsBody?.affectedByGravity = true
blueSquare.physicsBody?.dynamic = false
blueSquare.color = SKColor.blueColor()
// BlueSquare.physicsBody?.velocity = CGVectorMake(0, 0)
//BlueSquare.physicsBody?.applyImpulse(CGVectorMake(0, -90))
self.addChild(blueSquare)
let X2 = arc4random() % width
redSquare.size = CGSize(width: 100, height: 100)
redSquare.position = CGPoint(x: CGFloat(X2), y: 1000)
redSquare.name = "redcube"
redSquare.physicsBody = SKPhysicsBody(circleOfRadius: 20)
redSquare.physicsBody?.affectedByGravity = true
redSquare.physicsBody?.dynamic = true
redSquare.hidden = false
redSquare.physicsBody?.categoryBitMask = PhysicsCategory.RedSquare
redSquare.physicsBody?.contactTestBitMask = PhysicsCategory.touchlocation
redSquare.color = SKColor.redColor()
self.addChild(redSquare)
let timerAction1 = SKAction.waitForDuration(0.5)
let timerAction2 = SKAction.runBlock(spawning1)
let timerSequence = SKAction.sequence([timerAction1, timerAction2])
runAction(SKAction.repeatActionForever(timerSequence))
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB
print("didbegincontact")
if ((firstBody.categoryBitMask == PhysicsCategory.RedSquare) && (secondBody.categoryBitMask == PhysicsCategory.touchlocation)) || ((firstBody.categoryBitMask == PhysicsCategory.touchlocation) && (secondBody.categoryBitMask == PhysicsCategory.RedSquare)) {
collisionwithredsquare(firstBody.node as! SKSpriteNode)
print("Done")
}
}
func collisionwithredsquare(redsquare: SKSpriteNode) {
print("Hello")
}
func spawning1() {
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
touchpoint.position = location
}
}
func score () {
scorenumber++
print(scorenumber)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if CGRectIntersectsRect(redSquare.frame, ground.frame) {
redSquare.position = CGPoint(x: redSquare.position.x, y: redSquare.position.y + 10)
}
}
}
Thanks in advance, Niall
You collision categories are wrong. It should be this because you are dealing with 32 bit integers.
struct PhysicsCategory {
static let RedSquare: UInt32 = 0x1 << 1
static let touchlocation : UInt32 = 0x1 << 2
static let BlueSquare : UInt32 = 0x1 << 3
}
If you use your way you would have to write it like this
struct PhysicsCategory {
static let RedSquare: UInt32 = 1
static let touchlocation : UInt32 = 2
static let BlueSquare : UInt32 = 4
static let somethingElse : UInt32 = 8
}
which is more confusing because you cannot just increment the last number by 1.
Than change your collision code to this. This way you dont have to check for both bodies in the if statement.
/// Did Begin Contact
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 == PhysicsCategory.RedSquare) && (secondBody.categoryBitMask == PhysicsCategory.touchlocation) {
collisionwithredsquare(firstBody.node) // I aim pretty sure you dont need to cast as SKSpriteNod, because the body its already a SKNode.
print("Done")
}
}
Also on the touchLocaiton sprite you forgot to give it a physics body.
touchpoint.pysicsBody = SKPhysicsBody(..) // FORGOT THIS LINE
touchpoint.physicsBody?.categoryBitMask = PhysicsCategory.touchlocation
touchpoint.physicsBody?.contactTestBitMask = PhysicsCategory.RedSquare
touchpoint.physicsBody?.affectedByGravity = false
This should work now if you are already getting the contact method to be called. Let me know how it goes.

Problems with contact/collision in spritekit

I am developing an app where contact plays a big role. I'm my game the "shoe" rest on the ground. I need to be able to know when the shoe is on the ground so it doesn't multi-jump. I also need to know when the shoe hits a hurdle so the game will end. My problem is that it thinks that the ground is a hurdle along with the actual hurdles.
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var shoeGround = SKSpriteNode()
var hurdleTexture = SKTexture()
var hurdlesMoveAndRemove = SKAction()
var hurdlesStopAndRemove = SKAction()
var jump = false
//collision bitmask
let shoeGroundCategory:UInt32 = 0x1 << 0
let hurdleCategory:UInt32 = 0x1 << 28
let groundSensorCategory: UInt32 = 0x1 << 3
override func didMoveToView(view: SKView) {
//Physics
self.physicsWorld.gravity = CGVectorMake(0.0, -8.0)
self.physicsWorld.contactDelegate = self
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//ground
var groundTexture = SKTexture(imageNamed:"ground")
var sprite = SKSpriteNode(texture: groundTexture)
//scale it
sprite.setScale(2.1)
//position it
sprite.position = CGPointMake(self.size.width / 2, sprite.size.height / 2)
//add it to the scene
self.addChild(sprite)
//ground variable for the node
var ground = SKSpriteNode()
//set the position of the node
ground.position = CGPointMake(0, groundTexture.size().height)
ground.zPosition = 1000
//set the physics body to equal the size of the image.
ground.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake(self.frame.size.width, groundTexture.size().height * 1.85))
//physics bodies
ground.physicsBody?.dynamic = false
ground.physicsBody?.restitution = CGFloat(0.0)
//add the object to the scene
self.addChild(ground)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//ground
var groundSensorTexture = SKTexture(imageNamed:"ground")
var spriteSensor = SKSpriteNode(texture: groundSensorTexture)
//scale it
spriteSensor.setScale(2.1)
//position it
spriteSensor.position = CGPointMake(self.size.width / 2, spriteSensor.size.height / 2)
//add it to the scene
self.addChild(sprite)
//ground variable for the node
var groundSensor = SKSpriteNode()
//set the position of the node
groundSensor.position = CGPointMake(0, groundSensorTexture.size().height)
groundSensor.zPosition = 1000
//set the physics body to equal the size of the image.
groundSensor.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake(self.frame.size.width, groundSensorTexture.size().height * 1.85))
//physics bodies
groundSensor.physicsBody?.dynamic = false
groundSensor.physicsBody?.restitution = CGFloat(0.0)
groundSensor.physicsBody?.categoryBitMask = groundSensorCategory
groundSensor.physicsBody?.contactTestBitMask = shoeGroundCategory | hurdleCategory
//add the object to the scene
self.addChild(ground)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//shoe
var shoeGroundTexture = SKTexture(imageNamed:"default_shoe")
//change texture filtering mode.
shoeGroundTexture.filteringMode = SKTextureFilteringMode.Nearest
//Make the object.
shoeGround = SKSpriteNode(texture: shoeGroundTexture)
//set scale
shoeGround.setScale(0.35)
//position it.
shoeGround.position = CGPointMake(self.frame.size.width * 0.35, ((groundSensorTexture.size().height * 2.0) + (shoeGround.frame.size.height/2)))
shoeGround.zPosition = 100
//give it the collision collider of a circle.
shoeGround.physicsBody = SKPhysicsBody(circleOfRadius: shoeGround.size.height / 2)
shoeGround.physicsBody?.dynamic = true
shoeGround.physicsBody?.allowsRotation = false
shoeGround.physicsBody?.restitution = CGFloat(0.0)
shoeGround.physicsBody?.categoryBitMask = shoeGroundCategory
shoeGround.physicsBody?.contactTestBitMask = groundSensorCategory | hurdleCategory
//add it to the scene
self.addChild(shoeGround)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Hurdles
//Create the Hurdles.
hurdleTexture = SKTexture(imageNamed:"hurdle")
//Spawn the Hurdles.
let spawn = SKAction.runBlock({() in self.spawnHurdles()})
var time = arc4random() % 3
time += 2
let delay = SKAction.waitForDuration(2.0, withRange: 2.0)
let spawnThenDelay = SKAction.sequence([spawn, delay])
let spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)
self.runAction(spawnThenDelayForever)
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func didBeginContact(contact: SKPhysicsContact) {
var firstBody, secondBody, thirdBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if ((firstBody.categoryBitMask & shoeGroundCategory) != 0 && (secondBody.categoryBitMask & hurdleCategory != 0)) {
//secondBody.node?.removeFromParent()
println("Hurdle")
}
if ((firstBody.categoryBitMask & shoeGroundCategory != 0) && (secondBody.categoryBitMask & groundSensorCategory != 0)) {
//secondBody.node?.removeFromParent()
jump = true
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func didEndContact(contact: SKPhysicsContact) {
var firstBody, 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 & shoeGroundCategory != 0) &&
(secondBody.categoryBitMask & groundSensorCategory != 0)) {
//secondBody.node?.removeFromParent()
jump = false
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
moveHurdles()
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if jump == true {
shoeGround.physicsBody?.velocity = CGVectorMake(0, 0)
shoeGround.physicsBody?.applyImpulse(CGVectorMake(0, 82))
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func spawnHurdles() {
let hurdle = SKSpriteNode(texture: hurdleTexture)
hurdle.setScale(2.0)
hurdle.position = CGPointMake(0, 175)
hurdle.physicsBody = SKPhysicsBody(rectangleOfSize:hurdle.size)
hurdle.physicsBody?.dynamic = false
hurdle.physicsBody?.categoryBitMask = hurdleCategory
hurdle.physicsBody?.contactTestBitMask = shoeGroundCategory | groundSensorCategory
hurdle.runAction(hurdlesMoveAndRemove)
self.addChild(hurdle)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//movement of Hurdles.
func moveHurdles() {
let distanceToMove = CGFloat(self.frame.size.width + 10.0 * hurdleTexture.size().width)
let moveHurdles = SKAction.moveByX(-distanceToMove, y: 0, duration: NSTimeInterval(0.00185 * distanceToMove))
let removeHurdles = SKAction.removeFromParent()
hurdlesMoveAndRemove = SKAction.sequence([moveHurdles, removeHurdles])
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func stopHurdles() {
let distanceToMove = CGFloat(self.frame.size.width + 10.0 * hurdleTexture.size().width)
let stopHurdles = SKAction.moveByX(0, y: 0, duration: NSTimeInterval(0.00185 * distanceToMove))
let removeHurdles = SKAction.removeFromParent()
hurdlesStopAndRemove = SKAction.sequence([stopHurdles, removeHurdles])
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
One way to check if your shoe is on the ground is to implement a condition at the Update function.
The condition would be to check the shoe's y coordinate and if it's below a certain threshold depending on your hurdles and ground.
The second method would be to check the sprite's velocity if it's close to zero.

spritekit didBeginContact three object not worked

I am tring to [didBeginContact] by three nodes. I wrote under code.
But not work correctly. when white rectangle fole without hitting black or white rectangle,
println("black"), and println("blue") work...
whien white rectangle hit black rectangle, println("blue") work...
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let blackCategory: UInt32 = 0x1 << 0
let whiteCategory: UInt32 = 0x1 << 1
let blueCategory: UInt32 = 0x1 << 2
override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
self.size = view.bounds.size
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsWorld.gravity = CGVectorMake(0.0, -3.0)
let blackSquare = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
blackSquare.position = CGPoint(
x: CGRectGetMidX(self.frame),
y: CGRectGetMidY(self.frame)
)
blackSquare.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50))
blackSquare.physicsBody?.affectedByGravity = false
blackSquare.physicsBody?.dynamic = false
blackSquare.physicsBody?.categoryBitMask = blackCategory
blackSquare.physicsBody?.contactTestBitMask = whiteCategory
let blueSquare = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(50, 50))
blueSquare.position = CGPoint(
x: CGRectGetMidX(self.frame) - 100,
y: CGRectGetMidY(self.frame) - 100
)
blueSquare.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50))
blueSquare.physicsBody?.affectedByGravity = false
blueSquare.physicsBody?.dynamic = false
blackSquare.physicsBody?.categoryBitMask = blueCategory
blackSquare.physicsBody?.contactTestBitMask = whiteCategory
self.addChild(blackSquare)
self.addChild(blueSquare)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch in touches {
let location = touch.locationInNode(self)
let whiteRectangle = SKSpriteNode(color: UIColor.whiteColor(), size: CGSizeMake(50, 50))
whiteRectangle.position = location
whiteRectangle.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50))
whiteRectangle.physicsBody?.categoryBitMask = whiteCategory
whiteRectangle.physicsBody?.contactTestBitMask = blackCategory
whiteRectangle.physicsBody?.contactTestBitMask = blueCategory
self.addChild(whiteRectangle)
}
}
override func update(currentTime: CFTimeInterval) {
}
func didBeginContact(contact: SKPhysicsContact!) {
var firstBody, secondBody, thirdBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
thirdBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
thirdBody = contact.bodyA
}
if firstBody.categoryBitMask & whiteCategory != 0 &&
secondBody.categoryBitMask & blackCategory != 0 {
//secondBody.node?.removeFromParent()
println("black")
}
if firstBody.categoryBitMask & whiteCategory != 0 &&
thirdBody.categoryBitMask & blueCategory != 0 {
//secondBody.node?.removeFromParent()
println("blue")
}
}
}
1) Based on your contact handler, you should set the categories to be
let whiteCategory: UInt32 = 0x1 << 0
let blackCategory: UInt32 = 0x1 << 1
let blueCategory: UInt32 = 0x1 << 2
2) You are incorrectly setting the blackSquare's physicsBody's bit masks twice
blackSquare.physicsBody?.categoryBitMask = blueCategory
blackSquare.physicsBody?.contactTestBitMask = whiteCategory
The above should be
blueSquare.physicsBody?.categoryBitMask = blueCategory
blueSquare.physicsBody?.contactTestBitMask = whiteCategory
3) The third contact body is not needed here.
func didBeginContact(contact: SKPhysicsContact!) {
var firstBody, 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 & whiteCategory) != 0 &&
(secondBody.categoryBitMask & blackCategory != 0)) {
//secondBody.node?.removeFromParent()
println("black")
}
if ((firstBody.categoryBitMask & whiteCategory != 0) &&
(secondBody.categoryBitMask & blueCategory != 0)) {
//secondBody.node?.removeFromParent()
println("blue")
}
}
4) The following is not needed (in touchesBegan). Setting the contact bit masks for the blue and black nodes is sufficient.
whiteRectangle.physicsBody?.contactTestBitMask = blackCategory
whiteRectangle.physicsBody?.contactTestBitMask = blueCategory

Resources