Ios 检测SKNode上的触摸(swift)
我已经创建了一个容器节点,将所有需要在一次触摸中移动的SKSpriteNode放入其中,在iOS 8中,我可以正常检测到它们上的触摸,但在iOS 7中,我只能检测到主节点上的触摸,当我触摸SKSpriteNode时,它在容器节点中,什么都不会发生。。我怎样才能解决这个问题Ios 检测SKNode上的触摸(swift),ios,sprite-kit,sknode,child-nodes,Ios,Sprite Kit,Sknode,Child Nodes,我已经创建了一个容器节点,将所有需要在一次触摸中移动的SKSpriteNode放入其中,在iOS 8中,我可以正常检测到它们上的触摸,但在iOS 7中,我只能检测到主节点上的触摸,当我触摸SKSpriteNode时,它在容器节点中,什么都不会发生。。我怎样才能解决这个问题 let lvlsNode = SKNode() override func didMoveToView(view: SKView) { self.addChild(lvlsNode)
let lvlsNode = SKNode()
override func didMoveToView(view: SKView) {
self.addChild(lvlsNode)
axe = SKSpriteNode(imageNamed:"axe")
axe.anchorPoint = CGPointMake(1, 0)
axe.size = CGSizeMake(axe.size.width/1.4, axe.size.height/1.4)
axe.position = CGPointMake(0+screenWidth/7, shield.position.y-shield.size.width*1.4)
axe.zPosition = 12
axe.name = "axe"
lvlsNode.addChild(axe)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
if node.name == "axe" {
// do something.... this work in iOS8 but not in iOS 7.1
}
让lvlsNode=SKNode()
覆盖func didMoveToView(视图:SKView){
self.addChild(lvlsNode)
axe=SKSpriteNode(图像名称:“axe”)
axe.ancorpoint=CGPointMake(1,0)
axe.size=CGSizeMake(axe.size.width/1.4,axe.size.height/1.4)
axe.position=CGPointMake(0+屏幕宽度/7,屏蔽.位置.y-屏蔽.尺寸.宽度*1.4)
axe.zPosition=12
axe.name=“axe”
lvlsNode.addChild(axe)
}
覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
/*当触摸开始时调用*/
用于触摸输入(触摸设置为){
let location=touch.locationInNode(自)
让节点=节点点(位置)
如果node.name==“axe”{
//做点什么……这在iOS 8中可以工作,但在iOS 7.1中不行
}
Yournode.name=“nodeX”
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
让触摸=触摸。先触摸!
如果atPoint((touch?.location(in:self))!).name==Yournode.name{
//你的代码
}
}
记录节点时会发生什么?是否为空?在“let node=nodeAtPoint(location)”行下方放置一个断点,然后检查节点。它说什么?如果我打印(node.name),它会向我显示它在axe后面的SKSpriteNode对于容器节点OK中的每个SpriteNode,我都得到nil,这是因为您实际上正在按该节点。它是什么类型的节点?您可以将斧头附加到该节点上吗?如果可以,您可以检查node.name==nil,然后您可以检查node.child/parent.name,看看这是否是斧头名称
Yournode.name = "nodeX"
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first as UITouch!
if atPoint((touch?.location(in: self))!).name == Yournode.name {
//Your code
}
}