点击手势识别器for overlayScene无法在Swift Xcode中使用

时间:2016-06-08 05:58:45

标签: ios xcode swift scnlayer

嘿,我有一个HUD的叠加场景。我无法识别正在触摸叠加层场景上的按钮。所以我决定放入一个轻敲手势识别器,现在每次触摸屏幕都会崩溃。我认为这是因为没有" handleTap:"但现在问题是我不知道该怎么做才能使用#Tap;"。我在SKSCENE。使应用程序没有触及覆盖场景

代码:

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{

var ButtonA = SKSpriteNode(imageNamed:"GreenAButton")

let overlayScene = SKScene(size: CGSizeMake(100, 100))


override func viewDidLoad() {
    super.viewDidLoad()

    // Making basic overlayScene and all its property's
    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
    scnView.playing = true

    let tapGesture = UITapGestureRecognizer(target: self, action: Selector("touchesBegan:"))
    scnView.addGestureRecognizer(tapGesture)


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



   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)

}

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")
        }
    }
   }


    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch: AnyObject in touches  {
        let location = touch.locationInNode(self.overlayScene)
        if self.overlayScene.nodeAtPoint(location) == self.ButtonA || self.overlayScene.nodeAtPoint(location) == self.ButtonO || self.overlayScene.nodeAtPoint(location) == self.ButtonY || self.overlayScene.nodeAtPoint(location) == self.ButtonSquare {
            print(" Button Pressed")

        } else {
        let move:SKAction = SKAction.moveTo(base.position, duration: 0.05)
        move.timingMode = .EaseOut

        ball.runAction(move)
   }
  }
}

0 个答案:

没有答案