implemented ButtonEntity

This commit is contained in:
Niko 2020-05-02 23:12:37 +02:00
parent fcf49e656d
commit 94d7be35ff
2 changed files with 39 additions and 36 deletions

View File

@ -11,12 +11,21 @@ import SpriteKit
class ButtonNode : SKSpriteNode { class ButtonNode : SKSpriteNode {
var isEnabled:Bool var isEnabled:Bool{
didSet{
if isEnabled {
self.alpha = 1
self.childNode(withName: "label")?.alpha = 1
} else {
self.alpha = 0.3
self.childNode(withName: "label")?.alpha = 0.3
}
}
}
let onButtonPress: () -> () let onButtonPress: () -> ()
init(iconName: String, text: String,isEnabled:Bool, position: CGPoint, onButtonPress: @escaping () -> ()) { init(iconName: String, text: String,isEnabled:Bool, position: CGPoint, onButtonPress: @escaping () -> ()) {
self.onButtonPress = onButtonPress self.onButtonPress = onButtonPress
self.isEnabled = isEnabled self.isEnabled = isEnabled
let texture = SKTexture(imageNamed: "yellow_button04") let texture = SKTexture(imageNamed: "yellow_button04")

View File

@ -23,19 +23,15 @@ class MenuScene: SKScene {
onButtonPress: { onButtonPress: {
self.loadScene(scene: GameScene(size: self.size)) self.loadScene(scene: GameScene(size: self.size))
})) }))
entityManager.add(Button(name: "settingsButton", entityManager.add(Button(name: "settingsButton",
iconName: "", iconName: "",
text: "Settings", text: "Settings",
position: CGPoint(x: midX, y: midY - 80 ), position: CGPoint(x: midX, y: midY - 80 ),
onButtonPress: { onButtonPress: {
//TODO: create Settings Scene
})) }))
} }
func backgroundInit(){ func backgroundInit(){
for i in 0...2 { for i in 0...2 {
let sky = SKSpriteNode(imageNamed: "SkyBackground") let sky = SKSpriteNode(imageNamed: "SkyBackground")
@ -43,7 +39,6 @@ class MenuScene: SKScene {
sky.zPosition = -1 sky.zPosition = -1
sky.size = CGSize(width: self.size.width, height: self.size.height) sky.size = CGSize(width: self.size.width, height: self.size.height)
sky.position = CGPoint(x: CGFloat(i) * sky.size.width , y: (self.frame.size.height / 2)) sky.position = CGPoint(x: CGFloat(i) * sky.size.width , y: (self.frame.size.height / 2))
self.addChild(sky) self.addChild(sky)
} }
} }
@ -61,7 +56,6 @@ class MenuScene: SKScene {
node.position.x += (self.scene?.size.width)! * 3 node.position.x += (self.scene?.size.width)! * 3
} }
})) }))
entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated
entityManager.getButtonByName(buttonName: "startGameButton").isEnabled = GameCenterHelper.isAuthenticated
} }
} }