2015-08-14 2 views
-1

Идея состоит в том, что я создаю блоки, которые выпадают из неба.Как создать пользовательский SKAction в swift

Для этого мне нужно пользовательское действие, которое делает четыре вещи:

  1. Создать узел с моим блоком класса
  2. Установите положение этого узла
  3. добавить узел в сцене
  4. после задержки переходите к пункту один

мне интересно, если вы на самом деле можете создать SKAction.customActionWithDuration сделать это-го ь.

Заранее спасибо

ответ

3

Следующий метод создает SKAction, который должен соответствовать вашим потребностям.

func buildAction() -> SKAction { 
    return SKAction.runBlock { 
     // 1. Create a node: replace this line to use your Block class 
     let node = SKShapeNode(circleOfRadius: 100) 

     // 2. Set the position of that node 
     node.position = CGPoint(x: 500, y: 300) 

     // 3. add the node to the scene 
     self.addChild(node) 

     // 4. after a delay go to point one 
     let wait = SKAction.waitForDuration(3) 
     let move = SKAction.moveTo(CGPoint(x: 500, y: 0), duration: 1) 
     let sequence = SKAction.sequence([wait, move]) 
     node.runAction(sequence) 
    } 
} 
1

Благодаря @appsYourLife. Я сделал несколько изменений ниже:

  1. Я скорректированный на быстрый 3

  2. Я добавил параметр, называемый родительским, так что вы можете использовать либо buildAction(parent: self), или если вы хотите прикрепить узел в какой-то otherNode вы можете использовать buildAction(parent: otherNode)

    func buildAction(parent: SKNode) -> SKAction { 
        return SKAction.run { 
    
        // 1. Create a node: replace this line to use your Block class 
        let node = SKShapeNode(circleOfRadius: 100) 
        // 2. Set the position of that node 
        node.position = CGPoint(x: 500, y: 300) 
    
        // 3. add the node to the scene 
        parent.addChild(node) 
    
        // 4. after a delay go to point one 
        let wait = SKAction.wait(forDuration: 3) 
        let move = SKAction.move(to: CGPoint(x: 500, y: 0), duration: 1) 
        let sequence = SKAction.sequence([wait, move]) 
        node.run(sequence) 
    } 
    } 
    
Смежные вопросы