tamtoum1987
tamtoum1987

Reputation: 2067

Sprite Kit never detect my collision

When player body meet a monster body the function func didBeginContact(contact: SKPhysicsContact) is never called.

My class implement SKPhysicsContactDelegate

class GameScene: SKScene, SKPhysicsContactDelegate

the property collisionBitMask i commented it because i read that if we don't put it, node can collise with all node

I add my player like this :

bear.position = CGPoint(x:self.frame.width/20, y:self.frame.height/5)
bear.size = CGSize(width:  self.frame.width/7, height:  self.frame.height/5)
bear.physicsBody = SKPhysicsBody(rectangleOfSize: bear.frame.size)
bear.physicsBody?.dynamic = false;
bear.physicsBody?.categoryBitMask = PhysicsCategory.Player
bear.physicsBody?.contactTestBitMask = PhysicsCategory.Monster
//bear.physicsBody?.collisionBitMask = PhysicsCategory.Monster

I add monster like this:

           let thePosition = CGPoint(x: positionX, y: (self.frame.height/7.4)*positionY)

        let monster =  SKSpriteNode(imageNamed: pattern as String)
        monster.size = CGSize(width:  self.frame.width/16, height:  self.frame.height/11)
        monster.name = name
        monster.position = thePosition


         monster.physicsBody = SKPhysicsBody(rectangleOfSize: monster.frame.size)



        monster.physicsBody?.dynamic = false
        monster.physicsBody?.categoryBitMask = PhysicsCategory.Monster // 3
        monster.physicsBody?.affectedByGravity = false
        monster.physicsBody?.contactTestBitMask = PhysicsCategory.Player // 4
        //platformNode.physicsBody?.collisionBitMask = PhysicsCategory.Player // 5


        addChild(monster)

my PhysicalCategory

struct PhysicsCategory {
  static let None      : UInt32 = 0
  static let All       : UInt32 = UInt32.max
  static let Player: UInt32 = 0b1      // 1
  static let Monster   : UInt32 = 0b10       // 2
  static let Projectile: UInt32 = 0b100      // 3
  static let Plateforme: UInt32 = 0b1000      // 4
}

I have multiple Textures, for example when player walk i call this :

    func walkingBear() {
    bear.runAction(    SKAction.repeatActionForever(SKAction.animateWithTextures(bearCoursFrames, timePerFrame: 0.1, resize: false, restore: true)), withKey:"cours")
  }

I initlaise bearCoursFrames like this

  var walkFrames = [SKTexture]()
    var numImages : Int = bearAnimatedAtlas.textureNames.count
    for var i=1; i<=numImages/2; i++ {
        let bearTextureName = "cours\(i)"
        walkFrames.append(bearAnimatedAtlas.textureNamed(bearTextureName))
    }

    bearCoursFrames = walkFrames

Where is my error please, i can't find a solution in other post any help would be appreciated

---------------------------------EDIT------------------------------ Solution: I just add self.physicsWorld.contactDelegate = self and it works now.

Upvotes: 2

Views: 59

Answers (2)

tamtoum1987
tamtoum1987

Reputation: 2067

I just add

       self.physicsWorld.contactDelegate = self 

and it works now.

Upvotes: 1

Whirlwind
Whirlwind

Reputation: 13675

The answer is simple. There is a rule which says that at least one body has to be defined as dynamic in order to register the contact.

Change either player or other body to be dynamic and contact will be detected.

As an addition, if you already haven't, enable physics visual representation ... It can be helpful because sometimes contact is not detected because the actual bodies are not intersecting each other eg. when node's anchor point is changed, so the texture and a body are misplaced (which is fixable, using appropriate initializers of SKPhysicsBody class, but this is a very common error).

Upvotes: 2

Related Questions