如何检测哪个 SKSpriteNode 已被触摸

编程入门 行业动态 更新时间:2024-10-19 17:18:56
本文介绍了如何检测哪个 SKSpriteNode 已被触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我发现了一个类似的问题,但我试图检测和识别用户触摸的精灵,但我不知道如何做到这一点.这是我的变量:

I find a similar question, but I am trying to detect and identify which Sprite the user touch, and I don't know how to do that. This is my variable:

var sprites: [[SKSpriteNode]] = [[SKSpriteNode(imageNamed: "a"), SKSpriteNode(imageNamed: "b")], [SKSpriteNode(imageNamed: "c"),SKSpriteNode(imageNamed: "d")]]

这个想法是识别 spriteNode,然后将其替换为其他 sprite 或更改颜色,但我不知道如何使用这个 spriteNodes 矩阵来做到这一点,我猜第一步是识别 sprite.

The idea is identify the spriteNode and then replace it for other sprite or change the color, but I don´t know how to do it with this matrix of spriteNodes, I guess the first step it´s identify the sprite.

推荐答案

可以使用 委托模式.基本上,您将告诉您的委托(场景,或您设置为委托的任何内容)为您做一些事情,您将直接从按钮的 touchesBegan 方法中执行此操作.此外,您还将按钮的名称传递给场景.

What you are trying to do (even if I don't see a reason for this) can be accomplished using delegation pattern. Basically, you will tell your delegate (the scene, or whatever you set as a delegate) to do something for you, and you will do that directly from within the button's touchesBegan method. Also, you will pass the button's name to a scene.

要实现这一点,首先您必须定义一个名为 ButtonDelegate 的协议.该协议定义了一个要求,规定任何符合要求的类都必须实现一个名为 printButtonsName(_:):

To make this happen, first you have to define a protocol called ButtonDelegate. That protocol defines a requirement which states that any conforming class has to implement a method called printButtonsName(_:):

protocol ButtonDelegate:class { func printButtonsName(name:String?) }

这是将在您的 GameScene 类中实现的方法,但在按钮的 touchesBegan 中调用.此外,此方法将用于将按钮的名称传递给其代理(场景),因此您将始终知道点击了哪个按钮.

This is the method which will be implemented in your GameSceneclass, but called from within button's touchesBegan. Also, this method will be used to pass a button's name to its delegate (scene), so you will always know which button is tapped.

接下来是按钮类本身.Button 可能看起来像这样:

Next thing is button class itself. Button might look like this:

class Button : SKSpriteNode{ weak var delegate:ButtonDelegate? init(name:String){ super.init(texture: nil, color: .purpleColor(), size: CGSize(width: 50, height: 50)) self.name = name self.userInteractionEnabled = true } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { delegate?.printButtonsName(self.name) } }

这里重要的是userInteractionEnabled = true,这意味着按钮将接受触摸.另一个重要的事情是 delegate 属性.如前所述,按钮会将场景设置为它们的代理.当我们创建一些按钮时,将在稍后完成将场景设置为按钮的委托......为了使您更容易,将委托视为为他的老板工作的工人:) 老板(一个按钮)告诉他的工人(一个场景)为他做某事(打印他的名字).

The important thing here is userInteractionEnabled = true, which means that button will accept touches. Another important thing is a delegate property. As already mentioned, buttons will have the scene set as their delegate. Setting a scene as delegate of buttons will be done later when we create some buttons... To make this easier for you, think of a delegate as a worker who works for his boss :) The boss (a button) tells his worker (a scene) to do something for him (to prints his name).

好的,让我们确保场景符合 ButtonDelegate 协议......为什么这很重要?这很重要,因为工人(场景)必须听从老板的命令(一个按钮).通过遵守这个协议,工人正在与他的老板签订合同,确认他知道如何做他的工作并且会听从他的命令:)

Okay, so lets make sure that scene conforms to a ButtonDelegate protocol...Why is this important? It is important because the worker (scene) must follow the orders of his boss (a button). By conforming to this protocol, the worker is making a contract with his boss where confirming that he knows how to do his job and will follow his orders :)

class GameScene: SKScene, ButtonDelegate { override func didMoveToView(view: SKView) { let play = Button(name:"play") play.delegate = self let stop = Button(name:"stop") stop.delegate = self play.position = CGPoint(x: frame.midX - 50.0, y: frame.midY) stop.position = CGPoint(x: frame.midX + 50.0, y: frame.midY) addChild(play) addChild(stop) } func printButtonsName(name: String?) { if let buttonName = name { print("Pressed button : (buttonName) ") } //Use switch here to take appropriate actions based on button's name (if you like) } }

就是这样.当您点击播放按钮时,按钮本身的 touchesBegan 将被调用,然后按钮将告诉其委托使用场景类中定义的方法打印其名称.

And that's it. When you tap the play button, the touchesBegan on a button itself will be called, then the button will tell its delegate to print its name using the method defined inside of scene class.

更多推荐

如何检测哪个 SKSpriteNode 已被触摸

本文发布于:2023-11-27 09:14:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637459.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:已被   SKSpriteNode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!