gpt4 book ai didi

swift - 我如何更好地定义一个区域来检测拖放节点?

转载 作者:行者123 更新时间:2023-11-28 13:40:33 27 4
gpt4 key购买 nike

我正在尝试扩展我从 Drag and drop example 学习的拖放示例通过添加一个可以检测丢失节点的位置。

从我尝试检测被丢弃的节点的过程中,我了解到丢弃一个节点并期望它精确地命中预定义的位置是很困难的。

如何定义一个位置或像一个更大的矩形来检测丢弃的节点?

//: A SpriteKit based Playground

import PlaygroundSupport
import SpriteKit

class GameScene: SKScene {
private var currentNode: SKNode?

override func didMove(to view: SKView) {
let node = SKSpriteNode(
color: .red,
size: CGSize(width: 50, height: 50)
)
node.name = "draggable"
self.addChild(node)

let blueNode = SKSpriteNode(
color: .blue,
size: CGSize(width: 50, height: 50)
)
blueNode.name = "draggable"
self.addChild(blueNode)

let greenNode = SKSpriteNode(
color: .green,
size: CGSize(width: 50, height: 50)
)
greenNode.name = "droppable"
greenNode.position = CGPoint(x: 100, y: 100)
self.addChild(greenNode)

}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)

let touchedNodes = self.nodes(at: location)
for node in touchedNodes.reversed() {
if node.name == "draggable" {
self.currentNode = node
}
}
}
}
@objc static override var supportsSecureCoding: Bool {
// SKNode conforms to NSSecureCoding, so any subclass going
// through the decoding process must support secure coding
get {
return true
}
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

if let touch = touches.first, let node = self.currentNode {
let touchLocation = touch.location(in: self)
node.position = touchLocation
}

}

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

print(self.currentNode!.position);

if self.currentNode!.position == CGPoint(x:100,y:100) {
let yellowNode = SKSpriteNode(
color: .yellow,
size: CGSize(width: 50, height: 50)
)
yellowNode.name = "droppable"
yellowNode.position = CGPoint(x: 200, y: 200)
self.addChild(yellowNode)
}
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
self.currentNode = nil
}

override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}

// Load the SKScene from 'GameScene.sks'
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 640, height: 480))
if let scene = GameScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill

// Present the scene
sceneView.presentScene(scene)
}

PlaygroundSupport.PlaygroundPage.current.liveView = sceneView

最佳答案

我假设您在 touchesEnded(...) 函数中寻求建议。

你是对的:像在

中一样检查精确位置
if self.currentNode!.position == CGPoint(x:100,y:100) {}

不会带你走得太远。手动点击屏幕上的特定点非常困难。

例如,您可以使用 CGRect 指定目标区域,然后检查触摸位置是否位于矩形内。

let targetRect = CGRect(x: 0, y: 0, width: 200, height: 200)
guard let node = self.currentNode else {return} // Just to avoid forced unwrapping
if targetRect.contains(node.position) {
// Do stuff
}

关于swift - 我如何更好地定义一个区域来检测拖放节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56091099/

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