Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift SKSpriteNode类未注册对游戏场景的触摸_Ios_Swift_Inheritance_Skspritenode - Fatal编程技术网

Ios Swift SKSpriteNode类未注册对游戏场景的触摸

Ios Swift SKSpriteNode类未注册对游戏场景的触摸,ios,swift,inheritance,skspritenode,Ios,Swift,Inheritance,Skspritenode,我有一个类,我想封装所有围绕触摸的逻辑。但是,我还想将触摸事件传递到游戏场景,以便移除其他节点。我想super.touchesbeated(touch,withEvent:event)可以工作,但没有骰子 我也尝试了委托模式,但我也无法实现,而且我真的不喜欢这种方法 import Foundation import SpriteKit class LevelSelect: SKSpriteNode { var level:Int = 0 init(x: CGFloat, y:

我有一个类,我想封装所有围绕触摸的逻辑。但是,我还想将触摸事件传递到
游戏场景
,以便移除其他节点。我想
super.touchesbeated(touch,withEvent:event)
可以工作,但没有骰子

我也尝试了
委托
模式,但我也无法实现,而且我真的不喜欢这种方法

import Foundation
import SpriteKit

class LevelSelect: SKSpriteNode {
    var level:Int = 0

    init(x: CGFloat, y: CGFloat) {
        // init code ...
    }

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

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        // logic affecting this class
        super.touchesBegan(touches, withEvent: event)
    }
}
<代码>导入基础 进口SpriteKit 类别级别选择:SKSpriteNode{ 变量级别:Int=0 init(x:CGFloat,y:CGFloat){ //初始化代码。。。 } 必需的初始化(编码器aDecoder:NSCoder){ fatalError(“初始化(编码者:)尚未实现”) } 覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){ //影响这个类的逻辑 super.touchsbegind(touchs,withEvent:event) } }
SKSpriteNode的超类(super.touchs…)不是SKScene

另外,当您将touch方法添加到SKSpriteNode子类时,它们仅在实际节点被触摸而不是场景时才会激发。它通常用于button子类之类的东西

如果这是您想要的,您可以尝试将触摸直接转发到场景。每个SKSpriteNode都有一个可选的场景属性(在将精灵添加到场景之前为零),该属性允许您访问节点所在的场景

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
      // logic affecting this class
      scene?.touchesBegan(touches, withEvent: event)
}
一个更简单的方法是在LevelSelect类中为所有需要完成的事情创建方法,例如

 class LevelSelect: SKSpriteNode {
     var level = 0

     init(x: CGFloat, y: CGFloat) {
        // init code ...
     }

     // Started touches
     func didStartTouches() {
        // do something
     }
}
在游戏场景中,您可以使用创建的属性初始化该类并调用该方法


希望这能有所帮助

您是想让一个节点在触摸时做些什么,还是在定位手指位置时遇到困难?这正好回答了我的问题!非常感谢。接下来,有没有一种方法可以调用该
SKScene
中的方法?例如,我尝试了
scene?.removeAllLabels()
,但它没有访问权限。另外,我希望我可以再次为用户名投票:DHey,是的,您可以调用一个方法,您需要将scene属性强制转换到您的游戏场景。我更新了我的答案。是的!我喜欢互联网。谢谢你的帮助!
 class LevelSelect: SKSpriteNode {
     var level = 0

     init(x: CGFloat, y: CGFloat) {
        // init code ...
     }

     // Started touches
     func didStartTouches() {
        // do something
     }
}
 class GameScene: SKScene {

   levelSelect = LevelSelect(x: ..., y: ...)

   ...

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
          // logic affecting this class
          levelSelect.didStartTouches()
   }
}
 class LevelSelect: SKSpriteNode {

     ...

     func callSomeMethodInGameScene() {
        let gameScene = scene as? GameScene // dont use as!, always use optionals (as?) when doing the as casting
        gameScene?.removeAllLabels()

     }
 }