-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTargetNode.swift
65 lines (56 loc) · 1.92 KB
/
TargetNode.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// TargetNode.swift
// Shooting Game
//
// Created by Radu Istrati on 10.05.20.
// Copyright © 2020 Radu Istrati. All rights reserved.
//
import UIKit
import SpriteKit
class Target: SKNode {
var targetNode: SKSpriteNode!
var targetSpeed: Float = 1.0
var targetPoints = 0
func setTarget(row: Int) {
let yPosition = (200 * row) + 8
if Int.random(in: 0...2) != 0 {
targetNode = SKSpriteNode(imageNamed: "Red")
targetNode.name = "red"
if Int.random(in: 1...2) == 1 {
targetNode.setScale(CGFloat.random(in: 0.23...0.30))
targetSpeed = Float.random(in: 1.0...2.0)
targetPoints = 3
} else {
targetNode.setScale(CGFloat.random(in: 0.45...0.55))
targetSpeed = Float.random(in: 0.6...1.2)
targetPoints = 2
}
} else {
targetNode = SKSpriteNode(imageNamed: "Green")
targetNode.name = "green"
targetPoints = -2
if Int.random(in: 1...2) == 1 {
targetNode.setScale(0.25)
targetSpeed = 2
} else {
targetNode.setScale(0.5)
targetSpeed = 1
}
}
targetNode.anchorPoint = CGPoint(x: 0.5, y: 0)
let angle = CGFloat.random(in: -0.5...0.5)
targetNode.zRotation = CGFloat(angle)
self.position = CGPoint(x: -40, y: yPosition)
addChild(targetNode)
}
func moveTarget() {
let move = SKAction.moveTo(x: 500, duration: TimeInterval(targetSpeed))
let deleteTarget = SKAction.run { [weak self] in self?.removeFromParent() }
let sequence = SKAction.sequence([move, deleteTarget])
targetNode.run(sequence)
}
func fallDown() {
let scale = SKAction.scaleY(to: 0, duration: 0.2)
targetNode.run(scale)
}
}