【发布时间】:2019-05-11 14:09:53
【问题描述】:
我正在尝试扩展我从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
【问题讨论】:
标签: swift sprite-kit swift-playground