for the click response of the object element in the scene, we can process it uniformly within the Touchesbegan () method of the scene.
Skscene in Touchesbegan () is a way to respond to a screen click, where we can first get all the objects under the click, and then filter out the desired object and then call the method of the object.
123456789101112131415161718192021222324252627 |
import SpriteKit
class GameScene
:
SKScene {
//当切换到这个场景视图后
override func didMoveToView(view:
SKView
) {
}
//响应屏幕点击的方法
override func touchesBegan(touches:
NSSet
, withEvent event:
UIEvent
) {
for touch:
AnyObject in touches{
//获取点击的坐标
let location = touch.locationInNode(
self
)
//该坐标下所有的对象
var arrObject =
self
.nodesAtPoint(location)
//找出为HanggePoint类型的对象
for p
in arrObject {
let point = p
as
?
HanggePoint
//如果该对象不为nil,就调用该对象方法
if point !=
nil {
point.onTouch()
}
}
}
}
}
|
Swift-Get screen click coordinates under All objects (Spritekit game development)