changed base to SKSpriteNode and add effects touch/move

This commit is contained in:
Niko Jochim 2020-04-20 12:33:57 +02:00
parent 63e0336413
commit 9a560d2fa0

View File

@ -9,19 +9,30 @@
import SpriteKit import SpriteKit
import GameplayKit import GameplayKit
class Base { class Base : SKSpriteNode{
var spriteNode: BaseNode! public static let colorSelected = SKColor.yellow
var availableBases = [Base]()
var standartColor = SKColor.green
init(frame: CGRect, color: UIColor, position: CGPoint) { init(color: UIColor, position: CGPoint, name: String) {
spriteNode = BaseNode(texture: SKTexture(imageNamed: "Base"), color: color, size: CGSize(width: 50.0, height: 50.0)) super.init(texture: SKTexture(imageNamed: "Base"), color: color, size: CGSize(width: 50.0, height: 50.0))
spriteNode.colorBlendFactor = 1
spriteNode.name = "Base" standartColor = color
spriteNode.position = position self.colorBlendFactor = 1
spriteNode.isUserInteractionEnabled = true self.name = name
} self.position = position
self.zPosition = 2
self.isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addAvailableBase(base: Base) {
availableBases.append(base)
} }
class BaseNode: SKSpriteNode {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("\(self.name!) was touched and is located at \(self.position)") print("\(self.name!) was touched and is located at \(self.position)")
self.run( self.run(
@ -34,7 +45,26 @@ class BaseNode: SKSpriteNode {
) )
} }
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
self.color = SKColor.yellow
for base in availableBases {
base.color = Base.colorSelected
base.size = CGSize(width: 60, height: 60)
} }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.color = standartColor
for base in availableBases {
base.color = base.standartColor
base.size = CGSize(width: 50, height: 50)
}
}
}
enum PlayerColors { enum PlayerColors {
static let colors = [ static let colors = [