在淡入淡出时间后固定多个节点

时间:2017-12-01 13:46:20

标签: sprite-kit nodes

您好我使用空格键生成多个节点我已经设置了一个在x秒内淡化它们的动作。然后一个动作来固定它们。在淡入淡出完成后,所有内容都可以使用。但即使褪色仍有效,如果你不断按空格键,它也不会锁定每个节点。

如果我放置一个计数器,我可以看到一切都进入了mutate函数

这里是相关代码感谢您寻找

xcode 9 mac os不是iOS 10.13.2

    var myBall = SKSpriteNode()

    override func keyDown(with event: NSEvent) {
        let keyEvent = event.characters
        if keyEvent == " "{
            addAlien()
        }
    }


    func addAlien(){
        myBall = SKSpriteNode(imageNamed: "spinner")
        myBall.zPosition = 1
        myBall.xScale = 4
        myBall.yScale = 4

        // Determine where to spawn the ball along the Y axis
        let actualY:CGFloat = CGFloat(arc4random_uniform(624))-312.0
        // Position the ball
        myBall.position = CGPoint(x: 460.0, y: actualY)

        // Determine physicsBody of the ball
        myBall.physicsBody = SKPhysicsBody(circleOfRadius:myBall.size.width / 2 )
        myBall.physicsBody!.isDynamic = true
        myBall.physicsBody!.affectedByGravity = true
        myBall.physicsBody!.friction = 0
        myBall.physicsBody!.restitution = 1
        myBall.physicsBody!.mass = 0.008

        addChild(myBall)

        myBall.physicsBody!.applyImpulse(CGVector(dx: 3.0, dy: -3.0))

        let actionFade = SKAction.fadeAlpha(to: 0.2, duration: 5)
        let actionMutate = SKAction.run(self.mutateFunc)

        //run in a sequence
        myBall.run(SKAction.sequence([actionFade,actionMutate]))

    }//end of addAlien

    func mutateFunc(){
        myBall.physicsBody!.pinned = true

    }

1 个答案:

答案 0 :(得分:0)

嗨以下是回答问题的方法,但其理论和执行方面却很简陋。因为我希望能够在淡入淡出时间内产生40+。我还在寻找最好的答案。

xcode 9 mac os不是iOS 10.13.2

override func keyDown(with event: NSEvent) {
    let keyEvent = event.characters
    if keyEvent == " "{
        addAlien()
    }
}

var counter = 1
func addAlien(){
    myBall = SKSpriteNode(imageNamed: "spinner")
    myBall.zPosition = 1
    myBall.xScale = 4
    myBall.yScale = 4

    // Determine physicsBody of the ball
    myBall.physicsBody = SKPhysicsBody(circleOfRadius:myBall.size.width / 2 )
    myBall.physicsBody!.isDynamic = true
    myBall.physicsBody!.affectedByGravity = true
    myBall.physicsBody!.friction = 0
    myBall.physicsBody!.restitution = 1
    myBall.physicsBody!.mass = 0.008

    // Determine where to spawn the ball along the Y axis
    let actualY:CGFloat = CGFloat(arc4random_uniform(624))-312.0
    // Position the ball
    myBall.position = CGPoint(x: 460.0, y: actualY)

    let actionFade = SKAction.fadeAlpha(to: 0.2, duration: 7)

    if counter == 1{
        one = myBall
        addChild(one)
        one.physicsBody!.applyImpulse(CGVector(dx: 2.0, dy: -2.0))
        let actionMutate = SKAction.run(self.mutateFuncOne)
        one.run(SKAction.sequence([actionFade,actionMutate]))
    }


    if counter == 2{
        two = myBall
        addChild(two)
        two.physicsBody!.applyImpulse(CGVector(dx: 2.0, dy: -2.0))
        let actionMutate = SKAction.run(self.mutateFuncTwo)
        two.run(SKAction.sequence([actionFade,actionMutate]))
    }

    counter = counter + 1
    if counter == 3{counter = 1}
}//end of addAlien

func mutateFuncOne(){
    one.physicsBody!.pinned = true
    one.physicsBody?.allowsRotation = true
    let rotateAction = SKAction.rotate(byAngle: 500, duration: 30)
    one.run(rotateAction)
}

func mutateFuncTwo(){
    two.physicsBody!.pinned = true
    two.size.height = 300
    two.physicsBody?.allowsRotation = true
    let rotateAction = SKAction.rotate(byAngle: 500, duration: 30)
    two.run(rotateAction)
}