我试图检测正方形的4个边缘处的碰撞,每个边缘是不同的颜色。截图:
目标是获得它,以便如果某个颜色的SKSpriteNode
(圆圈)触及相同的颜色边缘,则将其从场景中移除。每个圆圈在屏幕的每个边缘随机产生。
我尝试在每个边缘添加4个SKSprite节点,并使用didBeginContact
func检测碰撞。我还尝试通过gameScene.sks文件执行此操作,但由于某种原因,即使添加了它未检测到的collisionBitMask
类别,它也无法检测到冲突。但是我仍然无法让它发挥作用而且非常令人沮丧,请帮助。
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var bombColor = SKSpriteNode()
var blueBombCatagory : UInt32 = 0x1 << 0
var redBombCatagory : UInt32 = 0x1 << 2
var yellowBombCatagory : UInt32 = 0x1 << 3
var greenBombCatagory : UInt32 = 0x1 << 4
var blueSideContact : UInt32 = 0x1 << 5
var yellowSideContact : UInt32 = 0x1 << 6
var greenSideContact : UInt32 = 0x1 << 7
var redSideContact : UInt32 = 0x1 << 8
var speedFactor:CGFloat = 0.5
var spawnInterval = 0.4 + (NSTimeInterval(arc4random_uniform(5)) * 0.25)
var spawnTimer = NSTimer()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, 0)
var rotationPoint = childNodeWithName("rotationPoint")
var player = childNodeWithName("player")
var blueSide = childNodeWithName("blueSidePhyscis") as! SKSpriteNode!
let redSide = childNodeWithName("redSide") as! SKSpriteNode!
let greenSide = childNodeWithName("greenSide") as! SKSpriteNode!
let yellowSide = childNodeWithName("yellowSide") as! SKSpriteNode!
blueSide?.physicsBody!.categoryBitMask = blueSideContact
blueSide?.physicsBody!.contactTestBitMask = blueBombCatagory
redSide?.physicsBody!.categoryBitMask = redSideContact
redSide?.physicsBody!.contactTestBitMask = redBombCatagory
greenSide?.physicsBody!.categoryBitMask = greenSideContact
greenSide?.physicsBody!.contactTestBitMask = greenBombCatagory
yellowSide?.physicsBody!.categoryBitMask = yellowSideContact
yellowSide?.physicsBody!.contactTestBitMask = yellowBombCatagory
spawnTimer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("spawnPoint"), userInfo: nil, repeats: true)
}
func didBeginContact(contact: SKPhysicsContact) {
//this gets called automatically when two objects begin contact with each other
if (contact.bodyA.categoryBitMask == blueSideContact && contact.bodyB.categoryBitMask == blueBombCatagory) {
println("blue")
deletion()
} else if(contact.bodyB.categoryBitMask == blueSideContact && contact.bodyA.categoryBitMask == blueBombCatagory) {
println("blue")
deletion()
}else if(contact.bodyA.categoryBitMask == redSideContact && contact.bodyB.categoryBitMask == redBombCatagory) {
println("red")
deletion()
} else if(contact.bodyA.categoryBitMask == greenSideContact && contact.bodyB.categoryBitMask == greenBombCatagory) {
println("green")
deletion()
} else if(contact.bodyA.categoryBitMask == yellowSideContact && contact.bodyB.categoryBitMask == yellowBombCatagory) {
println("yellow")
deletion()
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if location.x < self.size.width/2 {
println("Left")
rotateLeft()
}
else {
println("Right")
rotateRight()
}
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
func deletion() {
bombColor.removeFromParent()
}
func rotateLeft() {
var rotationPoint : SKNode = childNodeWithName("rotationPoint")!
let rotationLeft = SKAction.rotateByAngle(CGFloat(1.570793268), duration: 0.3)
rotationPoint.runAction(rotationLeft)
//player.runAction(rotationLeft)
}
func spawnPoint() {
let spritePicker = arc4random() % 4
var rotationPoint : SKNode = childNodeWithName("rotationPoint")!
switch spritePicker{
case 0:
bombColor = SKSpriteNode(imageNamed: "BombBlue")
bombColor.size = CGSize(width: 20, height: 20)
bombColor.physicsBody = SKPhysicsBody(circleOfRadius: 10)
bombColor.physicsBody?.categoryBitMask = 4294967294
bombColor.physicsBody?.usesPreciseCollisionDetection = true
bombColor.physicsBody?.dynamic = true
bombColor.physicsBody?.collisionBitMask = 4294967295
break;
case 1:
bombColor = SKSpriteNode(imageNamed: "BombRed")
bombColor.size = CGSize(width: 20, height: 20)
bombColor.physicsBody = SKPhysicsBody(circleOfRadius: 10)
bombColor.physicsBody?.categoryBitMask = redBombCatagory
bombColor.physicsBody?.usesPreciseCollisionDetection = true
bombColor.physicsBody?.dynamic = true
bombColor.physicsBody?.collisionBitMask = redSideContact
break;
case 2:
bombColor = SKSpriteNode(imageNamed: "BombYellow")
bombColor.size = CGSize(width: 20, height: 20)
bombColor.physicsBody = SKPhysicsBody(circleOfRadius: 10)
bombColor.physicsBody?.categoryBitMask = yellowBombCatagory
bombColor.physicsBody?.usesPreciseCollisionDetection = true
bombColor.physicsBody?.dynamic = true
bombColor.physicsBody?.collisionBitMask = yellowSideContact
break;
case 3:
bombColor = SKSpriteNode(imageNamed: "BombGreen")
bombColor.size = CGSize(width: 20, height: 20)
bombColor.physicsBody = SKPhysicsBody(circleOfRadius: 10)
bombColor.physicsBody?.categoryBitMask = greenBombCatagory
bombColor.physicsBody?.usesPreciseCollisionDetection = true
bombColor.physicsBody?.dynamic = true
bombColor.physicsBody?.collisionBitMask = greenSideContact
break;
default:
break;
}
let RandomNumber = arc4random() % 1
switch RandomNumber{
case 0:
bombColor.position.x = CGRectGetMidX(self.frame)
bombColor.position.y = CGRectGetMaxY(self.frame)
self.addChild(bombColor)
break;
case 1:
bombColor.position.x = CGRectGetMidX(self.frame)
bombColor.position.y = CGRectGetMinY(self.frame)
self.addChild(bombColor)
break;
case 2:
bombColor.position.x = CGRectGetMinX(self.frame)
bombColor.position.y = CGRectGetMidY(self.frame)
self.addChild(bombColor)
break;
case 3:
bombColor.position.x = CGRectGetMaxX(self.frame)
bombColor.position.y = CGRectGetMidY(self.frame)
self.addChild(bombColor)
break;
default:
break;
}
let speed = 2 - speedFactor
bombColor.runAction(SKAction.moveTo(rotationPoint.position, duration: NSTimeInterval(2)))
}
func rotateRight() {
var rotationPoint : SKNode = childNodeWithName("rotationPoint")!
let rotationRight = SKAction.rotateByAngle(CGFloat(-1.570793268), duration: 0.3)
rotationPoint.runAction(rotationRight)
//player.runAction(rotationRight)
}
}