我希望我的背景充满活力。但我在背景中奔跑的英雄也是动态的。当我移动他时,他也会影响我的背景。
2个动态对象可以相互重叠而不会影响彼此的物理吗?
如果是这样,怎么样?
我尝试给他们不同的categoryBitMasks,但他们仍然相互影响。
iOS和编程一般来说还是一个新手。
这是代码
override func didMoveToView(view: SKView) {
background.anchorPoint = CGPoint(x: 0, y: 0)
// background.color = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
background.size = CGSize(width: size.width, height: size.height)
background.name = "bg"
background.position = CGPoint(x: 0, y: 0)
background.physicsBody = SKPhysicsBody(rectangleOfSize: background.size)
background.physicsBody?.dynamic = true
background.physicsBody?.categoryBitMask = bgCategory // 3
background.physicsBody?.contactTestBitMask = bgCategory
background.physicsBody?.collisionBitMask = noneCategory
addChild(background)
background2.anchorPoint = CGPoint(x: 0, y:0)
background2.size = CGSize(width: size.width, height: size.height)
background2.position = CGPoint(x: size.width, y: 0)
background2.physicsBody = SKPhysicsBody(rectangleOfSize: background2.size)
background2.physicsBody?.dynamic = true
background2.physicsBody?.categoryBitMask = bgCategory // 3
background2.physicsBody?.contactTestBitMask = bgCategory
background2.physicsBody?.collisionBitMask = noneCategory
addChild(background2)
scoreLabel.position = CGPoint(x: size.width * 0.5, y: size.height * 0.9)
scoreLabel.text = "\(score)"
addChild(scoreLabel)
physicsWorld.gravity = CGVectorMake(0, 0)
physicsWorld.contactDelegate = self
let boPhysicsSize = CGSizeMake(boWidth - boWidth/2, boHeight - boHeight/6)
bo.physicsBody = SKPhysicsBody(rectangleOfSize: boPhysicsSize)
bo.position = CGPoint(x: -(bo.size.width), y: size.height * 0.5)
bo.physicsBody?.dynamic = true
bo.physicsBody?.categoryBitMask = boCategory // 3
bo.physicsBody?.contactTestBitMask = chainsawCategory // 4
addChild(bo)
func rightImpulse () {
bo.physicsBody?.applyImpulse(CGVector(bo.size.width/40, 0))
background.physicsBody?.applyImpulse(CGVector(-(bo.size.width/40*100), 0))
background2.physicsBody?.applyImpulse(CGVector(-(bo.size.width/40*100), 0))
}
func leftImpulse () {
bo.physicsBody?.applyImpulse(CGVector(-(bo.size.width/40), 0))
background.physicsBody?.applyImpulse(CGVectorMake(bo.size.width/40*100, 0))
background2.physicsBody?.applyImpulse(CGVectorMake(bo.size.width/40*100, 0))
}
func upImpulse () {
bo.physicsBody?.applyImpulse(CGVector(0, bo.size.width/40))
}
func downImpulse () {
bo.physicsBody?.applyImpulse(CGVector(0, -(bo.size.width/40)))
}