【问题标题】:Pre-set nodes not colliding with user-touched created nodes预设节点不与用户触摸创建的节点冲突
【发布时间】:2015-09-07 01:26:59
【问题描述】:

例子:

这面墙是在加载时预设的,

var preWall = SKSpriteNode(texture: preWallTexture, size: CGSize(width: width, height: height))

preWall.position = CGPoint(x: x, y: y)

preWall.physicsBody = SKPhysicsBody(edgeLoopFromRect: CGRect(x: x, y: y, width: width, height: height))
preWall.physicsBody?.affectedByGravity = false
preWall.physicsBody?.dynamic = false
preWall.physicsBody?.categoryBitMask = BLOCK

preWall.name = "preWall"

self.addChild(preWall)

这些“沙子”粒子是在用户触摸刷怪箱(屏幕上的红色块)后创建的,

var img = "BlockRed.png"
let sand = SKSpriteNode (imageNamed: img)
var randLoc = arc4random_uniform(26)
sand.position = CGPointMake(location.x - CGFloat(10) + CGFloat(randLoc), location.y)
sand.name = "redSand"
sand.zPosition = 0
self.addChild(sand)

//gravity
sand.physicsBody?.affectedByGravity = true

//contact
sand.physicsBody!.categoryBitMask = PARTICLE
sand.physicsBody?.collisionBitMask = BLOCK | PARTICLE
sand.physicsBody?.contactTestBitMask = BLOCK | PARTICLE
sand.physicsBody = SKPhysicsBody(circleOfRadius: sand.frame.width - 3)

但是,当它们接触时,粒子只是穿过墙壁掉下来。为什么会发生这种情况,我该如何解决?

注意:

// Constants
let NOTHING: UInt32    = 0x1 << 0
let PARTICLE: UInt32   = 0x1 << 1
let BLOCK: UInt32      = 0x1 << 2

【问题讨论】:

    标签: swift sprite-kit skspritenode skphysicsbody


    【解决方案1】:

    在设置bit masks 后,您正在初始化沙粒的physics body。尝试更改行的顺序。

    sand.physicsBody = SKPhysicsBody(circleOfRadius: sand.frame.width - 3)
    sand.physicsBody?.categoryBitMask = PARTICLE
    sand.physicsBody?.collisionBitMask = BLOCK | PARTICLE
    sand.physicsBody?.contactTestBitMask = BLOCK | PARTICLE
    

    【讨论】:

    • 我想通了,我发布了一个答案。感谢您的帮助!
    【解决方案2】:

    预设节点的物理体不是基于体积的体,因此不会与沙粒碰撞。我从:

    preWall.physicsBody = SKPhysicsBody(edgeLoopFromRect: CGRect(x: x, y: y, width: width, height: height))
    

    preWall.physicsBody = SKPhysicsBody(rectangleOfSize: preWall.frame.size)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多