[SpriteKit Swift]试图产生汽车并以不同的速度将它们移动到屏幕上

时间:2015-10-12 05:54:31

标签: swift

所以,我正在为我的朋友制作一个应用程序,我需要随机在道路上的3个车道中的1个车道中生成汽车,然后以一定的速度向下移动屏幕,并且可以在车辆的多个点上变化&# 39;一生。

let createCarAction = SKAction.runBlock({ //Creates the cars every 4 seconds w/ a waitAction

        let laneNum = laneList[Int(arc4random_uniform(3))] //There is an array higher up with the X values of each lane that the cars can use
        let newCar: SKSpriteNode
        let num = arc4random_uniform(4) //picks random car color
        if num == 0 {
            newCar = SKSpriteNode(imageNamed: "Audi")
        } else if num == 1 {
            newCar = SKSpriteNode(imageNamed: "Audi2")
        } else if num == 2 {
            newCar = SKSpriteNode(imageNamed: "Audi3")
        } else {
            newCar = SKSpriteNode(imageNamed: "Audi4")
        }
        newCar.xScale = 0.30
        newCar.yScale = 0.30
        newCar.position = CGPoint(x: laneNum, y: CGRectGetMaxY(self.frame) + newCar.size.height/2) //puts the car in the picked lane and just above the screen
        newCar.zPosition = CGFloat(2)
        self.addChild(newCar)
        let carAction = SKAction.runBlock({ //this is where it starts going bad. This action is run so many times in the first second the scene starts that it does not switch transition from the scene before it.
            if newCar.position.y > (CGRectGetMinY(self.frame)-newCar.size.height) { //if the car is on the screen basically
                newCar.position = CGPoint(x: newCar.position.x, y: newCar.position.y-CGFloat(spd))
                print("test")
            } else { // not on the screen
                newCar.removeFromParent()
                print("car y: \(CGRectGetMaxY(self.frame) + newCar.size.height/2)")
                newCar.removeAllActions() // This does not seem to have any affect as when only one car has been created, carAction is still running seconds after the car has left the screen
            }


        })
        newCar.runAction(SKAction.repeatActionForever(carAction))
        self.newCarList.append(newCar)
        print("made new car")


    })

我有产卵工作,但当newCar.runAction(SKAction.repeatActionForever(carAction))开始使用时,应用程序不会从MenuScene转移到GameScene。我认为汽车只是瞬间移动到底部,然后游戏只是试图打印汽车的位置。我有更新功能的道路。

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

    if road1.position.y > CGRectGetMinY(self.frame) {
        road1.position = CGPoint(x: CGRectGetMidX(self.frame), y: road1.position.y - CGFloat(spd)/2)
        road2.position = CGPoint(x: road1.position.x, y: road1.position.y + road1.size.height)

    } else {
        road1.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
        road2.position = CGPoint(x: road1.position.x, y: road1.position.y + road1.size.height)   
    }
}

我无法想出一种方法来移动那些不涉及大型阵列或复杂SKAction的汽车,这种汽车可以立即移动汽车而不是与道路相同的速度。

提前致谢。

修改:格式化

1 个答案:

答案 0 :(得分:0)

我最终找到了使用SKAction的方法。

let carWaitAction = SKAction.waitForDuration(1/60)
let carAction = SKAction.runBlock({

            if newCar.position.y > (CGRectGetMinY(self.frame)-newCar.size.height) {
                newCar.position = CGPoint(x: newCar.position.x, y: newCar.position.y-CGFloat(spd/3))
            } else {
                self.score = self.score+5
                newCar.removeFromParent()
                print("car removed")
                newCar.removeAllActions()
                print (newCar.hasActions())
            }
        })
newCar.runAction(SKAction.repeatActionForever(SKAction.group([carWaitAction, carAction])))