如何进行运行块操作Swift SpriteKit

时间:2015-01-02 23:01:31

标签: swift sprite-kit

您好我已经尝试这么做了很长时间,我正在使用Swift SpriteKit,我的问题是我需要为一个节点做一个runBlock动作我该怎么做一个运行一个函数的动作我试过这个:

        var a = SKAction.runBlock({() in self.spawn1()})

您的信息spawn1是功能 提前谢谢!

下面是我的代码,以获得第一个答案评论的帮助:

//
//  PlaysScene.swift
//  Pocket Rocket3
//
//  Created by Lucas Farleigh on 27/11/2014.
//  Copyright (c) 2014 Lucas Farleigh. All rights reserved.
//

import spriteKit

class PlayScene:SKScene {
    //declaring the node in this scene!
    let background = SKSpriteNode(imageNamed: "background")

    let bar1 = SKSpriteNode(imageNamed:"bar1")

    let bar2 = SKSpriteNode(imageNamed:"bar2")

    let bar3 = SKSpriteNode(imageNamed:"bar3")

    let bar4 = SKSpriteNode(imageNamed:"bar4")

    let bar5 = SKSpriteNode(imageNamed:"bar5")

    let bar6a = SKSpriteNode(imageNamed: "bar6")
    let bar6b = SKSpriteNode(imageNamed: "bar6")

    let bar6c = SKSpriteNode(imageNamed: "bar6")
    let bar6d = SKSpriteNode(imageNamed: "bar6")

    let bar6e = SKSpriteNode(imageNamed: "bar6")
    let bar6f = SKSpriteNode(imageNamed: "bar6")

    let bar6g = SKSpriteNode(imageNamed: "bar6")
    let bar6h = SKSpriteNode(imageNamed: "bar6")

    let bar6i = SKSpriteNode(imageNamed: "bar6")
    let bar6j = SKSpriteNode(imageNamed: "bar6")
    //making actions

    override func didMoveToView(view: SKView) {


        var actionmove = SKAction.moveToY(0, duration: 15)
        var delay = SKAction.waitForDuration(NSTimeInterval(1.5))
        var delchild = SKAction.removeFromParent()

        func spawn1(){
            bar6a.position = CGPointMake(800 ,CGRectGetMaxY(self.frame))
            bar6b.position = CGPointMake(1600,CGRectGetMaxY(self.frame))
            addChild(bar6a)
            addChild(bar6b)


        }
        func spawn2 (){
            bar6c.position = CGPointMake(400 ,CGRectGetMaxY(self.frame))
            bar6d.position = CGPointMake(1200,CGRectGetMaxY(self.frame))
            addChild(bar6c)
            addChild(bar6d)


        }
        func spawn3 (){
            bar6e.position = CGPointMake(600 ,CGRectGetMaxY(self.frame))
            bar6f.position = CGPointMake(1400,CGRectGetMaxY(self.frame))
            addChild(bar6e)
            addChild(bar6f)


        }
        func spawn4 (){
            bar6g.position = CGPointMake(700 ,CGRectGetMaxY(self.frame))
            bar6h.position = CGPointMake(1500,CGRectGetMaxY(self.frame))
            addChild(bar6g)
            addChild(bar6h)


        }

        func spawn5 (){
            bar6i.position = CGPointMake(700 ,CGRectGetMaxY(self.frame))
            bar6j.position = CGPointMake(1500,CGRectGetMaxY(self.frame))
            addChild(bar6i)
            addChild(bar6j)


        }
        var a = SKAction.runBlock({ self.spawn1() })
        var b = SKAction.runBlock(spawn2)
        var c = SKAction.runBlock(spawn3)
        var d = SKAction.runBlock(spawn4)
        var e = SKAction.runBlock(spawn5)



        var delayA = SKAction.waitForDuration(NSTimeInterval(1.5))
        var delayB = SKAction.waitForDuration(NSTimeInterval(2))
        var delayC = SKAction.waitForDuration(NSTimeInterval(4))
        var delayD = SKAction.waitForDuration(NSTimeInterval(6))
        var delayE = SKAction.waitForDuration(NSTimeInterval(8))

        var sequence1 = SKAction.sequence([delayA,a,actionmove,delchild])
        var sequence2 = SKAction.sequence([delayB,b,actionmove,delchild])
        var sequence3 = SKAction.sequence([delayC,c,actionmove,delchild])
        var sequence4 = SKAction.sequence([delayD,d,actionmove,delchild])
        var sequence5 = SKAction.sequence([delayE,e,actionmove,delchild])

        var repeat1 = SKAction.repeatActionForever(sequence1)
        var repeat2 = SKAction.repeatActionForever(sequence2)
        var repeat3 = SKAction.repeatActionForever(sequence3)
        var repeat4 = SKAction.repeatActionForever(sequence4)
        var repeat5 = SKAction.repeatActionForever(sequence5)


        //actionmove: making it smooth
        actionmove.timingMode = SKActionTimingMode.EaseOut

        //doing stuff with the background
        background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
       background.yScale = 10.0
        background.xScale = 10.0

        addChild(background)

        //running the actions
        bar6a.runAction(repeat1)
        bar6b.runAction(repeat1)
        bar6c.runAction(repeat2)
        bar6d.runAction(repeat2)
        bar6e.runAction(repeat3)
        bar6f.runAction(repeat3)
        bar6g.runAction(repeat4)
        bar6h.runAction(repeat4)
        bar6i.runAction(repeat5)
        bar6j.runAction(repeat5)



    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {



    }
}

1 个答案:

答案 0 :(得分:3)

var a = SKAction.runBlock({ self.spawn1() })
yournode.runAction(a)