提前致谢。 我通过使用在屏幕中间设置了一个圆圈
circle = SKShapeNode(circleOfRadius: 100 ) // Size of Circle
circle.position = CGPointMake(frame.midX, frame.midY) //Middle of Screen
circle.strokeColor = SKColor.whiteColor()
circle.glowWidth = 1.0
circle.fillColor = SKColor.orangeColor()
self.addChild(circle)
我想做的是,当用户点击屏幕时,一个 Sprite 会从随机位置出现,并向屏幕中心移动。我遇到的问题是,有时 Sprite 会出现在圆圈内。所以我的计划是让 Sprite 从屏幕的外侧向中心移动。我怎样才能做到这一点?
这是我对随机位置所做的代码
let randomX = CGFloat(arc4random()) % self.frame.weith
let randomY = CGFloat(arc4random()) % self.frame.height
然后设置 Sprite
sprite.position = CGPointMake(randomX, randomY)
我尝试了以下方法来设置 Sprite 的随机位置,但都不起作用
let randomX = Int(arc4random_uniform(UInt32(self.frame.width + self.frame.width / 2))) || Int(arc4random_uniform(UInt32(self.frame.width - self.frame.width / 2)))
let randomY = Int(arc4random_uniform(UInt32(self.frame.height + self.frame.height / 2))) || Int(arc4random_uniform(UInt32(self.frame.height - self.frame.height / 2)))
和
let randomX = (CGFloat(arc4random()) % self.frame.width + self.frame.width / 2) || (CGFloat(arc4random()) % self.frame.width - self.frame.width / 2)
let randomY = (CGFloat(arc4random()) % self.frame.height + self.frame.height / 2) || (CGFloat(arc4random()) % self.frame.height - self.frame.height / 2)
最佳答案
要在 Swift 中生成随机位置,您可以使用以下命令:
var randomX = CGFloat(Int(arc4random()) % width)
var randomY = CGFloat(Int(arc4random()) % height)
现在要生成屏幕外的随机位置,您需要在 4 个可能的位置生成位置 - 屏幕外的左侧、右侧、顶部或底部。
本质上,这就是您尝试用 || 做的事情声明,但是这不适用于分配非 bool 变量。
例子:
func randomPointOffscreen() -> CGPoint
{
let spawn = arc4random_uniform(4)+1
var randomX:CGFloat = -100
var randomY:CGFloat = 100
switch(spawn)
{
case 1:
randomX = -CGFloat(Int(arc4random()) % 320)
randomY = CGFloat(Int(arc4random()) % 640)
break;
case 2:
randomX = 320 + CGFloat(Int(arc4random()) % 320)
randomY = CGFloat(Int(arc4random()) % 640)
break;
case 3:
randomX = CGFloat(Int(arc4random()) % 320)
randomY = 640 + CGFloat(Int(arc4random()) % 640)
break;
case 4:
randomX = CGFloat(Int(arc4random()) % 320)
randomY = -CGFloat(Int(arc4random()) % 640)
break;
default:
break;
}
return CGPointMake(randomX, randomY)
}
关于ios - swift 和 spritekit 的 Sprite 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29454226/
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
我需要一个非常简单的字符串验证器来显示第一个符号与所需格式不对应的位置。我想使用正则表达式,但在这种情况下,我必须找到与表达式相对应的字符串停止的位置,但我找不到可以做到这一点的方法。(这一定是一种相当简单的方法……也许没有?)例如,如果我有正则表达式:/^Q+E+R+$/带字符串:"QQQQEEE2ER"期望的结果应该是7 最佳答案 一个想法:你可以做的是标记你的模式并用可选的嵌套捕获组编写它:^(Q+(E+(R+($)?)?)?)?然后你只需要计算你获得的捕获组的数量就可以知道正则表达式引擎在模式中停止的位置,你可以确定匹配结束
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
我将Cucumber与Ruby结合使用。通过Selenium-Webdriver在Chrome中运行测试时,我想将下载位置更改为测试文件夹而不是用户下载文件夹。我当前的chrome驱动程序是这样设置的:Capybara.default_driver=:seleniumCapybara.register_driver:seleniumdo|app|Capybara::Selenium::Driver.new(app,:browser=>:chrome,desired_capabilities:{'chromeOptions'=>{'args'=>%w{window-size=1920,1
我想在heroku.com上查看我的应用程序日志的内容,所以我关注了thisexcellentadvice并拥有我所有的日志内容。但是我现在很想知道我的日志文件实际在哪里,因为“log/production.log”似乎是空的:C:\>herokuconsoleRubyconsoleforajpbrevx.heroku.com>>files=Dir.glob("*")=>["public","tmp","spec","Rakefile","doc","config.ru","app","config","lib","README","Gemfile.lock","vendor","sc
这应该是一个简单的问题,但我找不到任何相关信息。给定一个Ruby中的正则表达式,对于每个匹配项,我需要检索匹配的模式$1、$2,但我还需要匹配位置。我知道=~运算符为我提供了第一个匹配项的位置,而string.scan(/regex/)为我提供了所有匹配模式。如果可能,我需要在同一步骤中获得两个结果。 最佳答案 MatchDatastring.scan(regex)do$1#Patternatfirstposition$2#Patternatsecondposition$~.offset(1)#Startingandendingpo
我使用“newapp_name”创建了一个新的Rails应用程序,我正在尝试编辑.gitignore文件,但在我的应用程序文件夹中找不到它。我在哪里可以找到它?我安装了Git。 最佳答案 .gitignore位于项目的root中,而不是app子目录中。首先打开终端并进入您的目录。您需要使用ls-a来显示stash文件。然后使用打开.gitignore 关于ruby-on-rails-尝试打开.gitignore以在文本编辑器中对其进行编辑,但在OSXMountainLion上找不到文件位
对于我正在编写的Rails3应用程序,我正在考虑从本地文件系统上的XML、YAML或JSON文件中读取一些配置数据。重点是:我应该把这些文件放在哪里?Rails应用程序中是否有用于存储此类内容的默认位置?附带说明一下,我的应用程序部署在Heroku上。 最佳答案 我经常做的是:如果文件是通用配置文件:我在目录/config中创建一个YAML文件,每个环境有一个上层key如果我为每个环境(大项目)创建一个文件:我为每个环境创建一个YAML并将它们存储在/config/environments/然后我在加载YAML的地方创建了一个初始化