2016-04-15 88 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) 
}