简体   繁体   中英

My if-statement doesn't work

It's either going to be "Riktig, bokstaven forekommer" for right letter chosen or otherwise. What have i done wrong, when i start it now none of the Messages show.

btnStart.addEventListener(MouseEvent.CLICK, start);

var feil:Array = new Array  ;

var riktig:Array = new Array  ;

var bokstav:Array = new Array  ;
bokstav[0] = "b";

bokstav[1] = "r";

bokstav[2] = "e";

bokstav[3] = "v"
var txtBokstav:TextField = new TextField( );

txtBokstav.maxChars = 1;

txtBokstav.restrict = "a-z æ ø å";

txtBokstav.type = flash.text.TextFieldType.INPUT;

 addChild(txtBokstav);

function start(evt)
{
    var bokstavInn:String = String(txtBokstav.text);

    if (txtBokstav.text.length == 1)
    {
        if (bokstav.indexOf(bokstavInn) >= 0)
        {
            txtUtskrift.text = "Riktig, bokstaven forekommer";

            riktig.push(bokstavInn);

            for (var i = 0; i < riktig.length; i++)
            {
                txtUtskrift.appendText(riktig[i] + ", ");
            }
        }
        else
        {
            txtUtskrift.text = "Feil, bokstaven forekommer ikke";

            feil.push(bokstavInn);

            for (var l = 0; l < feil.length; l++)
            {
                txtUtskrift.appendText(feil[l] + ", ");
            }
        }
    }
}

1) Are you sure that txtUtskrift is on the Stage and visible?

2) Make sure that start() is being called (use a breakpoint or trace)

3) As askmozo suggests, place trace statements in the possible flow of execution. So, at the beginning of the start function, and within each possible if condition. You might add an else for the text.length test:

function start( evt )
{
    trace( "start()..." );

    if (txtBokstav.text.length == 1)
    {
        // ... what you already have
    }
    else
    {
        trace( "txtBokstav length not valid:", txtBokStav.text.length );
    }

    trace( "...start()" );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM