如何使一组函数循环swift SpriteKit

时间:2014-12-31 18:53:30

标签: swift sprite-kit

我的问题是我试图让所有的条形图向下移动到屏幕的底部,类似于飞扬的鸟。我已经做了大部分工作,我所做的就是让酒吧走了 但是我想让它继续下去,但是只有5个栏可以继续下去,我怎样才能让它继续像重新生成一样,或者你可以在声明它时在函数中创建一个完成块。

这是我的代码:

//
//  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 check1 = false
        var check2 = false
        var check3 = false
        var check4 = false
        var check5 = false


        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 actionmove = SKAction.moveToY(0, duration: 15)
        var delay = SKAction.waitForDuration(NSTimeInterval(1.5))
        var sequence = SKAction.sequence([ delay , actionmove])
        var delchild = SKAction.removeFromParent()


        func f1 () {
            bar6a.position = CGPointMake(800 ,CGRectGetMaxY(self.frame))
            bar6b.position = CGPointMake(1600,CGRectGetMaxY(self.frame))
            check1 = false

            bar6a.runAction(actionmove)
            bar6b.runAction(actionmove,completion: {
                self.bar6a.position = CGPointMake(800 ,CGRectGetMaxY(self.frame))
                self.bar6b.position = CGPointMake(1600,CGRectGetMaxY(self.frame))
                check1 = true
                self.bar6a.runAction(actionmove)
                self.bar6b.runAction(actionmove,completion: {
                    self.bar6a.removeFromParent()
                    self.bar6b.removeFromParent()

                    check1 = true


                })

            })

            actionmove.timingMode = SKActionTimingMode.EaseOut


        }


        var sequence2 = SKAction.sequence([ delayB ,actionmove])
        var sequence3 = SKAction.sequence([ delayC ,actionmove])
        var sequence4 = SKAction.sequence([ delayD ,actionmove])
        var sequence5 = SKAction.sequence([delayE , actionmove])


        bar6a.xScale = 2.5
        bar6b.xScale = 2.5
        bar6c.xScale = 2.5
        bar6d.xScale = 2.5
        bar6e.xScale = 2.5
        bar6f.xScale = 2.5
        bar6g.xScale = 2.5
        bar6h.xScale = 2.5
        bar6i.xScale = 2.5
        bar6j.xScale = 2.5



        //making the actions

        var num1 = 1
        //making different delays

        //sequence actions


        addChild(bar6a)
        addChild(bar6b)
        addChild(bar6c)
        addChild(bar6d)
        addChild(bar6e)
        addChild(bar6f)
        addChild(bar6g)
        addChild(bar6h)
        addChild(bar6i)
        addChild(bar6j)



        //making the functions



        func f2 () {
            check2 = false

            bar6c.position = CGPointMake(400 ,CGRectGetMaxY(self.frame))
            bar6d.position = CGPointMake(1200,CGRectGetMaxY(self.frame))
            bar6d.runAction(sequence2)
            bar6c.runAction(sequence2, completion:{
            self.bar6c.removeFromParent()
            self.bar6d.removeFromParent()
                check2 = true

            })
            bar6d.runAction(sequence2)
        }

        func f3 () {
            check3 = false

            bar6e.position = CGPointMake(600 ,CGRectGetMaxY(self.frame))
            bar6f.position = CGPointMake(1400,CGRectGetMaxY(self.frame))

            bar6e.runAction(sequence3,completion:{
            self.bar6e.removeFromParent()
            self.bar6f.removeFromParent()
                check3 = true

            })
            bar6f.runAction(sequence3)


        }

        func f4 () {
            check4 = false
            bar6g.position = CGPointMake(700 ,CGRectGetMaxY(self.frame))
            bar6h.position = CGPointMake( 1500,CGRectGetMaxY(self.frame))
            bar6h.runAction(sequence4)
            bar6g.runAction(sequence4,completion:{
            self.bar6g.removeFromParent()
            self.bar6h.removeFromParent()
            check4 = true


            })



        }

        func f5 () {
            check5 = false
            bar6i.position = CGPointMake(700 ,CGRectGetMaxY(self.frame))
            bar6j.position = CGPointMake(1500 ,CGRectGetMaxY(self.frame))
            bar6j.runAction(sequence5)
            bar6i.runAction(sequence5,completion:{
            check5 = true
                self.bar6i.removeFromParent()
                self.bar6j.removeFromParent()


            })



        }

            f1()
            f2()
            f3()
            f4()
            f5()

                //making the action repeat forever

        var num2 = 1


        bar1.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMaxY (self.frame))
        bar2.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMaxY (self.frame))
        bar3.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMaxY (self.frame))
        bar4.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMaxY (self.frame))
        bar5.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMaxY (self.frame))

        bar1.xScale = 2.5
        bar2.xScale = 2.5
        bar3.xScale = 2.5
        bar4.xScale = 2.5
        bar5.xScale = 1.7







        /*
        bar1.runAction(sequence, completion: {
            self.bar2.runAction(sequence, completion: {
                self.bar3.runAction(sequence, completion:{
                    self.bar4.runAction(sequence, completion:{
                    self.bar5.runAction(sequence)
                    })
                    })
            })
            })*/


        background.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))

        //doing the background stuff
        background.yScale = 2.0
        background.xScale = 3.0
        addChild(background)
                //doing the the bar stuff
        if check1 == true{

           f1()

        }
}


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



    }
}

1 个答案:

答案 0 :(得分:0)

编辑:我发现你现在正试图做类似于飞鸟的事情。我建议使用spritekit进行SKPhysicsNode或尝试使用Cocos2d http://www.cocos2d-x.org/wiki/Physics