Randomizing node movement duration

前端 未结 2 722
清酒与你
清酒与你 2021-01-25 13:09

I am making a game in SpriteKit and I have a node that is moving back and forth across the screen and repeating using the code:

    let moveRight = SKAction.move         


        
2条回答
  •  無奈伤痛
    2021-01-25 13:41

    I don't have any project where I can try right now.

    But you might want to try this :

    let action = [SKAction runBlock:^{
        double randTime = 1.5; // do your arc4random here instead of fixed value
        let moveRight = SKAction.moveByX(frame.size.width/2.8, y: 0, duration: randTime)
        let moveLeft = SKAction.moveByX(-frame.size.width/2.8, y: 0, duration: randTime)
        let texRight = SKAction.setTexture(SKTexture(imageNamed: "Drake2"))
        let texLeft = SKAction.setTexture(SKTexture(imageNamed: "Drake1"))
    
        let sequence = SKAction.sequence([texRight, moveRight, texLeft, moveLeft])
    
        Drake1.runAction(sequence)
    }]; 
    
    let repeatAction = SKAction.repeatActionForever(action)
    
    Drake1.runAction(repeatAction)
    

    Let me know if it helped.

提交回复
热议问题