mercredi 15 mars 2017

Submit user input by click AS3

I am just starting my adventure with coding in AS3. I would love to learn how to submit my input by clicking a button and check if it is same as my "password" number. Then, display the message in the dynamic field about the results. I have written some code and it does not show any errors, but - as you may guess - it does not work either.

I thought that using the command button.addEventListener(MouseEvent.CLICK, handleClick); and then creating a simple if... else statement is enough, but somehow it does not work.

Would you kindly take a look at my code and point the mistake which has lead to my misery?

here is the code:

    private var Input:Object;
    private var Output:Object;
    private var yournr:String;
    public function Tehtava25a()
    {
        var Input:TextField = new TextField();
        var Output:TextField = new TextField();
        var initialtxt:String = "Give me the password";

        Input.type = TextFieldType.INPUT;
        Input.border = true;
        Input.width = 200;
        Input.height = 200;
        Input.backgroundColor=0xFFFF99;
        Input.background =true;
        Input.borderColor=0x006666;
        addChild(Input);

        Output.type = TextFieldType.DYNAMIC;
        Output.text=  initialtxt;
        Output.background = true;
        Output.border = true;
        Output.backgroundColor=0xFFFF99;
        Output.background =true;
        Output.borderColor=0x006666;
        Output.width = 200;
        Output.height = 200;
        Output.x = 200;
        addChild(Output);   
        var button:SimpleButton = new SimpleButton();
        button.x = 250;
        button.y = 250;

        button.upState = createCircle( 0x00FF00, 15 );
        button.overState = createCircle( 0xFFFFFF, 16 );
        button.downState = createCircle( 0xCCCCCC, 15 );
        addChild( button );
        button.hitTestState = createCircle( 0x000000, 50 );

        button.addEventListener(MouseEvent.CLICK, handleClick , false, 0, true);


    }

    private function createCircle(param0:Number, param1:int):DisplayObject
    {var circle:Shape = new Shape(  );
        circle.graphics.lineStyle( 1, 0x000000 );
        circle.graphics.beginFill( 0xFF794B );
        circle.graphics.drawCircle( 0, 0, 30 );
        circle.graphics.endFill(  );
        return circle;
    }

    protected function handleClick(event:MouseEvent):void
    {var yournr:String = Input.text;
    var mynr:String = "1234";
        if (yournr == mynr){
            Output.appendText = "the number is correct"}
        else { Output.appendText = "try again";
        }
    }   
     }
    }

Aucun commentaire:

Enregistrer un commentaire