简体   繁体   中英

need assistance in Collision between two objects

Before hand, I'm a beginner iOS and coding in general. I'm using Sprite Kit and swift in the following code. Im having trouble understanding how SKPhysicsContactDelegate works. in the following code i have two objects "Ball" and "whiteBall" so what i want to do is every time "Ball" collides with "whitewall" to count +1. i have no clue how to do it. Please help! Thank you!

class Game: SKScene, SKPhysicsContactDelegate {

let Ball = SKSpriteNode(imageNamed: "Red.png")
var QuitOption = SKLabelNode()
var ScoreLabel = SKLabelNode()
var timesecond = Int(60)
var locked = false
var loseOption = SKLabelNode()
var scorePoints = SKLabelNode()
var score = Int()
let whiteBall = SKSpriteNode(imageNamed: "whiteDot")


struct PhysicsCategory {
    static let whiteBall: UInt32 = 1 << 0
    static let Ball: UInt32 = 1 << 1
}


override func didMoveToView(view: SKView) {


    backgroundColor = SKColor.whiteColor() // background for the display

    self.physicsWorld.gravity = CGVectorMake(0, -9.8)
    self.physicsWorld.contactDelegate = self

    let SceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    SceneBody.friction = 0
    self.physicsBody = SceneBody

    scorePoints = SKLabelNode(fontNamed: "Noteworthy-Light")
    scorePoints.text = "0"
    scorePoints.fontColor = SKColor.darkGrayColor()
    scorePoints.fontSize = 35
    scorePoints.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*1 - 120)
    scorePoints.hidden = true
    scorePoints.name = "Points"

    addChild(scorePoints)

    Ball.size = CGSize(width: 82, height: 82)
    Ball.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.1 - 60)
    Ball.physicsBody = SKPhysicsBody(circleOfRadius: 42)
    Ball.physicsBody?.affectedByGravity = true
    Ball.physicsBody?.density = 10
    Ball.physicsBody?.restitution = 0.1
    Ball.physicsBody?.linearDamping = 0
    Ball.name = "Ball"
    Ball.physicsBody?.usesPreciseCollisionDetection = true
    Ball.physicsBody?.categoryBitMask = PhysicsCategory.whiteBall
    Ball.physicsBody?.contactTestBitMask = PhysicsCategory.Ball
    Ball.physicsBody?.collisionBitMask = PhysicsCategory.Ball

    self.addChild(Ball)

    QuitOption.text = "Quit"
    QuitOption.fontName = "Noteworthy-Light"
    QuitOption.fontColor = SKColor.purpleColor()
    QuitOption.fontSize = 35
    QuitOption.position = CGPoint(x: self.frame.size.width/2 - 160, y: self.frame.size.height*1 - 110)
    QuitOption.name = "Quit"

    addChild(QuitOption)

    ScoreLabel = SKLabelNode(fontNamed: "Noteworthy-Light")
    ScoreLabel.fontColor = SKColor.redColor()
    ScoreLabel.fontSize = 35                 // The + will move it to the right side and - to the left side for more accuracy.
    ScoreLabel.position = CGPoint(x: self.frame.size.width/2 + 160, y: self.frame.size.height/1 - 115) // position of ScoreLabelNode
    ScoreLabel.name = "Score+"
    ScoreLabel.hidden = false

    self.addChild(ScoreLabel)

    whiteBall.size = CGSize(width: 55, height: 55)
    whiteBall.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.8 - 30)
    whiteBall.name = "whiteBall"
    whiteBall.physicsBody = SKPhysicsBody(circleOfRadius: 17)
    whiteBall.physicsBody?.dynamic = false
    whiteBall.physicsBody?.restitution = 0.1
    whiteBall.physicsBody?.usesPreciseCollisionDetection = true
    whiteBall.physicsBody?.categoryBitMask = PhysicsCategory.Ball
    whiteBall.physicsBody?.contactTestBitMask = PhysicsCategory.whiteBall
    whiteBall.physicsBody?.collisionBitMask = PhysicsCategory.whiteBall

    self.addChild(whiteBall)


}

// Making the ball jump after user touches ball

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {


    var touch = touches.first as! UITouch
    var location = touch.locationInNode(self)
    var node = self.nodeAtPoint(location)


    if (node.name == "Quit"){

        let myScene = GameScene(size: self.size)
        myScene.scaleMode = scaleMode
        let reveal = SKTransition.fadeWithDuration(1)
        self.view?.presentScene(myScene, transition: reveal)

    }

    if (node.name == "Ball"){

        for touch: AnyObject in touches {


            Ball.physicsBody?.allowsRotation = true
            Ball.physicsBody?.velocity = CGVectorMake(0, 0)
            Ball.physicsBody?.applyImpulse(CGVectorMake(0, 650))

        }

    }

    if(!self.locked){

        self.locked = true

    var actionrun = SKAction.waitForDuration(0.5)

    var actionwait = SKAction.runBlock({

        self.timesecond--

        if self.timesecond == 60 {self.timesecond = 0}

        self.ScoreLabel.text = "\(self.timesecond)"

        if (self.timesecond == 0){

                let myScene = WT(size: self.size)
                myScene.scaleMode = self.scaleMode
                let reveal = SKTransition.fadeWithDuration(1)
                self.view?.presentScene(myScene, transition: reveal)

        }
    })

        let loopAction = SKAction.repeatAction(SKAction.sequence([actionwait, actionrun]), count: 60)

        ScoreLabel.runAction(loopAction, withKey: "scoreAction")


    }

}

func didBeginContact(contact: SKPhysicsContact) {


}


}

Add an "if" statement in the didBeginContact method to detect between ball and whiteBall :

if ((//name of the first body == "Ball" || //name of the second body == "Ball") && (// name of the first body == "whiteBall" || //name of the second body == "whiteBall") {
    //code to handle this collision
    score++;
    ScoreLabel.text = score;
}

I forget the handler to get the names of the first and second nodes that collide. Replace the comment blocks to handle it.

As a hint to get the names of both objects: the contact parameter of the didBeginContact , an SKPhysicsContact , has properties to get the first colliding node and the second colliding node.

try this

func didBeginContact(contact: SKPhysicsContact) {

    let collision = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    if collision == PhysicsCategory.Ball | PhysicsCategory.whiteBall {

        score++

        scoreLabel.text = "Score: \(score)"

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM