0

I'm making a quiz in flash using action script 3. The code below makes the text smaller if it goes over 2 lines so it can fit in the question box. This works! However, when you change key frame the formatting of the box changes. For example, when the question is presented the font changes, but when you answer the question and the key frame changes, the font changes making it overflow off the edge.

How can I make the formatting continue over the key frames.

This is the actions for the game.

var smallLimit:int = 1;
var format:TextFormat = new TextFormat();


myTextField11.text = shuffledAnswers1[0];   //puts into the text boxes the random answers
myTextField21.text = shuffledAnswers1[1];
myTextField31.text = shuffledAnswers1[2];
myTextField41.text = shuffledAnswers1[3];

var testSize11:int = 25;
var testSize21:int = 25;
var testSize31:int = 25;
var testSize41:int = 25;

while( testSize11 > smallLimit ){

    updateFormat11( testSize11 );
    trace( myTextField11.numLines  );

    if( myTextField11.numLines > 1){
        testSize11--;
    }else{
        testSize11 = smallLimit;
    }
}
while( testSize21 > smallLimit ){

    updateFormat21( testSize21 );
    trace( myTextField21.numLines  );

    if( myTextField21.numLines > 1){
        testSize21--;
    }else{
        testSize21 = smallLimit;
    }
}
while( testSize31 > smallLimit ){

    updateFormat31( testSize31 );
    trace( myTextField31.numLines  );

    if( myTextField31.numLines > 1){
        testSize31--;
    }else{
        testSize31 = smallLimit;
    }
}
while( testSize41 > smallLimit ){

    updateFormat41( testSize41 );
    trace( myTextField41.numLines  );

    if( myTextField41.numLines > 1){
        testSize41--;
    }else{
        testSize41 = smallLimit;
    }
}

function updateFormat11(size11:int):void{
    format.size = size11;
    myTextField11.setTextFormat( format );
    }
function updateFormat21(size21:int):void{
    format.size = size21;
    myTextField21.setTextFormat( format );
}
function updateFormat31(size31:int):void{
    format.size = size31;
    myTextField31.setTextFormat( format );
    }
function updateFormat41(size41:int):void{
    format.size = size41;
    myTextField41.setTextFormat( format );

}

This is key frame 2 which sets the questions in the text boxes.

myTextField11.text = shuffledAnswers1[0];  
myTextField21.text = shuffledAnswers1[1];
myTextField31.text = shuffledAnswers1[2];
myTextField41.text = shuffledAnswers1[3];
stop();
1

2 Answers 2

1

Perhaps try this. This assume you have 4 textfield on frame and array of 4 answer.

//Frame 1
var smallLimit:int = 1;
//put all your answer fields in array
var tFields:Array = [myTextField11,myTextField21,myTextField31,myTextField41]
var format:TextFormat = new TextFormat();
//your default font size
var defaultFontSize:int = 25;

function updateAnswerText( value:Array  )
{
    var fontSize:int = defaultFontSize;
    for(var i in tFields )
    {
        //reset to default
        fontSize = defaultFontSize;
        //add text
        tFields[i].text = value[i];
        //setformat
        updateTextFormat( tFields[i] , fontSize)
        //are we overflow
        while( tFields[i].numLines > 1 && fontSize > smallLimit)
        {
            --fontSize;
            updateTextFormat( tFields[i] , fontSize)
        }
    }
}
function updateTextFormat( field:TextField, value:int)
{
    format.size = value;
    field.setTextFormat( format );
}
updateAnswerText( shuffledAnswers1 )

//On every frame have answer textfield

var tFields:Array = [myTextField11,myTextField21,myTextField31,myTextField41]
updateAnswerText( shuffledAnswers1 )

call the above method and pass your answer array. This will give you an idea.

0

you will have to update the font size again after you update the content. You should be able to use the same code you are using in the first frame.

1
  • I have tried this but it gives me multiple duplication and conflict errors
    – alan smith
    Apr 14, 2016 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.