Jump to content

Witaj!

Zaloguj lub Zarejestruj się aby uzyskać pełny dostęp do forum.

Photo
- - - - -

Problem z SKPhysicsContact, kontak jest nierozpoznany.


  • Please log in to reply
No replies to this topic

#1 heezootoo

heezootoo
  • 20 posts

Posted 06 March 2018 - 15:56

Witam!

Mam problem z rozpoznaniem kontaktu, używam SKPhysicsContact  i categorybitmask by rozpoznać kontakt dwóch spritów, jeden to ShapeNode a drugi to SpriteNode, czy ma to znaczenie ze probuje rozpoznać kontakt miedzy SpriteNode, a ShapeNode? moze tutaj jest problem. Czy może problem leży gdzieś w category bitmaskach lub samych właściwościach .physicsBody Spritów? 

 

Wklejam poniżej istotne fragmenty kodu:

import Foundation
import SpriteKit
import GameplayKit
import UIKit

enum BodyType:UInt32{
    case TheCircle = 1
    case TheEightBack = 2
}

class TouchLevel3SceneClass: SKScene, SKPhysicsContactDelegate{
    
    var Eight = SKSpriteNode(imageNamed: "8.png")
    var EightBack = SKSpriteNode(imageNamed: "8Back.png")
    var Circle = SKShapeNode(circleOfRadius: 25)
    
    var counter : Int = 0;
    
    override func didMove(to view: SKView) {
        
      
        
        self.physicsWorld.contactDelegate = self
        
        
        
        Eight.position = CGPoint(x: 0, y: -50)
        Eight.name = "Eight"
        
        addChild(Eight)
        
        
        EightBack.position = CGPoint(x: 0, y: -50)
        EightBack.name = "EightBack"
        EightBack.zPosition = 2
        EightBack.physicsBody? = SKPhysicsBody(texture: EightBack.texture!,
                                                size: EightBack.texture!.size())
        EightBack.physicsBody?.usesPreciseCollisionDetection = true
        EightBack.physicsBody?.isDynamic = true
        EightBack.physicsBody?.affectedByGravity = false
        EightBack.physicsBody?.categoryBitMask = BodyType.TheEightBack.rawValue
        addChild(EightBack)
  
        
        Circle.position = CGPoint(x: 0, y: 450)
        Circle.zPosition = 2;
        Circle.fillColor = SKColor.black;
        Circle.name = "Circle"
        Circle.physicsBody? = SKPhysicsBody(circleOfRadius: 30)
        Circle.physicsBody?.usesPreciseCollisionDetection = true
        Circle.physicsBody?.isDynamic = true
        Circle.physicsBody?.affectedByGravity = false
        Circle.physicsBody?.categoryBitMask = BodyType.TheCircle.rawValue
        Circle.physicsBody?.collisionBitMask = BodyType.TheEightBack.rawValue
        Circle.physicsBody?.contactTestBitMask = BodyType.TheEightBack.rawValue
        addChild(Circle)
       
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches{
            let location = t.location(in: self);
            
            
            if atPoint(location).name == "Circle"{
                Circle.position.x = location.x
                Circle.position.y = location.y
            }
        }
    }
    
   
    
    
    func didBegin(_ contact: SKPhysicsContact) {
        if contact.bodyA.categoryBitMask == BodyType.TheCircle.rawValue && contact.bodyB.categoryBitMask == BodyType.TheEightBack.rawValue{
            print("Touched")
        } else if contact.bodyB.categoryBitMask == BodyType.TheCircle.rawValue && contact.bodyA.categoryBitMask == BodyType.TheEightBack.rawValue{
            print("Touched")
    }
    }
}
    


Edited by heezootoo, 06 March 2018 - 16:00.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users