I have a spritekit scene and added some sequence of actions to one of it's spriteSceneNode. Say for example,a spriteSceneNode called node1 moving to point1 then it will move to point2 then point3 etc. Its implemented by using "SKAction.moveTo" function.
My question is,
Is it possible to call a custom function when it reaches on each points (point1 or point2 or point3).?
Adding some code here.
func MoveObjectToAnotherPosition(arrayOfPoints : [CGPoint],object: SKSpriteNode ) {
let from = object.position
var curPoint1 = object.position
let move = SKAction.move(to: from ,duration: 0.5)
var arrayOfMove : [SKAction] = []
arrayOfMove.append(move)
for point in (arrayOfPoints ) {
let move2 = SKAction.move(to: point,duration: 2.5)
let deltaX = point.x - curPoint1.x
let deltaY = point.y - curPoint1.y
let angle = atan2(deltaY, deltaX)
// let graph = childNode(withName: "Graph1")?
print(angle)
let ang = point.angle(to: curPoint1)
print(ang)
let codeToRunWhenReachingPointX = SKAction.run {
let anglevalue = angle + 90 * self.DegreesToRadians
self.movObject.zPosition = anglevalue
print(anglevalue)
}
//print(anglevalue)
let rotate = SKAction.rotate(byAngle: -angle, duration: 0.0)
arrayOfMove.append(codeToRunWhenReachingPointX)
arrayOfMove.append(move2)
curPoint1 = point
}
let moveToSequence = SKAction.sequence(arrayOfMove)
object.run(moveToSequence)
}
Any help?