I'm using spritekit, using on touch to detect what the users has tapped, ie., no collisions, etc.
I have an array of sprite nodes which in effect is a multi-state boolean switch for now the user can only select one node to be active at a time so the logic looks like:
Node A, Node B, Node C .........Node M
off off off initial
on off off user touches node A
off off on user touches node C
This code is my working template and would like to reduce a lot of this boilerplate down to something reasonable.
I'm looking for ways that are either faster, more the "Apple Way", more OOP.
var temp1:SKSpriteNode = SKSpriteNode()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
if let touch = touches.first
{
let tappedNodes:[sk_Node] = nodes(at: touch.location(in: self) )
for node in tappedNodes
{
switch node.name!
{
case "level_1":
pushd(node: node)
node.run( sk_Action.animate(with: [sk_Texture(imageNamed: "level_1_selected") ], timePerFrame: K1_4.f64_t) )
temp1 = childNode(withName: "level_2")!
temp1.run(sk_Action.animate(with: [sk_Texture(imageNamed: "level_2_normal") ], timePerFrame: K1_4.f64_t) )
popd(node:temp1)
temp1 = SKSpriteNode()
temp1 = childNode(withName: "level_3")!
temp1.run( sk_Action.animate(with: [sk_Texture(imageNamed: "level_3_normal") ], timePerFrame: K1_4.f64_t) )
popd(node:temp1)
case "level_2":
pushd(node: node)
node.run( sk_Action.animate(with: [sk_Texture(imageNamed: "level_2_selected") ], timePerFrame: K1_4.f64_t) )
temp1 = childNode(withName: "level_1")!
temp1.run( sk_Action.animate(with: [sk_Texture(imageNamed: "level_1_normal") ], timePerFrame: K1_4.f64_t) )
popd(node:temp1)
temp1 = SKSpriteNode()
temp1 = childNode(withName: "level_3")!
temp1.run( sk_Action.animate(with: [sk_Texture(imageNamed: "level_3_normal") ], timePerFrame: K1_4.f64_t) )
popd(node:temp1)
case "level_3":
pushd(node: node)
node.run( sk_Action.animate(with: [sk_Texture(imageNamed: "level_3_selected") ], timePerFrame: K1_4.f64_t) )
temp1 = childNode(withName: "level_2")!
temp1.run( sk_Action.animate(with: [sk_Texture(imageNamed: "level_2_normal") ], timePerFrame: K1_4.f64_t) )
popd(node:temp1)
temp1 = SKSpriteNode()
temp1 = childNode(withName: "level_1")!
temp1.run( sk_Action.animate(with: [sk_Texture(imageNamed: "level_1_normal") ], timePerFrame: K1_4.f64_t) )
popd(node:temp1)
} } } }
pushd
and popd
are SKActions that do some simple scaling up and down.