私のXcodeプロジェクトで、私はSKShapeNodeは、私が作成した世界のボーダー(画面の端)に当たったときに呼び出される関数を作成しようとしています。 SKShapeNodeは画面の端に当たって重力のために右に回転しますが、接触が行われるとdidBeginContact関数は呼び出されません。ここに私のコードSpriteKitベースカテゴリビットマスクが機能しないが正しく
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
struct PhysicsCategory {
static let redBall: UInt32 = 0x1 << 1
static let blueBall: UInt32 = 0x1 << 2
static let worldBorder: UInt32 = 0x1 << 3
}
let slimeBall = SKSpriteNode(imageNamed: "slimeBall")
let lilyPete = SKSpriteNode(imageNamed:"golden")
override func didMoveToView(view: SKView) {
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsWorld.gravity = CGVectorMake(1, 5)
self.physicsBody?.categoryBitMask = PhysicsCategory.worldBorder
self.physicsBody!.node?.name = "world"
self.backgroundColor = UIColor.darkGrayColor()
let playerCircle = SKShapeNode(circleOfRadius: 15)
playerCircle.name = "blueCircle"
playerCircle.fillColor = UIColor.blueColor()
playerCircle.strokeColor = UIColor.blackColor()
playerCircle.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)
playerCircle.physicsBody?.categoryBitMask = PhysicsCategory.redBall
playerCircle.physicsBody?.contactTestBitMask = PhysicsCategory.worldBorder | PhysicsCategory.redBall
playerCircle.physicsBody?.collisionBitMask = PhysicsCategory.blueBall | PhysicsCategory.worldBorder
addChild(playerCircle)
let enemyCircle = SKShapeNode(circleOfRadius: 50)
enemyCircle.name = "redCircle"
enemyCircle.fillColor = UIColor.redColor()
enemyCircle.strokeColor = UIColor.blackColor()
enemyCircle.physicsBody = SKPhysicsBody(circleOfRadius: 50)
enemyCircle.position = CGPointMake(self.frame.size.width/3, self.frame.size.height/2)
enemyCircle.physicsBody?.affectedByGravity = true
enemyCircle.physicsBody?.dynamic = true
enemyCircle.physicsBody!.mass = 0.05
enemyCircle.physicsBody?.categoryBitMask = PhysicsCategory.blueBall
enemyCircle.physicsBody?.contactTestBitMask = PhysicsCategory.worldBorder | PhysicsCategory.blueBall
enemyCircle.physicsBody?.collisionBitMask = PhysicsCategory.worldBorder | PhysicsCategory.blueBall
addChild(enemyCircle)
enemyCircle.physicsBody?.applyForce(CGVectorMake(-2, -3))
}
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if(contact.bodyA.node?.name == "redCircle") && (contact.bodyB.node?.name == "world") || (contact.bodyA.node?.name == "world") && (contact.bodyB.node?.name == "redCircle"){
print("Contact Made")
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first! as UITouch
let touchLocation = touch.locationInNode(self)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first! as UITouch
let touchLocation = touch.locationInNode(self)
}
}