Swift:覆盖func update(currentTime:NSTimeInterval)不执行

时间:2015-08-26 12:32:54

标签: swift time

我正在创造一个游戏,其中随机敌人被释放并向目标投掷,我试图有一个难度系统,所以分数越高,难度越大,例如:

if score < 100{
            enemy.runAction(SKAction.moveTo(mainBall.position, duration:3))
        }
        else if score >= 100 && score < 200{
            enemy.runAction(SKAction.moveTo(mainBall.position, duration:2.5))

        }
        else if score >= 200 && score < 300{
            enemy.runAction(SKAction.moveTo(mainBall.position, duration:2))
        }
        else{
            enemy.runAction(SKAction.moveTo(mainBall.position, duration:1.5))
        }

所以基本上敌人会更快地移动到目标。我的问题是,我在敌人的功能中拥有所有这些代码。

 func Enemies(){}

我希望此难度代码位于:

override func update(currentTime: NSTimeInterval)

以便它在每帧之前执行代码。但是,当我将此代码放在此函数中时,无效。

这里还要考虑其他一些代码:

func Enemies(){
        // Properties of the enemies
        let enemy = SKSpriteNode(imageNamed:"Ball")
        enemy.size = CGSize(width: 20, height: 20)
        enemy.color = UIColor(red: 0.9, green: 0.1, blue: 0.1, alpha: 1.0)
        enemy.colorBlendFactor = 1.0
        // Physics
        enemy.physicsBody = SKPhysicsBody(circleOfRadius: enemy.size.width/2)
        enemy.physicsBody?.categoryBitMask = PhysicsCatagory.enemy
        enemy.physicsBody?.contactTestBitMask = PhysicsCatagory.smallBall | PhysicsCatagory.mainBall
        enemy.physicsBody?.collisionBitMask = PhysicsCatagory.smallBall | PhysicsCatagory.mainBall
        enemy.physicsBody?.affectedByGravity = false
        enemy.physicsBody?.dynamic = true
        enemy.name = "enemy"

        let randomPosNmbr = arc4random() % 4

        switch randomPosNmbr{
        case 0:
            enemy.position.x = 0
            var positionY = arc4random_uniform(UInt32(frame.size.height))
            enemy.position.y = CGFloat(positionY)
            self.addChild(enemy)
        case 1:
            enemy.position.y = 0
            var positionX = arc4random_uniform(UInt32(frame.size.width))
            enemy.position.x = CGFloat(positionX)
            self.addChild(enemy)
        case 2:
            enemy.position.y = frame.size.height
            var positionX = arc4random_uniform(UInt32(frame.size.width))
            enemy.position.x = CGFloat(positionX)
            self.addChild(enemy)
        case 3:
            enemy.position.x = frame.size.width
            var positionY = arc4random_uniform(UInt32(frame.size.height))
            enemy.position.y = CGFloat(positionY)
            self.addChild(enemy)
        default:
            break
            }
        if score < 100{
            enemy.runAction(SKAction.moveTo(mainBall.position, duration:3))
        }
        else if score >= 100 && score < 200{
            enemy.runAction(SKAction.moveTo(mainBall.position, duration:2.5))
            if count < 1 {
                runAction(SKAction.colorizeWithColor(SKColor.purpleColor(), colorBlendFactor: 1.0, duration: 2.0))
                count++
            }
        }
        else if score >= 200 && score < 300{
            enemy.runAction(SKAction.moveTo(mainBall.position, duration:2))
        }
        else{
            enemy.runAction(SKAction.moveTo(mainBall.position, duration:1.5))
        }

并在touchesBegan函数中:

enemyTimer = NSTimer.scheduledTimerWithTimeInterval(0.7, target: self, selector:Selector("Enemies"), userInfo: nil, repeats: true)

0 个答案:

没有答案