我如何更好地定义一个区域来检测拖放节点?
How do i better define an area to detect a dragged and dropped node?
我正在尝试通过添加一个可以检测拖放节点的位置来扩展我从 Drag and drop example 研究的拖放示例。
从我尝试检测被丢弃的节点的过程中,我了解到丢弃一个节点并期望它精确地命中预定义的位置是很困难的。
如何定义一个位置或像一个更大的矩形来检测丢弃的节点?[=12=]
//: 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
}
我正在尝试通过添加一个可以检测拖放节点的位置来扩展我从 Drag and drop example 研究的拖放示例。
从我尝试检测被丢弃的节点的过程中,我了解到丢弃一个节点并期望它精确地命中预定义的位置是很困难的。
如何定义一个位置或像一个更大的矩形来检测丢弃的节点?[=12=]
//: 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
}