我的游戏中有一个虚拟控制板。如果我点击并按住其中一个方向按钮,播放器会朝正确的方向移动并显示正确的图像和动画。但是,如果我将手指从一个控件滑动到另一个控件(比如从右到下),播放器会向下移动,但图像和动画不会改变,直到我抬起手指。下面是我处理触摸的代码:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches as! Set<UITouch>) {
let location = touch.location(in: cameraNode)
if upRect.contains(location) {
moveXDirection = 0
moveYDirection = 1
isIdle = false
playerSprite.walkUpPlayer()
} else if rightRect.contains(location) {
moveXDirection = 1
moveYDirection = 0
isIdle = false
playerSprite.walkLRPlayer()
} else if downRect.contains(location) {
moveXDirection = 0
moveYDirection = -1
isIdle = false
playerSprite.walkDownPlayer()
} else if leftRect.contains(location) {
moveXDirection = -1
moveYDirection = 0
isIdle = false
playerSprite.walkLRPlayer()
}
if !isIdle && moveXDirection != 0 {
playerSprite.xScale = moveXDirection
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches as! Set<UITouch>) {
let location = touch.location(in: cameraNode)
if upRect.contains(location) {
moveXDirection = 0
moveYDirection = 1
isIdle = false
} else if rightRect.contains(location) {
moveXDirection = 1
moveYDirection = 0
isIdle = false
} else if downRect.contains(location) {
moveXDirection = 0
moveYDirection = -1
isIdle = false
} else if leftRect.contains(location) {
moveXDirection = -1
moveYDirection = 0
isIdle = false
}
if !isIdle && moveXDirection != 0 {
playerSprite.xScale = moveXDirection
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
idlePlayer()
}
我改变播放器动画的代码是:
func idlePlayer() {
removeAllActions()
}
func walkLRPlayer() {
removeAllActions()
run(SKAction.repeatForever(walkLRAnimation!))
}
func walkUpPlayer() {
removeAllActions()
run(SKAction.repeatForever(walkUpAnimation!))
}
func walkDownPlayer() {
removeAllActions()
run(SKAction.repeatForever(walkDownAnimation!))
}
如有任何建议,我们将不胜感激。
最佳答案
您的播放器从 touchesBegan 中的方向开始,并在 touchesEnded 中停止。问题是您没有处理 touchesMoves 上的方向变化。
让我们创建一个全局变量,它会告诉我们是否改变了方向。
var previous_direction : Int = -1 // idle state
在触摸功能上,添加 previous_direction 行为。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches as! Set<UITouch>) {
// we start with previous_direction being the start direction
if upRect.contains(location) {
previous_direction = 0 // up
} else if rightRect.contains(location) {
previous_direction = 1 // right
} else if downRect.contains(location) {
previous_direction = 2 // down
} else if leftRect.contains(location) {
previous_direction = 3 // left
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches as! Set<UITouch>) {
// we create a current_direction also
var current_direction : Int = -1
// get current_direction
if upRect.contains(location) {
current_direction = 0 // up
} else if rightRect.contains(location) {
current_direction = 1 // right
} else if downRect.contains(location) {
current_direction = 2 // down
} else if leftRect.contains(location) {
current_direction = 0 // left
}
// now we compare if the current_direction is different from previous_direction
if current_direction != previous_direction
{
player.removeAllActions()
// attribute new direction
if current_direction == 0 {
playerSprite.walkUpPlayer()
} else if current_direction == 1 {
walkLRPlayer.walkLRPlayer()
} else if current_direction == 2 {
walkLRPlayer.walkDownPlayer()
} else if current_direction == 3 {
walkLRPlayer.walkLRPlayer() }
// we prepare previous_direction for the next time he enters this function
previous_direction = current_direction
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// reset previous_direction
previous_direction = - 1 // idle state
}
关于ios - Spritekit 动画没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54227703/
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
这里有一个很好的答案解释了如何在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”结果的
大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle
我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r
如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象
Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u