- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
注意:我在这个项目中使用 swift。 我目前正在开发我的第一个项目,如果在一段时间后没有按下按钮,我会尝试停止(无效)NSTimer。我找到了其他停止 NSTimer 的方法,但没有说明如何在一定时
我正在 Xcode4 中工作,在一个项目中,出于组织目的,我一直使用三个不同的 NSTimer,但我意识到,除了名称之外,计时器是相同的,因此很容易将所有三个计时器合并为一。对于我的项目来说,哪一个更
根据 Apple 文档,将同时使用 WKInterfaceTimer(Watch 本地,倒计时但在结束时不触发任何事件)和 NSTimer(在计时器结束时触发方法)。所以,我的 App Interfa
我目前正在尝试制作一个 queueHandler,它将一个对象数组作为输入,用于在一个简单的 Double 机器人上执行驱动命令。我目前正在尝试使用 GCD 以串行执行我的函数,但是当我在我的队列中使
有个问题...我有定时器 [NSTimer scheduledTimerWithTimeInterval:120
我创建了一个计时器, self.scanTimer = [NSTimer scheduledTimerWithTimeInterval:TIMEOUT_SECONDS target:self s
我尝试使用以下代码停止 NSTimer: - (void)viewDidLoad { [super viewDidLoad]; timer3 = [NSTimer timerWithT
我正在制作一个带有计时器的应用程序。我从给定时间开始计算分钟和秒,直到 0。发生这种情况时,我启动一个警报 View 。 我的结构是这样的: 主线程方法分配一个新线程并初始化它。线程的入口点(方法)有
好吧,这段代码非常基本。用户将答案输入文本框,如果等于“第一+第二”,他们就得到一分。然后,他们有 5 秒钟的时间回答下一个数学问题。如果他们这样做了,函数“doCalculation”将再次运行,他
我正在创建益智游戏应用程序,并使用 NSTimer 显示时间(即 01:20)。当应用程序进入后台时,NSTimer 会暂停,但即使应用程序处于后台状态,我也想继续它。 例如,当应用程序进入后台时,计
我有一个带有 NSTimer *myTimer; 变量的类。在某些时候我会这样做: myTimer = [NSTimer scheduledTimerWithTimeInterval:20 targe
Edit2:为什么在“doSomething”方法中只更新了进度而不是point0? 编辑:使用我拥有的代码。我知道我一定忽略了一些东西,但我就是找不到它。 我正在编写一个 iPhone 应用程序,它
我的应用是一款体育应用。即使在后台游戏也应该继续,我为此使用了 nstimer。但我无法在后台运行计时器。每当应用程序处于后台时,计时器就会停止,返回时它会从离开时的同一时间继续。 最佳答案 您无法在
它将不断倒计时并更新当前时间。任何人都知道如何解决这个问题?我不明白 NStimer 如何识别这样的日期格式:20110803 23:59:59 最佳答案 查找NSDateFormatter 。您可以
是否可以在主线程上运行由 nstimer 调用的选择器? NSTimer 在它自己的线程中生成。 我的结构是一个线程调用一个带有 nstimer 的方法,nstimer 调用一个执行一些更新的方法,但
我在我的应用程序中使用了很多计时器。用于记录时间、移动物体、淡入淡出等。我在不同时间、同一 View 中使用同一个计时器来实现多个目的。我应该如何正确声明和无效或释放我的计时器? Atm 我这样声明计
我正在尝试使用 iPhone SDK 3.0 在线程上运行 NSTimer。我认为我做的一切都是正确的(新的运行循环等)。如果我在 viewDidDissappear 上调用 [timer inval
这是让我困惑了一段时间的事情。 我有一个 NSTimer,添加到 currentRunLoop 中,如果我不保留它,它就会崩溃。 NSTimer *timer = [[NSTimer timerWit
当我们使用 NSTimer 时,一旦在上述时间间隔后调用回调,UI 是否会被阻塞? 最佳答案 这要看情况。大多数时候,这不会成为问题。 如果,但是,满足以下两个条件,NSTimer 将阻塞 UI 线程
我使用大约 20 个 UIImageView 的小型飞机图像(50x50 像素)在 iPhone 屏幕上制作简单的动画。动画是通过按计时器间隔移动 UIImageView center 属性来完成的。
我是一名优秀的程序员,十分优秀!