精灵工具包touch userInteractionEnabled = true

时间:2015-07-01 15:17:27

标签: ios

嗨我正在尝试将精灵叠加设置为水,alpha设置为0.3 和其他精灵一样下面的鱼,但我不能触摸鱼,因为水精灵吞下了触摸

来自这篇文章[iOS7 Sprite Kit how to disable touches on a sprite to make it "tap through"?
它在继续阅读子类化之后说要继承SKSpriteNode 我做了这个https://www.dropbox.com/s/mt067syvbvkmhjb/newClass.zip?dl=0 我看不出我哪里错了?任何帮助都会是好欢呼

我的GameScene.swift

import SpriteKit
class GameScene: SKScene
{
override func didMoveToView(view: SKView)
{
  let fishsprite  = fish(imageNamed: "fish")
    fishsprite.position = CGPoint(x: 512, y:350)
    fishsprite.zPosition = 1
    fishsprite.name = "fish"
    addChild(fishsprite)

    let watersprite  = water(imageNamed: "water")
   watersprite.position = CGPoint(x: 512, y: 360)
   watersprite.zPosition = 3
    watersprite.alpha = 0.3
    watersprite.name = "Water"
    addChild(watersprite)
}

}

我的水课

 import SpriteKit
 class water : SKSpriteNode
 {
required init(coder aDecoder: NSCoder)
 {
    fatalError("NSCoding not supported")
}

init(imageNamed: String) {
    let waterTexture = SKTexture(imageNamed: imageNamed)
    super.init(texture: waterTexture, color: nil, size: CGSize(width: 1024, height: 768))
    userInteractionEnabled = false // blocks the touches from everything
   // userInteractionEnabled = true //gets  the touches but stop fish getting them
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch in (touches as! Set<UITouch>)
    {

 //                      let location = touch.locationInNode(scene)
 //                   let touchedNode = nodeAtPoint(location)
 //                      println(touchedNode.zPosition)
 //     

    }
}

    override func touchesMoved(touches: Set<NSObject>, withEvent event:   UIEvent) {
    for touch in (touches as! Set<UITouch>)
    {

    }
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch in touches
    {

    }
}

}

我的鱼类

  import Foundation
  import SpriteKit
   class fish : SKSpriteNode
  {
    required init(coder aDecoder: NSCoder) {
    fatalError("NSCoding not supported")
}

init(imageNamed: String)
{
    let fishTexture = SKTexture(imageNamed: imageNamed)
    super.init(texture: fishTexture, color: nil, size: fishTexture.size())
    userInteractionEnabled = true
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch in (touches as! Set<UITouch>)
    {
        let location = touch.locationInNode(scene)
        let touchedNode = nodeAtPoint(location)
        println(touchedNode)
        zPosition = 15
        let liftUp = SKAction.scaleTo(1.2, duration: 0.2)
        runAction(liftUp, withKey: "pickup")
        let wiggleIn = SKAction.scaleXTo(1.0, duration: 0.2)
        let wiggleOut = SKAction.scaleXTo(1.2, duration: 0.2)
        let wiggle = SKAction.sequence([wiggleIn, wiggleOut])
        let wiggleRepeat = SKAction.repeatActionForever(wiggle)
        runAction(wiggleRepeat, withKey: "wiggle")
    }
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch in (touches as! Set<UITouch>)
    {
        let location = touch.locationInNode(scene)
        let touchedNode = nodeAtPoint(location)
        touchedNode.position = location
        touchedNode.zPosition = 15
        println(touchedNode)
    }
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch in touches
    {
        zPosition = 2
        let dropDown = SKAction.scaleTo(1.0, duration: 0.2)
        runAction(dropDown, withKey: "drop")
        removeActionForKey("wiggle")
    }
}
}      

1 个答案:

答案 0 :(得分:2)

嘿,我想出了一个小解决方法,

您想要点击的节点,

设置节点名称&#34; Overlay&#34;

现在这个节点从

下面的其他节点中脱颖而出

您可以在触摸开始时使用此代码

        for touch in touches {

           let location = touch.locationInNode(self)

           var allNodesTouched = nodesAtPoint(location)

           allNodesTouched.forEach({ (nodeTouched) in

               if nodeTouched.name == "Overlay" {

                   let I = allNodesTouched.indexOf(nodeTouched)
                   allNodesTouched.removeAtIndex(I!)
            }
        })

           if let selectedNode = allNodesTouched.first {

       }
    }

现在您可以使用selectedNode