gpt4 book ai didi

ios - Sprite 套件触摸 userInteractionEnabled = true

转载 作者:行者123 更新时间:2023-12-02 05:16:30 25 4
gpt4 key购买 nike

嗨,我正在尝试将 Sprite 覆盖设置为水,并将 alpha 设置为 0.3下面的其他 Sprite 都是鱼,但我无法触摸鱼,因为水 Sprite 吞下了触摸

来自这篇文章[ 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")
}
}
}

最佳答案

嘿,我想出了一些解决方法,

无论您想点击哪个节点,

设置节点名称“Overlay”

现在这个节点从下面的其他节点中脱颖而出

您可以在touched begin中使用此代码

        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

关于ios - Sprite 套件触摸 userInteractionEnabled = true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31165355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com