碰撞检测不适用于将 1 加分



嘿伙计们,我在碰撞检测方面遇到了一个大问题,似乎这艘船能够与对象组和导弹组正确碰撞。问题是我希望当导弹通过墙壁节点时,它会在分数上加 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

}
}

如果你愚蠢地改变了你的碰撞,删除didBeginContact if ,else if。它应该有效。我不知道你说的workspaceShip.physicsBody?.collisionBitMask = objectGroup | missileGroup是什么意思.设置workspaceShip.physicsBody?.collisionBitMask = 2以确保其正常工作。

编辑

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

}
}

它有点工作,但它从一开始就增加了 1,因为我无法弄清楚.左边的墙是谁

相关内容

  • 没有找到相关文章

最新更新