当我按下屏幕上的按钮时,我试图将 Sprite 向右移动。但是,当我尝试这样做时,我只有将 Sprite 移动到某个点的解决方案。所以...我希望 Sprite 永远向右移动,或者直到我做其他事情。
这是在 Xcode 中使用 SpriteKit 中的 Swift。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch : AnyObject in touches{
let pointInTouch = touch.locationInNode(self)
let tappedNode = nodeAtPoint(pointInTouch)
let tappeNodeName = tappedNode.name
if tappeNodeName == "Btn"{
player.physicsBody?.velocity = CGVectorMake(0, 0)
let action = SKAction.applyImpulse(CGVectorMake(400, 0), duration: 1)
player.runAction(SKAction.repeatActionForever(action))
print("Touched!")
}
}
}
最佳答案
您可以简单地将您的节点向右移动,直到它确实存在于场景中
class GameScene: SKScene {
private var player: SKSpriteNode!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard let touch = touches.first else { return }
let pointInTouch = touch.locationInNode(self)
let tappedNode = nodeAtPoint(pointInTouch)
if tappedNode.name == "Btn"{
let moveToRight = SKAction.moveToX(self.frame.width + player.frame.width, duration: 5)
player.runAction(moveToRight)
}
}
}
如果您希望玩家以恒定速度移动,您可以使用此代码。
如您所见,我正在使用space/speed 计算持续时间。您只需要为您的场景找到速度的最佳常数值。
class GameScene: SKScene {
private var player: SKSpriteNode!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard let touch = touches.first else { return }
let pointInTouch = touch.locationInNode(self)
let tappedNode = nodeAtPoint(pointInTouch)
let deltaX = self.frame.width + player.frame.width - player.position.x
let speed: CGFloat = 10 // <-- change this to find the best value for you
let duration: NSTimeInterval = NSTimeInterval(deltaX / speed)
if tappedNode.name == "Btn"{
let moveToRight = SKAction.moveByX(deltaX, y:0, duration: duration)
player.runAction(moveToRight)
}
}
}
关于swift - 永远移动 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38749200/
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?
当我执行>rvminstall1.9.2时一切顺利。然后我做>rvmuse1.9.2也很顺利。但是当涉及到ruby-v时..sam@sjones:~$rvminstall1.9.2/home/sam/.rvm/rubies/ruby-1.9.2-p136,thismaytakeawhiledependingonyourcpu(s)...ruby-1.9.2-p136-#fetchingruby-1.9.2-p136-#downloadingruby-1.9.2-p136,thismaytakeawhiledependingonyourconnection...%Total%Rece
我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions
我正在尝试使用Ruby中的SeleniumWebDriver2.4模拟鼠标移动如果我运行测试,是否应该看到鼠标在我的屏幕上移动?我很困惑。我试过很多不同的方法示例代码:require'selenium-webdriver'driver=Selenium::WebDriver.for:firefoxdriver.navigate.to'http://www.google.com'element=driver.find_element(:id,'gbqfba')那我试过了driver.action.move_to(element).performdriver.mouse.move_to(e
在控制台中,您可以像这样打印"\b"来删除光标左侧的字符(退格键)print"thelastcharisgoingtobeerased\b"#thelastcharisgoingtobeerased如何只向左移动一个位置而不是删除(向左箭头)? 最佳答案 这取决于终端类型和连接,但通常可以假定ANSI光标移动,因此光标向左是ESC+'['+'D':print"Thecursorshouldbebetweenthearrows:->参见http://ascii-table.com/ansi-escape-sequences.php获取
x={:name=>"John",:data=>{:physical=>{:age=>25,:weight=>150}}}我希望将数据的子属性向上移动一个级别(但不一定只是简单地展平所有属性)。在这种情况下,我基本上想将:physical属性“向上”移动一个级别。我正在尝试这个y=x[:data']y.each{|key|x[key]=y[key]}但是我得到...x=x.except(:data)=>{:name=>"John",[:physical,{:age=>25,:weight=>150}]=>nil}我在找...=>{:name=>"John",:physical=>{:a
我正在为我的网站使用MiddlemanBloggem,但默认情况下,博客文章似乎需要位于/source中,这在查看vim中的树时并不是特别好并尝试在其中找到其他文件之一(例如模板)。通过查看文档,我看不出是否有任何方法可以移动博客文章,以便将它们存储在其他地方,例如blog_articles文件夹或类似文件夹。这可能吗? 最佳答案 将以下内容放入您的config.rb文件中。activate:blogdo|blog|blog.permalink=":year-:month-:day-:title.html"blog.sources=
我的一个模型中有以下方法来保存用户记录:defsave_user(params)beginsave_user_details(params)rescueActiveRecord::RecordInvalid=>ex{success:false,errors:ex.messages}rescueException=>exRails.logger.info(ex.message)Rails.logger.info(ex.backtrace.join(‘\n’){success:false,errors:’Someerroroccurred.}endend我们可以看到rescueblock很
我最近一直在使用Compass和Sass来做一些CSSspriting,因为它非常有用。但是,文件名总是附加一个随机字符串。例如。图标-s5eb424578c.png。而且我不希望附加这个随机字符串,因为这意味着每次发生更改时我都需要上传新的CSS文件和新的Sprite图像。那么,有人知道Compassgem目录中哪个Ruby或其他配置文件附加了这个随机字符串吗?然后我可以只注释掉那个位的代码。除非我缺少官方变量,否则我可以在Compass中设置以告诉它我不想附加此字符串?提前感谢您对此提供的任何帮助。 最佳答案 尝试将这些行添加到