NSTimer不重复

时间:2015-09-05 11:26:48

标签: ios swift sprite-kit nstimer skspritenode

我正在使用Swift创建一个SpriteKit游戏。我正在使用NSTimer,调度计时器与目标和选择器的时间间隔。虽然重复设置为true,但管道只进出一次。这是我的计时器:

let timer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: "makePipes", userInfo: nil, repeats: true)

管道只移动一次,但如果为真则重复。我在didMoveToView中声明了函数(makePipes)。

以下是我的完整代码:

import SpriteKit

class GameScene: SKScene {
func makePipes()
{let action = SKAction.moveToX(self.frame.size.width - 1000, duration: 3)
    pipe.runAction(SKAction.repeatActionForever(action))}

var mainPlayer = SKSpriteNode(imageNamed: "Ball")
var ground = SKSpriteNode(imageNamed: "ground.png")
var pipe = SKSpriteNode(imageNamed: "pipe.png")
var barrier = SKSpriteNode(imageNamed: "Barrier.png")
override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    backgroundColor = UIColor.whiteColor()
    self.physicsWorld.gravity = CGVector(dx: 0, dy: -5)
     mainPlayer.position = CGPoint(x: self.size.width / 2 , y: self.frame.height / 2)
    mainPlayer.color = UIColor.grayColor()
    mainPlayer.colorBlendFactor = 1.0
    ground.size.width = 1000
    barrier.size.width = 1000
    // physics for the main palyer
    mainPlayer.physicsBody = SKPhysicsBody(circleOfRadius: mainPlayer.size.height / 2)
    self.addChild(mainPlayer)


    ground.position = CGPoint(x: self.frame.size.width - 500, y: self.frame.size.height - 750)
    ground.setScale(5)
    ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: self.ground.size.width , height: self.ground.size.height))
    ground.physicsBody?.dynamic = false

    pipe.position = CGPoint(x: self.frame.width , y: self.frame.size.height - self.frame.height + 178)
    pipe.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: pipe.size.width, height: pipe.size.height))
    pipe.physicsBody?.dynamic = true
    pipe.physicsBody?.affectedByGravity = false
    self.addChild(pipe)
    self.addChild(ground)
    barrier.setScale(5)
    barrier.position = CGPoint(x: frame.width / 2 , y: frame.height / 2 + 40)
    barrier.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: barrier.size.width, height: barrier.size.height))
    barrier.physicsBody?.dynamic = false
    barrier.size.height = 200
    self.addChild(barrier)

}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */
    self.physicsWorld.gravity = CGVector(dx: 0, dy: -1)
              if (mainPlayer.size.height < self.frame.height / 5.5) {
        mainPlayer.physicsBody?.velocity = CGVectorMake(0.0, 0.0)
        mainPlayer.physicsBody?.applyImpulse(CGVectorMake(0, 10))
    let timer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: "makePipes", userInfo: nil, repeats: true)

    }



}

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

1 个答案:

答案 0 :(得分:1)

我没有看到管道在任何地方重建。在我看来,函数实际上是实际上被重复调用,但是因为你没有重新创建管道而只是使用相同的管道对象,它只是向下移动得越来越远。您可以尝试在该函数中继续生成管道,而不是仅使用一个管道对象:

fun makePipes()
{
    //create pipe
    var Pipe = SKSpriteNode(imageNamed:"pipe.png")
    pipe.position = CGPoint(x: self.frame.width , y: self.frame.size.height - self.frame.height + 178)
    pipe.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: pipe.size.width, height: pipe.size.height))
    pipe.physicsBody?.dynamic = true
    pipe.physicsBody?.affectedByGravity = false
    self.addChild(pipe)

    //move pipe
    let action = SKAction.moveToX(self.frame.size.width - 1000, duration: 3)
    pipe.runAction(action, completion: {() -> Void in
        //completed action, can remove pipe
        pipe.removeFromParent()
    })
}

还会从您的类中删除实例变量,这样它就不会发生冲突并从viewDidLoad中取出初始化。最后,我建议使用SKAction执行选择器,它将执行相同的操作。但是,它集成在SpriteKit中(您可以避免内存泄漏,并且可以更好地控制时序!)