使用SkPhysicsBody联系

时间:2016-01-20 02:54:20

标签: ios swift sprite-kit

以下是我的代码,它是游戏的一部分,现在玩游戏时会出现一些问题。首先是子弹从英雄的物理形状的底部射出,而不是从形状的中心射出。另一个是英雄和敌人之间的碰撞检测并不总是有效,并使英雄船做随机事。我对这些类别位掩码和接触位的哪一部分有错误?

import UIKit
import SpriteKit
import CoreMotion

class hero: SKSpriteNode {
    var width: CGFloat = 0.0
    var height: CGFloat = 0.0

    func createShip() {

        self.anchorPoint = CGPointMake(1.0, 0.5)
        self.physicsBody = SKPhysicsBody(rectangleOfSize: self.size)

        self.zPosition = 1.0
        self.physicsBody?.mass = 0.02
        self.physicsBody?.dynamic = true
        self.physicsBody?.affectedByGravity = false
        self.physicsBody?.categoryBitMask = ObjectCategory.collisionHeroCategory.rawValue
        self.physicsBody?.contactTestBitMask = ObjectCategory.sceneCategory.rawValue | ObjectCategory.collisionEnemyCategory.rawValue
        self.physicsBody?.collisionBitMask = ObjectCategory.sceneCategory.rawValue | ObjectCategory.collisionEnemyCategory.rawValue
        self.position = CGPointMake(width, height)

    }
}

class enemy:SKSpriteNode {

    func createEnemyShip(heroShipSize:CGSize,viewFrame:CGRect) -> SKSpriteNode{
        let enemyShip = SKSpriteNode(imageNamed: "enemyShip")
        enemyShip.position = CGPointMake(viewFrame.maxX + enemyShip.size.height, CGFloat(arc4random_uniform(UInt32(viewFrame.maxY) - UInt32(enemyShip.size.width))))
        enemyShip.name = "enemyShip"
        enemyShip.zPosition = 1.0
        enemyShip.anchorPoint = CGPointMake(0.5, 0.5)
        enemyShip.physicsBody = SKPhysicsBody(rectangleOfSize: heroShipSize)
        enemyShip.physicsBody?.mass = 5
        enemyShip.physicsBody?.dynamic = true
        enemyShip.physicsBody?.affectedByGravity = false
        enemyShip.physicsBody?.categoryBitMask = ObjectCategory.collisionEnemyCategory.rawValue
        enemyShip.physicsBody?.contactTestBitMask = ObjectCategory.collisionBulletCategory.rawValue | ObjectCategory.collisionHeroCategory.rawValue
        enemyShip.physicsBody?.collisionBitMask = ObjectCategory.collisionBulletCategory.rawValue | ObjectCategory.collisionHeroCategory.rawValue
        return enemyShip
    }
}

class bullet:SKSpriteNode{
    func createBullet(heroShipPosition:CGPoint) -> SKSpriteNode{
        let bullet = SKSpriteNode(imageNamed: "bullet")
        bullet.position = CGPointMake(heroShipPosition.x, heroShipPosition.y)
        bullet.zPosition = 1.0
        // Add physics body for collision detection
        bullet.physicsBody = SKPhysicsBody(rectangleOfSize: bullet.frame.size)
        bullet.physicsBody?.dynamic = true
        bullet.physicsBody?.affectedByGravity = false
        bullet.physicsBody?.categoryBitMask = ObjectCategory.collisionBulletCategory.rawValue
        bullet.physicsBody?.contactTestBitMask = ObjectCategory.collisionEnemyCategory.rawValue
        bullet.physicsBody?.collisionBitMask = ObjectCategory.collisionEnemyCategory.rawValue
        return bullet
    }
}

enum ObjectCategory:UInt32{
    case collisionBulletCategory  =  0
    case collisionHeroCategory =  1
    case sceneCategory =  2
    case collisionEnemyCategory =  3

}

以下是我的场景中的联系功能

func didBeginContact(contact: SKPhysicsContact) {
    if contact.bodyA.categoryBitMask == ObjectCategory.collisionBulletCategory.rawValue && contact.bodyB.categoryBitMask == ObjectCategory.collisionEnemyCategory.rawValue{
    score++
    }

    if contact.bodyB.categoryBitMask == ObjectCategory.collisionEnemyCategory.rawValue && contact.bodyA.categoryBitMask == ObjectCategory.collisionHeroCategory.rawValue{
        endGame()
    }
}

0 个答案:

没有答案