Add Bases, Autospawn 15 Bases

Add visual effect when base is touched
This commit is contained in:
Marcel Schwarz 2020-04-18 16:50:57 +02:00
parent 79dcc78f75
commit ee69f2c345
3 changed files with 38 additions and 9 deletions

View File

@ -10,13 +10,29 @@ import SpriteKit
import GameplayKit import GameplayKit
class Base { class Base {
var spriteNode: SKSpriteNode! var spriteNode: BaseNode!
init(frame: CGRect, color: UIColor, position: CGPoint) { init(frame: CGRect, color: UIColor, position: CGPoint) {
spriteNode = SKSpriteNode(texture: SKTexture(imageNamed: "Base"), color: color, size: CGSize(width: 30.0, height: 30.0)) spriteNode = BaseNode(texture: SKTexture(imageNamed: "Base"), color: color, size: CGSize(width: 50.0, height: 50.0))
spriteNode.colorBlendFactor = 1 spriteNode.colorBlendFactor = 1
spriteNode.name = "Base" spriteNode.name = "Base"
spriteNode.position = position spriteNode.position = position
spriteNode.isUserInteractionEnabled = true
}
}
class BaseNode: SKSpriteNode {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("\(self.name!) was touched and is located at \(self.position)")
self.run(
SKAction.sequence(
[
SKAction.resize(byWidth: 20, height: 20, duration: 0.5),
SKAction.resize(byWidth: -20, height: -20, duration: 0.5)
]
)
)
} }
} }

Binary file not shown.

View File

@ -13,9 +13,28 @@ class GameScene: SKScene {
var entities = [GKEntity]() var entities = [GKEntity]()
var graphs = [String : GKGraph]() var graphs = [String : GKGraph]()
var bases = [Base]()
override func sceneDidLoad() { override func sceneDidLoad() {
// Spawn bases
for _ in 1...15 {
let color = PlayerColors.colors.randomElement()
let xRange = Int(frame.minX + frame.width * 0.07)...Int(frame.maxX - frame.width * 0.07)
let yRange = Int(frame.minY + frame.height * 0.07)...Int(frame.maxY - frame.width * 0.07)
let position = CGPoint(
x: Int.random(in: xRange),
y: Int.random(in: yRange)
)
print("Generating Base at \(position)")
let base = Base(frame: frame, color: color!, position: position)
addChild(base.spriteNode)
bases.append(base)
}
} }
func touchDown(atPoint pos : CGPoint) { func touchDown(atPoint pos : CGPoint) {
@ -31,7 +50,7 @@ class GameScene: SKScene {
} }
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
spawnBase()
} }
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
@ -49,10 +68,4 @@ class GameScene: SKScene {
override func update(_ currentTime: TimeInterval) { override func update(_ currentTime: TimeInterval) {
} }
func spawnBase() {
let color = PlayerColors.colors.randomElement()
let base = Base(frame: frame, color: color!, position: CGPoint(x: 100, y: 100))
addChild(base.spriteNode)
}
} }