Swift SKAction仅对一个节点施加力

时间:2019-02-27 17:26:53

标签: swift skaction

    func obstacle1() {

    ob1Node.position = CGPoint(x: frame.size.width/8, y: frame.size.height*1.5)
    ob2Node.position = CGPoint(x: frame.size.width*0.31, y: frame.size.height*1.5)
    ob3Node.position = CGPoint(x: frame.size.width/2, y: frame.size.height*1.5)
    ob4Node.position = CGPoint(x: frame.size.width*(1-(0.31)), y: frame.size.height*1.5)
    ob5Node.position = CGPoint(x: frame.size.width*(1-(1/8)), y: frame.size.height*1.5)

    ob1Node.physicsBody = SKPhysicsBody(circleOfRadius: 100)
    ob2Node.physicsBody = SKPhysicsBody(circleOfRadius: 100)
    ob3Node.physicsBody = SKPhysicsBody(circleOfRadius: 100)
    ob4Node.physicsBody = SKPhysicsBody(circleOfRadius: 100)
    ob5Node.physicsBody = SKPhysicsBody(circleOfRadius: 100)

    ob1Node.physicsBody?.collisionBitMask = 0
    ob2Node.physicsBody?.collisionBitMask = 0
    ob3Node.physicsBody?.collisionBitMask = 0
    ob4Node.physicsBody?.collisionBitMask = 0
    ob5Node.physicsBody?.collisionBitMask = 0

    ob1Node.fillColor = .yellow
    ob2Node.fillColor = .yellow
    ob3Node.fillColor = .yellow
    ob4Node.fillColor = .yellow
    ob5Node.fillColor = .yellow

    addChild(ob1Node)
    addChild(ob2Node)
    addChild(ob3Node)
    addChild(ob4Node)
    addChild(ob5Node)

    let firstMove = SKAction.moveBy(x: 0, y: -frame.size.height*(0.68), duration:2)
    let delay1 = SKAction.wait(forDuration: 1)
    let randomVector = SKAction.applyForce(CGVector(dx: CGFloat(100), dy: CGFloat(-400)), duration: 5)
    let removeNode = SKAction.removeFromParent()
    let sequence = SKAction.sequence([firstMove,delay1,randomVector,removeNode])

    ob1Node.run(sequence)
    ob2Node.run(sequence)
    ob3Node.run(sequence)
    ob4Node.run(sequence)
    ob5Node.run(sequence)

    print("obstacle1")

}

我正在尝试使用此代码添加5个圆形障碍物进入场景,按矢量移动然后被移除。问题是,节点被添加到其初始位置,然后力矢量仅应用于其中一个圆。在尝试删除某些对象时,只有最左边的节点会接收到向量。在序列的末尾,所有节点都被删除,就好像所有节点都按照该序列进行操作一样,只是力矢量仅如前所述施加了力。

0 个答案:

没有答案