TextField does not work

1k views Asked by At

I have a text field to type the word that, when (auto) and press the button (checkbutton) it should print out (correct). The problem is that it's not working.

When I'm in office touchesBegan: if (TextField.text == "auto") lists me an error:

Use Unresolved identifier "TextField".

When this condition removed as everything works fine and writes to me (correct).

I tried complet function touchesBegan moved to override func didMoveToView (view: SKView) and TextField.text is resolved but it is not working.

I work in SpriteKit

Where I make mistakes ?. Thank you

Here is my code:

class level1: SKScene{
    let logo = SKSpriteNode(imageNamed: "Q1")
    let check = SKSpriteNode(imageNamed: "check")
    let number = SKSpriteNode(imageNamed: "N1")

    override func didMoveToView(view: SKView) {
        scene!.scaleMode = .AspectFill
        scene!.scaleMode = SKSceneScaleMode.ResizeFill

        var background = SKSpriteNode(imageNamed: "Background")
        background.size = CGSizeMake(self.size.width, self.size.height)
        background.position = CGPointMake(self.size.width/2, self.size.height/2)
        self.addChild(background)

        let screenSize: CGRect = UIScreen.mainScreen().bounds
        let screenWidth = screenSize.width;
        let screenHeight = screenSize.height;

        var TextField = UITextField(frame : CGRect(x:10, y:(screenHeight/2.25), width:(screenWidth - 20), height:(screenHeight/15) ))
        self.view!.addSubview(TextField)
        TextField.backgroundColor = UIColor.whiteColor()
        TextField.textAlignment = .Center
        TextField.font = UIFont(name: "Helvetica Neue", size: 23)

        number.position = CGPoint(x: size.width * 0.5, y: size.height * 0.95)
        addChild(number)

        logo.position = CGPoint(x: size.width * 0.5, y: size.height * 0.72)
        addChild(logo)

        check.position = CGPointMake(size.width/2,size.height/2 - 43)
        check.name = "checkbutton"
        addChild(check)
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        let touch = touches.first as! UITouch
        let touchLocation = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(touchLocation)

        if(touchedNode.name == "checkbutton"){
            if(TextField.text == "auto"){
                println("CORRECT")
            }
        } else {
        }
    }
}
1

There are 1 answers

0
Ali Beadle On BEST ANSWER

Your var TextField is local to the method didMoveToView so cannot be accessed from inside touchesBegan. Move this var outside of didMoveToView into the class level.

Also, by convention vars should start with a lower case letter: textField rather than TextField.