2016-04-15 87 views
0

我正在創建一個遊戲,其中涉及一個計時器,該計時器每秒鐘都會在屏幕下方產生對象。爲了獲得積分,你必須捕捉物體。在if語句中更改計時器的時間間隔

我希望一旦玩家達到一定數量的積分,就會增加產卵率。

我試圖做到這一點,通過分配定時器的間隔一個整數(SpeedNumber)值1秒開始並已創建,如果聲明,應該改變該整數爲0.5,一旦玩家達到一定點數。這對我有意義,但它不起作用。

爲什麼這不起作用,應該改變什麼?

import SpriteKit 

struct physicsCatagory { 
    static let person : UInt32 = 0x1 << 1 
    static let Ice : UInt32 = 0x1 << 2 
    static let IceTwo : UInt32 = 0x1 << 3 
    static let IceThree : UInt32 = 0x1 << 4 
    static let Score : UInt32 = 0x1 << 5 
} 


class GameScene: SKScene, SKPhysicsContactDelegate { 


    var scorenumber = Int() 
    var lifenumber = Int() 
    var SpeedNumber : Double = 0.5 
    var person = SKSpriteNode(imageNamed: "Person") 
    let Score = SKSpriteNode() 
    var ScoreLable = SKLabelNode() 


    override func didMoveToView(view: SKView) { 

     self.scene?.backgroundColor = UIColor.purpleColor() 

     physicsWorld.contactDelegate = self 

     self.scene?.size = CGSize(width: 640, height: 1136) 

     lifenumber = 0 
     SpeedNumber = 1 

     Score.size = CGSize(width: 648, height: 1) 
     Score.position = CGPoint(x: 320, y: -90) 
     Score.physicsBody = SKPhysicsBody(rectangleOfSize: Score.size) 
     Score.physicsBody?.affectedByGravity = false 
     Score.physicsBody?.dynamic = false 
     Score.physicsBody?.categoryBitMask = physicsCatagory.Score 
     Score.physicsBody?.collisionBitMask = 0 
     Score.physicsBody?.contactTestBitMask = physicsCatagory.IceThree 
     Score.color = SKColor.blueColor() 
     self.addChild(Score) 



     person.position = CGPointMake(self.size.width/2, self.size.height/12) 
     person.setScale(0.4) 
     person.physicsBody = SKPhysicsBody(rectangleOfSize: person.size) 
     person.physicsBody?.affectedByGravity = false 
     person.physicsBody?.categoryBitMask = physicsCatagory.person 
     person.physicsBody?.contactTestBitMask = physicsCatagory.Ice 
     person.physicsBody?.collisionBitMask = physicsCatagory.Ice 
     person.physicsBody?.dynamic = false 




     ScoreLable.position = CGPoint(x: self.frame.width/2, y: 1000) 
     ScoreLable.text = "\(scorenumber)" 
     ScoreLable.fontColor = UIColor.yellowColor() 
     ScoreLable.fontSize = 100 
     ScoreLable.fontName = "Zapfino " 
     self.addChild(ScoreLable) 



     var IceThreeTimer = NSTimer.scheduledTimerWithTimeInterval(SpeedNumber, target: self, selector: ("spawnThirdIce"), userInfo: nil, repeats: true) 


     self.addChild(person) 





    } 




    func didBeginContact(contact: SKPhysicsContact) { 
      let firstBody = contact.bodyA 
      let secondBody = contact.bodyB 


     if firstBody.categoryBitMask == physicsCatagory.person && secondBody.categoryBitMask == physicsCatagory.IceThree || firstBody.categoryBitMask == physicsCatagory.IceThree && secondBody.categoryBitMask == physicsCatagory.person{ 

      scorenumber++ 

      if scorenumber == 5 { 

       SpeedNumber = 0.5 


      } 


      ScoreLable.text = "\(scorenumber)" 
      CollisionWithPerson(firstBody.node as! SKSpriteNode, Person: secondBody.node as! SKSpriteNode) 


     } 

     if firstBody.categoryBitMask == physicsCatagory.Score && secondBody.categoryBitMask == physicsCatagory.IceThree || 
      firstBody.categoryBitMask == physicsCatagory.IceThree && secondBody.categoryBitMask == physicsCatagory.Score{ 
       lifenumber++ 

       if lifenumber == 3{ 

      self.view?.presentScene(EndScene()) 

     } 

       //self.view?.presentScene(EndScene()) 

     } 
    } 



