Swift 碰撞检测不适用于将1添加到分数

Swift 碰撞检测不适用于将1添加到分数,swift,collision,detection,contact,Swift,Collision,Detection,Contact,嘿,伙计们,我在碰撞检测方面遇到了一个大问题,看来飞船能够与objectGroup和missileGroup正确碰撞。问题是,我希望当导弹通过墙节点时,它会在分数上增加1,但它不起作用,我已经尝试修复了5个多小时,但没有找到任何解决方案 class GameScene: SKScene , SKPhysicsContactDelegate { var spaceShip = SKSpriteNode () var bgSpace = SKSpriteNode () var score = 0

嘿,伙计们,我在碰撞检测方面遇到了一个大问题,看来飞船能够与objectGroup和missileGroup正确碰撞。问题是,我希望当导弹通过墙节点时,它会在分数上增加1,但它不起作用,我已经尝试修复了5个多小时,但没有找到任何解决方案

class GameScene: SKScene , SKPhysicsContactDelegate {
var spaceShip = SKSpriteNode ()
var bgSpace = SKSpriteNode ()

var score = 0
var scoreLabel = SKLabelNode()
var gameOverLabel = SKLabelNode()
var labelHolder = SKSpriteNode()


let shipGroup:UInt32 = 1 << 1
let objectGroup:UInt32 = 1 << 2
let wallGroup:UInt32 = 1 << 3
let missileGroup:UInt32 = 1 << 4

var gameOver = 0

var movingObjects = SKNode()


override func didMoveToView(view: SKView) {
/* Setup your scene here */

self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, -5)
self.addChild(movingObjects)

makeBackground()

self.addChild(labelHolder)

scoreLabel.fontName = "Helvetica"
scoreLabel.fontSize = 60
scoreLabel.text = "0"
scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame),        self.frame.size.height - 150)
scoreLabel.zPosition = 1000
self.addChild(scoreLabel)

//add spaceship

var spaceShipTexture = SKTexture (imageNamed: "productImgs/F5S4.png")
spaceShip = SKSpriteNode (texture: spaceShipTexture)
spaceShip.position = CGPoint(x:CGRectGetMidX(self.frame) / 3, y:CGRectGetMidY(self.frame))
spaceShip.xScale = 0.7
spaceShip.yScale = 0.7

spaceShip.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake(spaceShip.frame.size.width, spaceShip.frame.size.height))

spaceShip.physicsBody?.dynamic = true
spaceShip.physicsBody?.allowsRotation = false
spaceShip.physicsBody?.categoryBitMask = shipGroup
spaceShip.physicsBody?.collisionBitMask = objectGroup | missileGroup

spaceShip.zPosition = 10

self.addChild(spaceShip)



//add ground

let ground = SKNode()
ground.position = CGPointMake(0, 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake (self.frame.size.width * 2, 250)) //180
ground.physicsBody?.dynamic = false
ground.physicsBody?.categoryBitMask = objectGroup
ground.physicsBody?.contactTestBitMask = shipGroup

self.addChild(ground)

//add top

let top = SKNode()
top.position = CGPointMake(0, 1180)
top.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake (self.frame.size.width * 2, 1200)) // 1000
top.physicsBody?.dynamic = false
top.physicsBody?.categoryBitMask = objectGroup
top.physicsBody?.contactTestBitMask = shipGroup

self.addChild(top)



var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("makeMissile"), userInfo: nil, repeats: true)

// this is the wall on the left... enable skView.showsPhysics = true in viewcontroller 
var wall = SKNode()
wall.position = CGPoint(x:10 , y: CGRectGetMidY(self.frame))
wall.physicsBody = SKPhysicsBody (rectangleOfSize: CGSizeMake(3, self.frame.size.height))

