ios - 带有overlayskscene Xcode Swift 的屏幕触摸Scenekit 导致应用程序崩溃

标签 ios xcode swift sprite-kit scenekit

嘿,好吧,在 Overlayskce 的场景套件 3D 游戏中识别 HUD 触摸的最佳方法是什么。因为我有一个名为“AButton”的按钮,但是当我触摸按钮或屏幕时,游戏会在几个小时的搜索后崩溃,我猜问题是场景套件上的 touchbegin 不太配合。但是我该如何做到这一点,以便当用户触摸 HUD 上的按钮时,它不会使系统崩溃并且它实际上可以工作。你能看看我的代码并重写它吗?我是使用触摸开始还是使用 HitTest 或其他东西(我以前从未使用过 HitTest )?

handleTap:]: 无法识别的选择器发送到实例 0x14d547710' *** 首先抛出调用堆栈: (0x1868042d8 0x1980300e4

代码:

 import iAd
 import UIKit
 import GameKit
 import SceneKit
 import StoreKit
 import SpriteKit
 import QuartzCore
 import Foundation
 import AVFoundation
 import AudioToolbox

  //============================================================
 class GameViewController: UIViewController, ADBannerViewDelegate, SKPhysicsContactDelegate, SKSceneDelegate, SCNSceneRendererDelegate, SCNPhysicsContactDelegate{


   let FieldScene = SCNScene(named: "art.scnassets/TesingCampusField.dae")!

let GuyScene = SCNScene(named: "art.scnassets/Guy.dae")!
//-------------------HUD-SetUp-------------------------------------------------------
let overlayScene = SKScene(size: CGSizeMake(100, 100))
override func viewDidLoad() {
    super.viewDidLoad()

    let scnView = self.view as! SCNView
    scnView.overlaySKScene = overlayScene
    scnView.backgroundColor = UIColor.whiteColor()
    scnView.scene = FieldScene
    scnView.delegate = self
    scnView.overlaySKScene!.delegate = self
    scnView.overlaySKScene!.anchorPoint = CGPointMake(0, 0)
    scnView.overlaySKScene!.physicsWorld.contactDelegate = self
    scnView.overlaySKScene!.physicsWorld.gravity = CGVectorMake(0.0, 0.0)
    scnView.allowsCameraControl = false
    scnView.showsStatistics = false

    let Guy1: SCNNode = GuyScene.rootNode.childNodeWithName("Bob_014", recursively: true)!
    FieldScene.rootNode.addChildNode(Guy1)

    ButtonA.size = CGSize(width: 6, height: 9)
    ButtonA.anchorPoint = CGPointMake(-13.3, -0.5)
    ButtonA.zPosition = 0
    overlayScene.addChild(ButtonA)

    let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:")
    scnView.addGestureRecognizer(tapGesture)
    //--------------------------
    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    FieldScene.rootNode.addChildNode(cameraNode)
    cameraNode.position = SCNVector3(x: 0, y: 5, z: 15)
    //-----------------------------------------------
    let lightNode = SCNNode()
    lightNode.light = SCNLight()
    lightNode.light!.type = SCNLightTypeOmni
    lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
    FieldScene.rootNode.addChildNode(lightNode)
    //-----------------------------------------------
    let ambientLightNode = SCNNode()
    ambientLightNode.light = SCNLight()
    ambientLightNode.light!.type = SCNLightTypeAmbient
    ambientLightNode.light!.color = UIColor.darkGrayColor()
    FieldScene.rootNode.addChildNode(ambientLightNode)
    //----------------------------------------------
}
    func AButtonPressed() {
    let AButtonPressed = SKTexture(imageNamed: "GreenAButtonPressed")
    let OrignalButtonA = SKTexture(imageNamed:"GreenAButton")
    let AButtonPressedAnimation = SKAction.animateWithTextures([AButtonPressed, OrignalButtonA], timePerFrame: 0.2)
    let RunAButtonPressedAnimation = SKAction.repeatAction(AButtonPressedAnimation, count: 1)
    ButtonA.runAction(RunAButtonPressedAnimation)
    print("finishedpressing")
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {
        let location1 = touch.locationInNode(self.overlayScene)
        if self.overlayScene.nodeAtPoint(location1) == self.ButtonA {
            AButtonPressed()
            print("AButtonPressed")
        }
    }

最佳答案

您的手势识别器配置为在用户点击 View 时调用 handleTap: 方法,但您没有定义该方法。

func handleTap(gestureRecognizer: UITapGestureRecognizer) {
    // do something
}

如果您想直接使用touchesBegan,则不需要手势识别器。

关于ios - 带有overlayskscene Xcode Swift 的屏幕触摸Scenekit 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37694340/

相关文章:

ios - 第二次选择 UITabBarItem 时关闭之前的 ViewController

ios - 由于关闭而导致对开始/结束外观转换的不平衡调用 - 不涉及任何转换或转场

ios - 如何多次显示游戏中心验证登录提醒?

iphone - 在 iOS 上,为什么更改 CALayer 的帧不会导致动画?

ios - UILabel textRectForBounds 不工作

javascript - 如何辨别 UIWebView 中堆叠的 HTML 元素上的触摸事件

ios - 为什么 Xcode 不识别 PFFacebookUtils?

ios - 在 downloadTask 完成后执行 segue

ios - 使用 CIFilter 围绕中心旋转 CIImage

objective-c - 在第二个 ViewController 上显示工具栏