Hi,
In Chapter 21 in Coded actions we do:
var driveLeftAction: SCNAction!
var driveRightAction: SCNAction!
driveLeftAction = SCNAction.repeatForever(SCNAction.move(by: SCNVector3Make(-2.0, 0, 0), duration: 1.0))
driveRightAction = SCNAction.repeatForever(SCNAction.move(by: SCNVector3Make(2.0, 0, 0), duration: 1.0))
for node in trafficNode.childNodes {
// 3 Let vehicle drive towards its facing direction
if node.eulerAngles.y > 0 {
node.runAction(driveLeftAction)
} else {
node.runAction(driveRightAction)
}
}
As SCNAction is a subclass of NSObject and it is reference type, so each
node.runAction(driveLeftAction)
will execute the same SCNAction instance.
The question is why when we change speed, we change it for instance used all nodes of the one side:
if node.name?.contains("Bus") == true {
driveLeftAction.speed = 1.0
driveRightAction.speed = 1.0
} else {
driveLeftAction.speed = 2.0
driveRightAction.speed = 2.0
}
It is not changed for both bus and cars?