Nodeをタッチした際に動作させたいアクションがあるのですが、Nodeのタッチを検出致しません。
情報不足ございましたらお申し付けください。
【実現したい事】
class WorldNodeでは画面をスクロール動作する為のコードを記載しています。
class GameScene内のtouchesEndedでclass WorldNodeのcontentNodeをタッチした際に、class GameScene内のメソッドを動作させたいです。
逆にclass WorldNode内のtouchesEndedでclass GameSceneのメソッドを動作させる方法でも良いかなと思ってます。
・タッチしたいNode:class WorldNodeのcontentNode
var viewSizeWidth: Int = 375
var viewSizeHeight: Int = 812
class GameScene: SKScene, SKPhysicsContactDelegate {
var battleship: SKSpriteNode!
let worldNode = WorldNode(size: CGSize(width: viewSizeWidth * 8, height: viewSizeHeight * 8))
override func didMove(to view: SKView) {
self.addChild(worldNode)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let location = touch!.location(in: self)
//ここにclass WorldNode内のcontentNodeをタッチした際、class GameScene内のメソッドを動作させたい
}
//このメソッドをタッチイベントで使用したい。
func addBattleship() {
let battleship = SKSpriteNode(imageNamed: "battleship")
self.battleship = battleship
battleship.position = CGPoint(x: 0, y: 0)
battleship.size = CGSize(width: 50, height: 50)
battleship.zPosition = 2.0
self.worldNode.contentNode.addChild(battleship)
let move = SKAction.moveTo(y: 2700, duration: 10)
battleship.run(move)
}
}
//WorldNodeでは、画面をtouchesMovedでスクロールする為のコードを書いています。
class WorldNode: SKSpriteNode {
var contentNode = SKNode()
private var startX: CGFloat = 0.0
private var lastX: CGFloat = 0.0
private var startY: CGFloat = 0.0
private var lastY: CGFloat = 0.0
init(size: CGSize) {
super.init(texture: nil, color: SKColor.gray, size: size)
self.isUserInteractionEnabled = true
self.contentNode.position = CGPoint(x: 0, y: 0)
self.contentNode.name = "contentNode"
let ConstraintYRange = SKRange (lowerLimit: self.frame.minY + 500, upperLimit: self.frame.maxY - 500)
let ConstraintXRange = SKRange (lowerLimit: self.frame.minX + self.frame.maxX / 2, upperLimit: self.frame.maxX - self.frame.maxX / 2)
let yconst = SKConstraint.positionY(ConstraintYRange)
let xconst = SKConstraint.positionX(ConstraintXRange)
contentNode.constraints = [yconst, xconst]
self.addChild(contentNode)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let location = touch!.location(in: self)
startX = location.x
lastX = location.x
startY = location.y
lastY = location.y
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let location = touch!.location(in: self)
//ここにclass GameSceneのメソッドを動作させてもOK?(GameSceneのメソッドを引っ張る方法分かりません)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch: UITouch = touches.first!
let location = touch.location(in: self)
let currentX = location.x
let currentY = location.y
let scrollspeed: CGFloat = 1.0
let newX = self.contentNode.position.x + ((currentX - lastX) * scrollspeed)
let newY = self.contentNode.position.y + ((currentY - lastY) * scrollspeed)
let limitFactir: CGFloat = 100
let leftLimitX = self.size.width * (-limitFactir)
let rightLimitX = self.size.width * limitFactir
let lowLimitY = self.size.height * (-limitFactir)
let heightLimitY = self.size.height * limitFactir
if newX < leftLimitX && newY < lowLimitY {
self.contentNode.position = CGPoint(x: leftLimitX, y: self.contentNode.position.y)
self.contentNode.position = CGPoint(x: self.contentNode.position.x, y: lowLimitY)
} else if newX > rightLimitX && newY > heightLimitY {
self.contentNode.position = CGPoint(x: rightLimitX, y: contentNode.position.y)
self.contentNode.position = CGPoint(x: self.contentNode.position.y, y: heightLimitY)
} else {
self.contentNode.position = CGPoint(x: newX, y: self.contentNode.position.y)
self.contentNode.position = CGPoint(x: self.contentNode.position.x, y: newY)
}
lastX = currentX
lastY = currentY
}
}