0

I've been working on a game that has to perform a test every time my test node contacts with my check node. As these two nodes are the only nodes that will be checking for collision in the game, I decided to use this method to initiate my test.

The problem is I have been having trouble getting my collision to work when the 2 nodes physical cross.

The current code will move the test square until it reaches the check square, but right now nothing happens when they meet.

Am I missing something? any help would be grateful. thanks.

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {


var test:SKSpriteNode!
var testTexture = SKTexture(imageNamed: "redsquare.png")
var check:SKSpriteNode!
var checkTexture = SKTexture(imageNamed: "bluesquare.png")
var moving:SKNode!
let testCategory: UInt32 = 1 << 0
let checkCategory: UInt32 = 1 << 2


override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    self.physicsWorld.gravity = CGVectorMake(0.0, 0.0)
    self.physicsWorld.contactDelegate = self

    moving = SKNode()
    self.addChild(moving)

    //create test block

    test = SKSpriteNode(texture: testTexture)
    test.xScale = 0.1
    test.yScale = 0.1
    test.position = CGPointMake(0, self.frame.height - test.size.height)

    test.physicsBody = SKPhysicsBody(rectangleOfSize: test.size)
    test.physicsBody?.dynamic = false

    test.physicsBody?.categoryBitMask = testCategory
    test.physicsBody?.collisionBitMask = checkCategory
    test.physicsBody?.contactTestBitMask = checkCategory

    self.addChild(test)

    //creates check block

    check = SKSpriteNode(texture: checkTexture)
    check.xScale = 0.1
    check.yScale = 0.1
    check.position = CGPointMake(self.frame.width, self.frame.height - check.size.height)

    check.physicsBody = SKPhysicsBody(rectangleOfSize: check.size)
    check.physicsBody?.dynamic = false

    check.physicsBody?.categoryBitMask = checkCategory
    check.physicsBody?.collisionBitMask = testCategory
    check.physicsBody?.contactTestBitMask = testCategory

    self.addChild(check)


    // move action

    let actionMove = SKAction.moveToX(self.frame.width, duration: 1.5)
    let actionMoveDone = SKAction.moveToX(0, duration: 0)

    let moveForever = SKAction.repeatActionForever(SKAction.sequence([actionMove,actionMoveDone]))
    test.runAction(moveForever)


}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {

    }
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}

func didBeginContact(contact: SKPhysicsContact) {

// help here! 

    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 & testCategory) != 0 && (secondBody.categoryBitMask & checkCategory) != 0){


        println("contact!")

    }


}

}

  • Update: I figured it out. I was setting both nodes dynamic to false. when I changed one of them to true the contact worked. I'm not sure why though. – John Imyeob Kim Feb 12 '15 at 06:37

1 Answers1

1

According to Apple's documentation, The dynamic property controls whether a volume-based body is affected by gravity, friction, collisions with other objects, and forces or impulses you directly apply to the object.

dynamic property : The default value is true. If the value is false, the physics body ignores all forces and impulses applied to it. This property is ignored on edge-based bodies; they are automatically static.

So set your dynamic property to true.

node.physicsBody?.dynamic = true

For more information : https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/index.html#//apple_ref/occ/instp/SKPhysicsBody/

rakeshbs
  • 24,392
  • 7
  • 73
  • 63