gpt4 book ai didi

ios - NSTimer 不重复

转载 作者:行者123 更新时间:2023-11-29 01:40:50 25 4
gpt4 key购买 nike

我正在使用 Swift 创建 SpriteKit 游戏。我正在使用 NSTimer,计划计时器与目标和选择器的时间间隔。尽管 repeats 设置为 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 */
}
}

最佳答案

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

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 中(您可以避免内存泄漏,并且可以更好地控制时间!)

关于ios - NSTimer 不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32412515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com