wall.physicsBody?.dynamic = false
wall.physicsBody?.categoryBitMask = wallGroup
wall.physicsBody?.contactTestBitMask = missileGroup
wall.physicsBody?.collisionBitMask = 0
self.addChild(wall)



}

 func makeBackground(){
 //add bg

if gameOver == 0 {

var bgTexture = SKTexture(imageNamed: "productImgs/1820925.jpg")

var backgroundAnimated = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 12)
var replaceBackground = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
var backgroundMoveForever = SKAction.repeatActionForever(SKAction.sequence([backgroundAnimated,replaceBackground]))

for var i:CGFloat=0; i<3; i++ {

    bgSpace = SKSpriteNode(texture: bgTexture)
    bgSpace.position = CGPoint (x: CGRectGetMidX(self.frame)/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
    bgSpace.size.height = self.frame.height
    bgSpace.runAction(backgroundMoveForever)
    movingObjects.addChild(bgSpace)

    //check this part needed

}

}


}

func makeMissile(){

if gameOver == 0 {


         var missile = SKSpriteNode(imageNamed: "productImgs/missile-hi.png")
    missile.setScale(0.38)

    // Adding SpriteKit physics body for collision detection
    missile.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake(missile.frame.size.width,missile.frame.size.height))
    missile.physicsBody?.dynamic = false



    missile.physicsBody?.categoryBitMask = missileGroup
    missile.physicsBody?.collisionBitMask = shipGroup
    missile.physicsBody?.contactTestBitMask = shipGroup | wallGroup



    missile.physicsBody?.usesPreciseCollisionDetection = true
    missile.name = "missile"


    // Selecting random y position for missile
    var random = arc4random_uniform(UInt32(self.frame.size.height)) + 1

var randomPosition = CGFloat(random)
    missile.position = CGPointMake(self.frame.size.width + 20, randomPosition)


    var movementAmount = arc4random_uniform(UInt32(self.frame.size.height / 2))

    var moveMissile = SKAction.moveByX(-self.frame.size.width * 5, y: 0, duration: NSTimeInterval(self.frame.size.width / 100))
    var removeMissile = SKAction.removeFromParent()
    var moveAndRemoveMissile = SKAction.sequence([moveMissile, removeMissile])

    missile.runAction(moveAndRemoveMissile)

    movingObjects.addChild(missile)

    //add wall



} else if gameOver == 0 {
    gameOver = 1
    movingObjects.speed = 0
    gameOverLabel.fontName = "Helvetica"
    gameOverLabel.fontSize = 40
    gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame) , CGRectGetMidY(self.frame))
    gameOverLabel.zPosition = 100000
    labelHolder.addChild(gameOverLabel)
}

}

// part for adding score not working

func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == wallGroup || contact.bodyB.categoryBitMask == wallGroup {
    score++
    scoreLabel.text = "\(score)"
}
else if gameOver == 0 {

        gameOver = 1

        movingObjects.speed = 0

        gameOverLabel.fontName = "Helvetica"
        gameOverLabel.fontSize = 30
        gameOverLabel.text = "Game Over! Tap to play again."
        gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        labelHolder.addChild(gameOverLabel)

    }


}


override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */

