Xcode Swift 碰撞无效
Xcode Swift Collision not working
我正在尝试让 "hero" 与 "wallGenerator" 发生碰撞(生成墙壁)
但无论我做什么它都不会碰撞。 Hero 会撞到地面而不是墙壁,因为它只会 运行 穿过它们。
这是 Hero 和 WallGenerator 的代码
hero.position = CGPointMake(70, movingGround.position.y + movingGround.frame.size.height/2 + hero.frame.size.height/2)
hero.physicsBody = SKPhysicsBody(rectangleOfSize: hero.size)
hero.physicsBody!.dynamic = true
hero.physicsBody!.allowsRotation = false
hero.physicsBody!.categoryBitMask = heroCategory
hero.physicsBody!.collisionBitMask = heroCategory | wallCategory
hero.physicsBody!.contactTestBitMask = wallCategory | heroCategory | groundCategory
wallGenerator = CSWallGenerator(color: UIColor.clearColor(), size: view.frame.size)
wallGenerator.position = view.center
wallGenerator.physicsBody = SKPhysicsBody(edgeLoopFromRect : wallGenerator.frame)
wallGenerator.physicsBody!.dynamic = false
wallGenerator.physicsBody!.categoryBitMask = wallCategory
wallGenerator.physicsBody!.collisionBitMask = wallCategory | heroCategory
CSWallGenerator 代码
class CSWallGenerator: SKSpriteNode {
var generationTimer: NSTimer?
func startGeneratingWallsEvery(seconds: NSTimeInterval) {
generationTimer = NSTimer.scheduledTimerWithTimeInterval(seconds, target: self, selector: "generateWall", userInfo: nil, repeats: true)
}
func generateWall() {
var scale: CGFloat
let rand = arc4random_uniform(2)
if rand == 0 {
scale = -1.0
} else {
scale = 1.0
}
let wall = CSWall()
wall.position.x = size.width/2 + wall.size.width/2
wall.position.y = scale * (kCSGroundHeight/2 + wall.size.height/2)
addChild(wall)
}
}
您正在为 wallgenerator 设置物理体,但实际添加到场景中的是 cswall。 Cswall没有设置物理体
试试
wall.physicsBody = SKPhysicsBody(edgeLoopFromRect : wall.frame)
在您的 generateWall 函数中
我正在尝试让 "hero" 与 "wallGenerator" 发生碰撞(生成墙壁)
但无论我做什么它都不会碰撞。 Hero 会撞到地面而不是墙壁,因为它只会 运行 穿过它们。
这是 Hero 和 WallGenerator 的代码
hero.position = CGPointMake(70, movingGround.position.y + movingGround.frame.size.height/2 + hero.frame.size.height/2)
hero.physicsBody = SKPhysicsBody(rectangleOfSize: hero.size)
hero.physicsBody!.dynamic = true
hero.physicsBody!.allowsRotation = false
hero.physicsBody!.categoryBitMask = heroCategory
hero.physicsBody!.collisionBitMask = heroCategory | wallCategory
hero.physicsBody!.contactTestBitMask = wallCategory | heroCategory | groundCategory
wallGenerator = CSWallGenerator(color: UIColor.clearColor(), size: view.frame.size)
wallGenerator.position = view.center
wallGenerator.physicsBody = SKPhysicsBody(edgeLoopFromRect : wallGenerator.frame)
wallGenerator.physicsBody!.dynamic = false
wallGenerator.physicsBody!.categoryBitMask = wallCategory
wallGenerator.physicsBody!.collisionBitMask = wallCategory | heroCategory
CSWallGenerator 代码
class CSWallGenerator: SKSpriteNode {
var generationTimer: NSTimer?
func startGeneratingWallsEvery(seconds: NSTimeInterval) {
generationTimer = NSTimer.scheduledTimerWithTimeInterval(seconds, target: self, selector: "generateWall", userInfo: nil, repeats: true)
}
func generateWall() {
var scale: CGFloat
let rand = arc4random_uniform(2)
if rand == 0 {
scale = -1.0
} else {
scale = 1.0
}
let wall = CSWall()
wall.position.x = size.width/2 + wall.size.width/2
wall.position.y = scale * (kCSGroundHeight/2 + wall.size.height/2)
addChild(wall)
}
}
您正在为 wallgenerator 设置物理体,但实际添加到场景中的是 cswall。 Cswall没有设置物理体
试试
wall.physicsBody = SKPhysicsBody(edgeLoopFromRect : wall.frame)
在您的 generateWall 函数中