    func CollisionWithPerson (Ice: SKSpriteNode, Person: SKSpriteNode){ 

     Person.removeFromParent() 

    } 



    func spawnThirdIce(){ 

     var Ice = SKSpriteNode(imageNamed: "Ice") 
     Ice.setScale(0.9) 
     Ice.physicsBody = SKPhysicsBody(rectangleOfSize: Ice.size) 
     Ice.physicsBody?.categoryBitMask = physicsCatagory.IceThree 
     Ice.physicsBody?.contactTestBitMask = physicsCatagory.person | physicsCatagory.Score 
     Ice.physicsBody?.affectedByGravity = false 
     Ice.physicsBody?.dynamic = true 
     let MinValue = self.size.width/8 
     let MaxValue = self.size.width - 20 
     let SpawnPoint = UInt32(MaxValue - MinValue) 
     Ice.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height) 
     self.addChild(Ice) 

     let action = SKAction.moveToY(-85, duration: 2.5) 
     let actionDone = SKAction.removeFromParent() 
     Ice.runAction(SKAction.sequence([action,actionDone])) 


    } 



    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 


     for touch in touches { 
      let location = touch.locationInNode(self) 

     person.position.x = location.x 



        } 
    } 
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 

     for touch in touches { 
      let location = touch.locationInNode(self) 

      person.position.x = location.x 



     } 
    } 




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

回答

1

你可以做你想做的,如下所示:

首先聲明2點的屬性(在你的類,但以外的所有函數定義)

var timeOfLastSpawn: CFTimeInterval = 0.0 
var timePerSpawn: CFTimeInterval = 1.0 

然後,在Update,請檢查如果timePerSpawn已被超過。如果是這樣,請與重生的過程,會生成新的對象,然後重置自上次產卵:

override func update(currentTime: CFTimeInterval) { 
    /* Called before each frame is rendered */ 
    if (currentTime - timeOfLastSpawn > timePerSpawn) { 
     spawnObject() 
     self.timeOfLastSpawn = currentTime 
    } 
} 

func spawnObject() { 
     // Your spawn code here 
} 

使產卵過程中一個獨立的功能,你可以從didMoveToView或任何其他地方調用它的優點在正常的時間控制週期之外產卵物體。

您可以根據需要更改timePerSpawn的值以控制生成對象的速率。

您也可以考慮創建一個SKAction,它在指定的時間間隔運行spawnObject,但我認爲要改變對象產生的速率,您必須刪除並重新創建SKAction,但是您可以做這在timePerSpawn的setter中。

您不應該使用NSTimer作爲SpriteKit,因爲SpriteKit引擎將不知道計時器正在做什麼並且無法控制它(例如,如果將場景設置爲暫停狀態,計時器會繼續運行) 。

+0

對不起,花了這麼久才能回來,但這是完美的,謝謝。現在,一旦達到一定數量的積分,我就可以改變每個產卵的時間。再次感謝你。 – Jaa3

0

你應該你的var IceThreeTimer聲明移到類級別(該方法之外,就在你申報SpeedNumber,這將確保你的手柄,指針會在兩個didMoveToViewdidBeginContact可用的方法。

didMoveToView你改變你的聲明:

// I removed the "var" 
IceThreeTimer = NSTimer.scheduledTimerWithTimeInterval(SpeedNumber, target: self, selector: ("spawnThirdIce"), userInfo: nil, repeats: true) 

然後在didBeginContact修改:

if scorenumber == 5 { 
    SpeedNumber = 0.5 
    // Stop the already running timer 
    IceThreeTimer.invalidate() 
    // Schedule a new timer 
    IceThreeTimer = NSTimer.scheduledTimerWithTimeInterval(SpeedNumber, target: self, selector: ("spawnThirdIce"), userInfo: nil, repeats: true) 
}