儘管代碼指定應將球從視圖中移除並且應打印「點擊」,但不會發生任何反應。Collsions未被檢測到,儘管它已按照http://www.raywenderlich.com上的規定實施
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
struct PhysicsCatagory {
static let none : UInt32 = 0
static let all : UInt32 = UInt32.max
static let ball :UInt32 = 0b1
static let basket : UInt32 = 0b10
}
var basket = SKSpriteNode()
//moves towards top right corner of the screen
let actionMoveUpRight = SKAction.moveByX(60, y: 60, duration: 0.2)
//moves towards bottom center
let actionMoveDownLeft = SKAction.moveByX(-60, y: -60, duration: 0.2)
//moves towards top left corner of the screen
let actionMoveUpLeft = SKAction.moveByX(-60, y: 60, duration: 0.2)
//moves towards bottom center
let actionMoveDownRight = SKAction.moveByX(60, y: -60, duration: 0.2)
// moves down
let actionMoveDown = SKAction.moveByX(0, y: -50, duration: 0.2)
let basketCategoryMask: UInt32 = 0x1 << 1
let ballCategoryMask: UInt32 = 0x1 << 2
override func didMoveToView(view: SKView) {
/* Setup your scene here */
// setting background colour to green
self.backgroundColor = SKColor.greenColor()
// creating a sprite node named basket and asigning it to an image of a 2D drawn basket
basket = SKSpriteNode(imageNamed: "basket")
//intiallizing basket's location and size
basket.position = CGPointMake(self.size.width/2, self.size.height/8)
basket.size.height = 50
basket.size.width = 75
basket.physicsBody = SKPhysicsBody(rectangleOfSize: basket.size)
basket.physicsBody?.dynamic = false
basket.physicsBody?.categoryBitMask = PhysicsCatagory.basket
basket.physicsBody?.categoryBitMask = PhysicsCatagory.none
basket.physicsBody?.usesPreciseCollisionDetection = true
self.addChild(basket)
//adding the basket to the scene
physicsWorld.contactDelegate = self
//creating the action that adds the balls to the scene
let updateAction = SKAction.runBlock {
var leftOrRight = arc4random_uniform(2)
if leftOrRight == 0 {
self.physicsWorld.gravity = CGVectorMake(-0.5,-2)
}
if leftOrRight == 1 {
self.physicsWorld.gravity = CGVectorMake(0.5, -2)
}
var ball = SKSpriteNode(imageNamed: "redBall")
ball.position = CGPointMake(self.size.width/2, self.size.height)
ball.setScale(0.5)
ball.size.height = 20
ball.size.width = 35
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height/2)
ball.physicsBody!.dynamic = true
self.addChild(ball)
ball.physicsBody? = SKPhysicsBody(circleOfRadius: ball.size.width/2)
ball.physicsBody?.dynamic = true
ball.physicsBody?.categoryBitMask = PhysicsCatagory.ball
ball.physicsBody?.contactTestBitMask = PhysicsCatagory.basket
ball.physicsBody?.collisionBitMask = PhysicsCatagory.none
}
let waitDuration : NSTimeInterval = 2.0
let updateAndWaitAction = SKAction.sequence([updateAction,SKAction.waitForDuration(waitDuration)])
let repeatForeverAction = SKAction.repeatActionForever(updateAndWaitAction)
self.runAction(repeatForeverAction)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if basket.position.y < 125 {
if location.x > basket.position.x {
if location.y > basket.position.y {
basket.runAction(actionMoveUpRight)
}
else {
basket.runAction(actionMoveDownRight)
}
}
else if location.x < basket.position.x {
if location.y > basket.position.y {
basket.runAction(actionMoveUpLeft)
}
else {
basket.runAction(actionMoveDownLeft)
}
}
}
else {
basket.runAction(actionMoveDownLeft)
}
}
}
func didBeginContact(contact: SKPhysicsContact){
var firstBody : SKPhysicsBody
var secondBody : SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if ((firstBody.categoryBitMask & PhysicsCatagory.ball != 0) && (secondBody.categoryBitMask & PhysicsCatagory.basket != 0)) {
var y = true||false
collisionDidHappen(firstBody.node as SKSpriteNode, basket: secondBody.node as SKSpriteNode)
}
}
func collisionDidHappen (ball : SKSpriteNode, basket : SKSpriteNode) {
println("Hit")
ball.removeFromParent()
println("collsion did happen")
}
}
'basket.physicsBody?.categoryBitMask = PhysicsCatagory.none'這應該是collisionBitMask? – Okapi