if (gameOver == 0) {
    spaceShip.physicsBody?.velocity = CGVectorMake(0, 0)
    spaceShip.physicsBody?.applyImpulse(CGVectorMake(0, 300))
} else {
    score = 0
    scoreLabel.text = "0"
    movingObjects.removeAllChildren()

    makeBackground()

    spaceShip.position = CGPoint(x:CGRectGetMidX(self.frame) / 3, y:CGRectGetMidY(self.frame))
    spaceShip.physicsBody?.velocity = CGVectorMake(0, 0)
    labelHolder.removeAllChildren()
    gameOver = 0
    movingObjects.speed = 1


}

}
类游戏场景:SKScene,skphysiccontactdelegate{
var spaceShip=SKSpriteNode()
变量bgSpace=SKSpriteNode()
风险值得分=0
var scoreLabel=SKLabelNode()
var gameOverLabel=SKLabelNode()
var labelHolder=SKSpriteNode()

让shipGroup:UInt32=1如果你纠正了冲突,请在
didBeginContact
中删除If,否则If。它应该可以工作。我不知道你所说的
workspaceShip.physicsBody?.collisionBitMask=objectGroup | missionGroup
。设置
workspaceShip.physicsBody?.collisionBitMask=2
,以确保它工作正常

编辑

类游戏场景:SKScene,skphysiccontactdelegate{
var spaceShip=SKSpriteNode()
变量bgSpace=SKSpriteNode()
var wall=SKNode()
风险值得分=0
var scoreLabel=SKLabelNode()
var gameOverLabel=SKLabelNode()
var labelHolder=SKSpriteNode()

让shipGroup:UInt32=1在
中开始接触
,如果contact.bodyA.categoryBitMask==wallGroup | | contact.bodyB.categoryBitMask==wallGroup
我不太了解这些,我想说的是如果bodaA(导弹)与bodyB(墙)接触那么分数会增加1,但墙和导弹似乎彼此都探测到了,我不知道。所以,问题是,如果你做对了if线,有没有办法解决这个问题?我不太理解contact.BodyA.category…语句,一直试图找到一种理解它们的方法来解决这个问题,但我失败了edNow,我正在回答为什么我要删除if,else if…没有办法编辑分数那么,飞船精灵中有多种类型的接触…我想在检测到导弹和墙之间的接触后,在分数上加1。很抱歉打扰你,但是如果你想,我可以把我的代码放在github中并链接到这里。@Darkboss,是的,把它放在g上ithub.首先尝试“if contact.bodyA.categoryBitMask==wallGroup”。如果bodyA是船,则尝试“让wallGroup:UInt32=3”进行修改clear@Darkboss首先,为什么图像之外有很多图像。xcassets,并且在不同的文件夹中是重复的。很抱歉,我测试了不同的图像…正如我告诉你的,我对这个很陌生,sorry@Darkboss、 由于图像和github的速度,我仍然没有这个项目
class GameScene: SKScene , SKPhysicsContactDelegate {
var spaceShip = SKSpriteNode ()
var bgSpace = SKSpriteNode ()
var wall = SKNode()

var score = 0
var scoreLabel = SKLabelNode()
var gameOverLabel = SKLabelNode()
var labelHolder = SKSpriteNode()


let shipGroup:UInt32 = 1 << 1
let objectGroup:UInt32 = 1 << 2
let wallGroup:UInt32 = 1 << 3
let missileGroup:UInt32 = 1 << 4

var gameOver = 0

var movingObjects = SKNode()


override func didMoveToView(view: SKView) {
/* Setup your scene here */

self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, -5)
self.addChild(movingObjects)

makeBackground()

self.addChild(labelHolder)

scoreLabel.fontName = "Helvetica"
scoreLabel.fontSize = 60
scoreLabel.text = "0"
scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame),        self.frame.size.height - 150)
scoreLabel.zPosition = 1000
self.addChild(scoreLabel)

//add spaceship

var spaceShipTexture = SKTexture (imageNamed: "productImgs/F5S4.png")
spaceShip = SKSpriteNode (texture: spaceShipTexture)
spaceShip.position = CGPoint(x:CGRectGetMidX(self.frame) / 3, y:CGRectGetMidY(self.frame))
spaceShip.xScale = 0.7
spaceShip.yScale = 0.7

spaceShip.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake(spaceShip.frame.size.width, spaceShip.frame.size.height))

spaceShip.physicsBody?.dynamic = true
spaceShip.physicsBody?.allowsRotation = false
spaceShip.physicsBody?.categoryBitMask = shipGroup
spaceShip.physicsBody?.collisionBitMask = objectGroup | missileGroup

spaceShip.zPosition = 10

self.addChild(spaceShip)



//add ground

let ground = SKNode()
ground.position = CGPointMake(0, 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake (self.frame.size.width * 2, 250)) //180
ground.physicsBody?.dynamic = false
ground.physicsBody?.categoryBitMask = objectGroup
ground.physicsBody?.contactTestBitMask = shipGroup

self.addChild(ground)

//add top

let top = SKNode()
top.position = CGPointMake(0, 1180)
top.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake (self.frame.size.width * 2, 1200)) // 1000
top.physicsBody?.dynamic = false
top.physicsBody?.categoryBitMask = objectGroup
top.physicsBody?.contactTestBitMask = shipGroup

self.addChild(top)



var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("makeMissile"), userInfo: nil, repeats: true)



wall.position = CGPoint(x:10 , y: CGRectGetMidY(self.frame))
wall.physicsBody = SKPhysicsBody (rectangleOfSize: CGSizeMake(3, self.frame.size.height))

wall.physicsBody?.dynamic = false
wall.physicsBody?.categoryBitMask = wallGroup
wall.physicsBody?.contactTestBitMask = missileGroup
wall.physicsBody?.collisionBitMask = 0
self.addChild(wall)



}

 func makeBackground(){
 //add bg

if gameOver == 0 {

var bgTexture = SKTexture(imageNamed: "productImgs/1820925.jpg")

var backgroundAnimated = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 12)
var replaceBackground = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
var backgroundMoveForever = SKAction.repeatActionForever(SKAction.sequence([backgroundAnimated,replaceBackground]))

for var i:CGFloat=0; i<3; i++ {

    bgSpace = SKSpriteNode(texture: bgTexture)
    bgSpace.position = CGPoint (x: CGRectGetMidX(self.frame)/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
    bgSpace.size.height = self.frame.height
    bgSpace.runAction(backgroundMoveForever)
    movingObjects.addChild(bgSpace)

    //check this part needed

}

}


}

func makeMissile(){

if gameOver == 0 {


         var missile = SKSpriteNode(imageNamed: "productImgs/missile-hi.png")
    missile.setScale(0.38)

    // Adding SpriteKit physics body for collision detection
    missile.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake(missile.frame.size.width,missile.frame.size.height))
    missile.physicsBody?.dynamic = false



    missile.physicsBody?.categoryBitMask = missileGroup
    missile.physicsBody?.collisionBitMask = shipGroup
    missile.physicsBody?.contactTestBitMask = shipGroup | wallGroup



    missile.physicsBody?.usesPreciseCollisionDetection = true
    missile.name = "missile"


    // Selecting random y position for missile
    var random = arc4random_uniform(UInt32(self.frame.size.height)) + 1

var randomPosition = CGFloat(random)
    missile.position = CGPointMake(self.frame.size.width + 20, randomPosition)


    var movementAmount = arc4random_uniform(UInt32(self.frame.size.height / 2))

    var moveMissile = SKAction.moveByX(-self.frame.size.width * 5, y: 0, duration: NSTimeInterval(self.frame.size.width / 100))
    var removeMissile = SKAction.removeFromParent()
    var moveAndRemoveMissile = SKAction.sequence([moveMissile, removeMissile])

    missile.runAction(moveAndRemoveMissile)

    movingObjects.addChild(missile)

    //add wall
 if wall.position.x < missile.position.x{
            score++
            scoreLabel.text = "\(score)"
        }


} else if gameOver == 0 {
    gameOver = 1
    movingObjects.speed = 0
    gameOverLabel.fontName = "Helvetica"
    gameOverLabel.fontSize = 40
    gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame) , CGRectGetMidY(self.frame))
    gameOverLabel.zPosition = 100000
    labelHolder.addChild(gameOverLabel)
}

}

// part for adding score not working

func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == wallGroup || contact.bodyB.categoryBitMask == wallGroup {
    score++
    scoreLabel.text = "\(score)"
}
else if gameOver == 0 {

        gameOver = 1

        movingObjects.speed = 0

        gameOverLabel.fontName = "Helvetica"
        gameOverLabel.fontSize = 30
        gameOverLabel.text = "Game Over! Tap to play again."
        gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        labelHolder.addChild(gameOverLabel)

    }


}


override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */

if (gameOver == 0) {
    spaceShip.physicsBody?.velocity = CGVectorMake(0, 0)
    spaceShip.physicsBody?.applyImpulse(CGVectorMake(0, 300))
} else {
    score = 0
    scoreLabel.text = "0"
    movingObjects.removeAllChildren()

    makeBackground()

    spaceShip.position = CGPoint(x:CGRectGetMidX(self.frame) / 3, y:CGRectGetMidY(self.frame))
    spaceShip.physicsBody?.velocity = CGVectorMake(0, 0)
    labelHolder.removeAllChildren()
    gameOver = 0
    movingObjects.speed = 1


}

}