草庐IT

ios - SpriteKit touchesMoved 在 SKSpritenode 的子类中使用时不稳定

coder 2023-09-11 原文

我将 SKSpritenode(在 Swift 中)子类化以创建彩色 block ,然后可以在场景中拖动这些 block 。子类是 SoundNode。

import SpriteKit

class SoundNode: SKSpriteNode {

init() {
    super.init(texture: nil, color: UIColor.blue, size: CGSize(width: 100, height: 100))
    self.isUserInteractionEnabled = true

    }

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
    }



override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("touch began")
    }

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("touches moved")
    guard let touch = touches.first else {
        return
        }   
    let touchLocation = touch.location(in: self)
    self.position = touchLocation
    }


override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("touch ended")
    }


}

在 GameScene.swift 中

import SpriteKit

class GameScene: SKScene {

  override func didMove(to view: SKView) {
    addSoundBlock()
  }


  func addSoundBlock() {
    let soundBlock = SoundNode()
    soundBlock.position = CGPoint(x: 800, y: 800)
    addChild(soundBlock)
    }
}

这有点管用。

添加了 soundBlock,可以在场景中拖动。但它会闪烁,有时会消失。 我在 touchesMoved 中尝试了其他方法,但没有一种方法会影响抖动。

如果我不对 touchesBegan、touchesMoved、touchesEnded 进行子类化,并在 GameScene 中实现这些 Action ,那么拖动就会变得流畅。但 future 的计划取决于能否对这些进行子类化。

Xcode 8、Swift 3、iOS10

最佳答案

我创建了一个类方法来处理 SKSpriteNode 的子类的移动,在我的例子中是“Player”,并将触摸的位置传递给它在 GameScene.swift 中。

Player.swift 中的类方法声明:

    func movePlayer(location: CGPoint) {
        //movement code
    }

调用GameScene.swift中的函数:

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

         guard let location = touches.first?.location(in: self) else { return }
         player.movePlayer(location: location)

    }

我想更干净一些。并允许进一步子类化。

关于ios - SpriteKit touchesMoved 在 SKSpritenode 的子类中使用时不稳定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40793998/

有关ios - SpriteKit touchesMoved 在 SKSpritenode 的子类中使用时不稳定的更多相关文章

  1. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  2. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  3. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在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返回它复制的字节数,但是当我还没有下

  4. Ruby——嵌套类和子类是一回事吗? - 2

    下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby​​解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc

  5. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  6. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  7. Ruby - 如何处理子类意外覆盖父类(super class)私有(private)字段的问题? - 2

    假设您编写了一个类Sup,我决定将其扩展为SubSup。我不仅需要了解你发布的接口(interface),还需要了解你的私有(private)字段。见证这次失败:classSupdefinitialize@privateField="fromsup"enddefgetXreturn@privateFieldendendclassSub问题是,解决这个问题的正确方法是什么?看起来子类应该能够使用它想要的任何字段而不会弄乱父类(superclass)。编辑:equivalentexampleinJava返回"fromSup",这也是它应该产生的答案。 最佳答案

  8. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    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上

  9. ruby - 如何在 Cucumber 步骤定义中使单词可选? - 2

    我在下面有一个步骤定义,它执行我想要它执行的操作,即它根据“PAGES”哈希的“page”元素检查页面的url。Then(/^Ishould(still)?beatthe"(.*)"page$/)do|still,page|BROWSER.url.should==PAGES[page]end步骤定义用于两者我应该在...页面我应该还在...页面但是,我不需要将“still”传递到block中。我只需要它是可选的以匹配步骤但不传递到block中。我该怎么做?谢谢。 最佳答案 您想将“静止”组标记为非捕获。这是通过使用?:启动组来完成的

  10. ruby - 从 ruby​​ 调用时返回 shell 脚本的状态值? - 2

    我希望这些值匹配。当shell脚本由于某些错误条件而退出时(因此返回非零值),它们不匹配。壳$?返回1,ruby$?返回256。>>%x[lskkr]ls:kkr:Nosuchfileordirectory=>"">>puts$?256=>nil>>exitHadoop:~Madcap$lskkrls:kkr:NosuchfileordirectoryHadoop:~Madcap$echo$?1 最佳答案 在Ruby中$?是一个Process::Status实例。打印$?等同于调用$?.to_s,这等同于$?.to_i.to_s(来

随机推荐