- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我制作了一款类似于 Flappy Bird 的游戏。我已经设置了一个函数,以便在英雄死亡时调用 restartScene。触摸后会重新开始游戏,这样用户就可以继续玩了。
我的问题是,是否可以在用户点击重启之前延迟 2-3 秒?
func restartScene(){
self.removeAllChildren()
self.removeAllActions()
died = false
gameStarted = false
score = 0
createScene()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if gameStarted == false{
gameStarted = true
for touch in touches{
let location = touch.location(in: self)
if died == true{
restartScene()
}
}
}
最佳答案
在 createButton 的末尾使用 SKAction
来执行延迟:
场景 1,您正在使用 SKScene
处理所有触摸事件(这是您必须做的事情,因为 restartButton 是一个 SKSpriteNode
):
let enable = SKAction.run({[unowned self] in self.restartButton.isUserInteractionEnabled = false})
restartButton.isUserInteractionEnabled = true //This actually disables the button because the touch handler will not be called by scene, but instead the individual button.
//The individual button will have no touch code associated with it, so nothing will happen
restartButton.run(SKAction.sequence([SKAction.wait(duration:2),enable]), withKey:"waitingToEnable")
场景 2,您将 restartButton 用作自定义类:
let enable = SKAction.run({[unowned self] in self.restartButton.isUserInteractionEnabled = true})
restartButton.isUserInteractionEnabled = false //This disables the button because the touch handler will not be called by individual button, and instead will go to whatever is touch enabled under it.
restartButton.run(SKAction.sequence([SKAction.wait(duration:2),enable]), withKey:"waitingToEnable")
在您的特定情况下,我会这样写:
func createButton(){
restartButton = SKSpriteNode(imageNamed: "restart")
restartButton.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
restartButton.zPosition = 10
restartButton.setScale(1.2)
restartButton.name = "Restart"
restartButton.setScale(1.5)
self.addChild(restartButton)
let enable = SKAction.run({[unowned self] in self.restartButton.isUserInteractionEnabled = false})
restartButton.isUserInteractionEnabled = true //This actually disables the button because the touch handler will not be called by scene, but instead the individual button.
//The individual button will have no touch code associated with it, so nothing will happen
let waitAndEnable = SKAction.sequence([SKAction.wait(duration:2),enable])
let fadeIn = SKAction.fadeIn(withDuration: 1.5)
let runConcurrently = SKAction.group([waitAndEnable,fadeIn])
restartButton.run(runConcurrently)
highScoreLabel.text = "High Score: \(UserDefaults().integer(forKey: "HIGHSCORE"))"
highScoreLabel.fontColor = UIColor.white
highScoreLabel.fontSize = 20
highScoreLabel.position = CGPoint(x: 80, y: 20)
highScoreLabel.zPosition = 6
livesLabel.position = CGPoint(x: frame.size.width / 5.4, y: 30)
livesLabel.text = "Lives: \(lives)"
livesLabel.zPosition = 5
livesLabel.fontSize = 20
livesLabel.fontColor = UIColor.black
self.addChild(livesLabel)
livesLabel.zPosition = 10
}
看起来您没有正确处理 touchesBegan。您已将其设置为用户触摸场景中的任何位置,游戏将重新开始。
您需要针对特定节点以确保发生这种情况。
我已经添加了 touchesBegan 更改以满足您的需求。您必须根据案例陈述中的内容命名您的游戏场景才能使其正常工作。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let location = touch.location(in: self)
let node = nodeAtPoint(location)
switch(node.name)
{
case "Restart":
if died = true{
restartScene()
}
case "GameScene": //maybe we want to make this a button?
gameStarted = true //who cares about a branch statement
default:()
}
}
}
关于swift - 试图让我的重启按钮保持空闲 2 - 3 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41225287/
我有一个需要从远程下载数据的更新服务,但是就像微软的 BITS 我想在用户空闲和/或他们的网络利用率低时执行此操作,以免影响他们的体验. 我需要做什么或看什么?谁能指出我从哪里开始并获得我需要的信息的
我有一个运行良好的 Angular 项目,我正在实现 NG-IDLE 和 KeepAlive,以保持 session 新鲜并在 API session 到期之前注销用户。 我的问题是 ng-idle
我开始使用http://reactphp.org/用于处理 HTTP 请求。 我运行多个在不同端口上运行的 React Worker,然后使用 Nginx 作为负载平衡来处理请求。像这样 upstre
我有一个 ViewController,它将通过 init 请求访问位置服务 if ([CLLocationManager authorizationStatus] == kCLAuthorizati
1 S postgres 5038 876 0 80 0 - 11962 sk_wai 09:57 ? 00:00:00 postgres: postgres my_app
有没有办法在 Python 的 IDLE 中循环打开的窗口?似乎唯一的方法就是转到菜单栏,或者单击所需的窗口。谢谢。 最佳答案 在 Mac OS X 上,如果您使用的是来自与 Aqua Cocoa T
我在 java 中实现了一个邮件监听器,监听器进入 IDLE 直到新邮件到达,它打印电子邮件主题并立即再次返回 IDLE。 我是否有可能错过我发出的两个 IDLE 之间的事件(新电子邮件)? 例如,我
在嵌入式设备中,允许空闲 HTTP 连接保持打开状态的实际时间是多少? 我知道在 1999 年左右的互联网时代,互联网聊天室有时只会保持连接打开并在他们进来时发送回复。在那些日子里,HTTP 连接的空
我正在网页(在弹出窗口中)检查用户状态 - 他是活跃的还是空闲的。如果他空闲超过 30 分钟,窗口将自动关闭并重置数据库中的一些标志。 我设置了包含时间的 cookie(他打开弹出窗口的时间),并在每
我在空闲: >>> import mymodule >>> # ??? 导入模块后: if __name__ == '__main__': doStuff() 我实际上如何从 IDLE 中调用
除了 Sun Java Mail 之外,还有支持 IDLE 命令的 Java IMAP 库吗? 我一直在使用 Sun 的 JavaMail,它运行良好,只是它有相当多的内存开销。 最佳答案 Chilk
我尝试实现一个邮件服务器,使用 sun IMAPFolder 和空闲命令解析传感器发送的电子邮件。 不幸的是,空闲状态在30分钟后关闭并抛出FolderClosedException。为了模拟在空闲重
我有一个 PHP 脚本可以从文本文件中导入各种数据。 导入非常复杂,我的测试文件有 32.000 个条目。这些条目必须被解析并插入到 mysql 数据库中。 如果我要运行我的脚本,它需要 30 分钟才
我正在使用在后端使用 SQL Server 2008 的 Hibernate 开发 Spring REST Web 应用程序。我在 64 位 Windows 机器上使用 Tomcat 6 作为 Web
我对 JS 中的任务运行器非常陌生,这是我第一次尝试 GruntJS。在我的 Gruntfile.js 中,我保留了几个任务,例如 jshint、cssmin 等。我可以从命令行运行它们,但是当我在那
麦克:特立独行 python :3.4 我尝试在 Python 的 IDLE 中测试 timeit 模块 import timeit >>> timeit.timeit( "obj.method",
我的 C# 应用程序在系统启动时运行,必须等待本地 SQL Server 实例才能真正执行任何操作。现在,我只是等待服务器响应(我曾经获得服务的等待句柄,但那不可靠),然后启动应用程序的主对话框。 当
我在服务器上有一个 (bash) 脚本,我继承了它的管理方面,最近发现脚本中的一个缺陷,没有人引起我的注意。 发现问题后,其他人都跟我说这让他们很恼火,但从来没有告诉过我(太棒了。。。) 所以,脚本遵
假设我有一个 websocket 可以随时接收事件,但大部分时间处于空闲状态,初始连接后将消耗多少带宽以使其保持事件状态? 不管怎样,服务器是使用 ws 的 NodeJS,而客户端使用的是 QtWeb
当我打开应用程序时,让它在后台模式下运行很长时间,然后再次打开,然后不久又崩溃了。崩溃日志表明这次崩溃是由MapKit框架引起的。这只发生在设备中... . 有人能理解这个崩溃日志吗?请帮我解决这个问
我是一名优秀的程序员,十分优秀!