This is the first question I wrote on the stack. I use Xcode 7.3.1, iOS 9.3 and Swift 2. Also tested directly on the simulator and my iPad. I conducted numerous searches, read 2 Spritekit physics books, and watched numerous video tutorials as well as written ones. I already wrote body physics without problems, and many times with smaller projects, but this one just doesn't give me feedback. This question was asked here earlier in specific cases, i.e. 2 collisions with the same spriton or what is the difference between collisionBitMask and contactTestBitMask. I know about the differences and also know that every object you want to contact needs a BitMask category. Firstly, I assigned the global Physicalbody structure (I tried the binary bitmask and the corresponding Int32, corresponding to each of them without success) or the physical delegate,and each SKSpritenode corresponds to its paired contacts / s. I tried many ways to implement didBeginContact (regarding how it is structured), none of them changed the other. I used to mention feedback. I destroyed everyone except my main player, bullet, newEnemy and the enemy. When I try to even print the result of a contact or a collision with the console, it looks like it is not even readable. I have no activity. These spritenodes are within class 1 (GameScene), I tried separate classes for each functional sprinoid without any luck. In addition, with separate classes, it seems that everything is getting worse even if there are protocols for communication between classes and subclasses. Which, using separate classes, is still new to me, but I know this is the preferred way. I will only show the code which,I believe should be seen to minimize, but let me know if there is anything else to see. I just want the bullets to hit the planes, and I can go from there to other objects.
import AVFoundation
import SpriteKit
import GameKit
struct PhysicsCategory {
static let GroundMask : UInt32 = 1
static let BulletMask : UInt32 = 2
static let PlayerMask : UInt32 = 4
static let EnemyMask : UInt32 = 8
static let EnemyFire : UInt32 = 16
static let All : UInt32 = UInt32.max
}
class GameScene: SKScene, SKPhysicsContactDelegate {
func startScene() {
view?.showsPhysics = true
self.physicsWorld.gravity = CGVectorMake(0, 0)
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsWorld.contactDelegate = self
}
func setupPlayer() {
player = SKScene(fileNamed: "Player")!.childNodeWithName("player")! as! SKSpriteNode
player.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "MyFokker2.png"), size: player.size)
player.physicsBody?.dynamic = false
player.physicsBody?.usesPreciseCollisionDetection = true
player.physicsBody?.categoryBitMask = PhysicsCategory.PlayerMask
player.physicsBody?.contactTestBitMask = PhysicsCategory.EnemyFire
player.physicsBody?.collisionBitMask = 0
player.removeFromParent()
self.addChild(player)
}
func fireBullets() {
bullet = SKSpriteNode(imageNamed: "fireBullet")
bullet.physicsBody = SKPhysicsBody(rectangleOfSize: bullet.size)
bullet.physicsBody?.dynamic = false
bullet.physicsBody?.usesPreciseCollisionDetection = true
bullet.physicsBody?.categoryBitMask = PhysicsCategory.BulletMask
bullet.physicsBody?.contactTestBitMask = PhysicsCategory.EnemyMask
bullet.physicsBody?.collisionBitMask = 0
self.addChild(bullet)
}
func spawnEnemyPlanes() {
let enemy1 = SKScene(fileNamed: "Enemy1")!.childNodeWithName("enemy1")! as! SKSpriteNode
let enemy2 = SKScene(fileNamed: "Enemy2")!.childNodeWithName("enemy2")! as! SKSpriteNode
let enemy3 = SKScene(fileNamed: "Enemy3")!.childNodeWithName("enemy3")! as! SKSpriteNode
let enemy4 = SKScene(fileNamed: "Enemy4")!.childNodeWithName("enemy4")! as! SKSpriteNode
enemyPlanes = [enemy1, enemy2, enemy3, enemy4]
let randomIndex = Int(arc4random_uniform(UInt32(enemyPlanes.count)))
newEnemy = (enemyPlanes[randomIndex])
newEnemy.physicsBody = SKPhysicsBody(rectangleOfSize: newEnemy.size)
newEnemy.physicsBody?.dynamic = false
newEnemy.physicsBody?.usesPreciseCollisionDetection = true
newEnemy.physicsBody?.categoryBitMask = PhysicsCategory.EnemyMask
newEnemy.physicsBody?.contactTestBitMask = PhysicsCategory.BulletMask
newEnemy.physicsBody?.collisionBitMask = 0
newEnemy.removeFromParent()
self.addChild(newEnemy)
}
func spawnEnemyFire() {
enemyFire = SKScene(fileNamed: "EnemyFire")!.childNodeWithName("bullet")! as! SKSpriteNode
enemyFire.removeFromParent()
self.addChild(enemyFire)
enemyFire.physicsBody = SKPhysicsBody(rectangleOfSize: enemyFire.size)
enemyFire.physicsBody?.dynamic = false
enemyFire.physicsBody?.usesPreciseCollisionDetection = true
enemyFire.physicsBody?.categoryBitMask = PhysicsCategory.EnemyFire
enemyFire.physicsBody?.contactTestBitMask = PhysicsCategory.PlayerMask
enemyFire.physicsBody?.collisionBitMask = 0
}
func didBeginContact(contact: SKPhysicsContact) {
if !self.gamePaused && !self.gameOver {
firstBody = contact.bodyA
secondBody = contact.bodyB
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if ((firstBody.categoryBitMask & PhysicsCategory.BulletMask != 0) && (secondBody.categoryBitMask & PhysicsCategory.EnemyMask != 0)) {
print("Bullet hit Enemy")
}
else if ((firstBody.categoryBitMask & PhysicsCategory.PlayerMask != 0) && (secondBody.categoryBitMask & PhysicsCategory.EnemyFire != 0)) {
print("EnemyFire hit our Player")
}
}
}
}
, , ... , .
checkPhysics, .
SpriteNodes didMoveToView.
- , , , ?