Swift condition for the button



  • I have a file.

    import SpriteKit
    

    class GameScene: SKScene, SKPhysicsContactDelegate {
    var RestartBa : UIButton!
    var RestartB : UIButton!
    internal var ScoreL = UILabel()

    internal var Score: NSInteger = 0 //передача внешней переменной

    override func didMoveToView(view: SKView) {

    ScoreL.text = "(Score)"
    ScoreL = UILabel(frame: CGRect(x: 5, y: 0, width: 100, height: 20))
    ScoreL.backgroundColor = UIColor.whiteColor()
    ScoreL.textColor = UIColor.redColor()
    self.view?.addSubview(ScoreL)

    if (Score == 2) {
    self.view?.addSubview(RestartB)
    }

    scene?.backgroundColor = UIColor.blueColor()
    physicsWorld.contactDelegate = self
    self.scene?.size = CGSize(width: 640, height: 1136)
    // self.scene?.size = CGSize(width: self.frame.size.width, height: self.frame.size.height)

    RestartBa = UIButton(frame: CGRect(x: 0, y: 0, width: view.frame.size.width / 3, height: 30))
    RestartBa.center = CGPointMake(view.frame.size.width / 2, view.frame.size.width / 3)
    RestartBa.setTitle("1 Уровень", forState: UIControlState.Normal)
    RestartBa.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    RestartBa.addTarget(self, action: Selector("Res"), forControlEvents: UIControlEvents.TouchUpInside)
    self.view?.addSubview(RestartBa)

    RestartB = UIButton(frame: CGRect(x: 0, y: 0, width: view.frame.size.width / 3, height: 30))
    RestartB.center = CGPointMake(view.frame.size.width / 2, view.frame.size.width / 2)
    RestartB.setTitle("2 Уровень", forState: UIControlState.Normal)
    RestartB.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    RestartB.addTarget(self, action: Selector("Rest"), forControlEvents: UIControlEvents.TouchUpInside)

    }

    func Res(){

    self.view?.presentScene(Start(), transition: SKTransition.flipHorizontalWithDuration(0.3))

    let scene = Start(fileNamed: "GameScene")!
    scene.scaleMode = .AspectFill
    self.view?.presentScene(scene, transition: SKTransition.flipHorizontalWithDuration(0.3))

    RestartBa.removeFromSuperview()
    RestartB.removeFromSuperview()
    }

    func Rest(){

    self.view?.presentScene(copyGameScene(), transition: SKTransition.flipHorizontalWithDuration(0.3))

    let scene = copyGameScene(fileNamed: "GameScene")!
    scene.scaleMode = .AspectFill
    self.view?.presentScene(scene, transition: SKTransition.flipHorizontalWithDuration(0.3))

    RestartBa.removeFromSuperview()
    RestartB.removeFromSuperview()
    }

    }
    22:00:35

    Subject if (Score == 2) {
    self.view?.addSubview(RestartB)
    }
    I'm supposed to have a button, but there's a thread 1 exc_bad_instruction on this line.
    Thank you.



  • Score is transferred before ScoreL is established. After the establishment:

    ScoreL = UILabel(frame: CGRect(x: 5, y: 0, width: 100, height: 20))
    ScoreL.text = "\(Score)"
    ScoreL.backgroundColor = UIColor.whiteColor()
    ScoreL.textColor = UIColor.redColor()
    self.view?.addSubview(ScoreL)
    

    At the time you're trying to add RestartB, it doesn't exist yet.

    RestartB = UIButton(frame: CGRect(x: 0, y: 0, width: view.frame.size.width / 3, height: 30))
    RestartB.center = CGPointMake(view.frame.size.width / 2, view.frame.size.width / 2)
    RestartB.setTitle("2 Уровень", forState: UIControlState.Normal)
    RestartB.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    RestartB.addTarget(self, action: Selector("Rest"), forControlEvents: UIControlEvents.TouchUpInside)
    

    if (Score == 2) {
    self.view?.addSubview(RestartB)
    }

    It's a small letter.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2