2009-09-10 8 views
1

J'utilise AS3 et je crée une boîte de pop-up en tant que tel:automatiquement redimensionnée Sprite en Actionscript 3

 var s:Sprite = new Sprite(); 

     var shape:Shape = new Shape(); 
     shape.name = "HitArea"; 
     s.addChild(shape); 

     shape.graphics.lineStyle(4, 0xFFFFFF, 1); 
     shape.graphics.beginFill(0xFFFFFF); 
     shape.graphics.drawRoundRect(0, 0, 200, 30, 10, 10); 

     var text:TextField = new TextField(); 
     text.text = ""; 
     text.autoSize = TextFieldAutoSize.CENTER; 
     text.name = "Text"; 
     text.x = 100; 
     text.y = 10; 
     s.addChild(text); 

     return s; 

Ce qui crée une boîte de 200 x 30 qui sert une boîte d'erreur. Le texte sort parfois de la boîte, comme j'utilise

 (s.getChildByName('Text') as TextField).text = "Text here"; 

comment puis-je adapter la boîte au texte? Ou y a-t-il une meilleure alternative?

Répondre

1

Comment régler la taille après avoir réglé le texte?

var s:Sprite = new Sprite(); 

    var text:TextField = new TextField(); 
    text.text = ""; 
    text.autoSize = TextFieldAutoSize.CENTER; 
    text.name = "Text"; 
/* // Not yet 
    text.x = 100; 
    text.y = 10; 
*/ s.addChild(text); 

    // Create the shape based on text's size 
    var shape:Shape = new Shape(); 
    shape.name = "HitArea"; 
    s.addChild(shape); 
    shape.graphics.lineStyle(4, 0xFFFFFF, 1); 
    shape.graphics.beginFill(0xFFFFFF); 
    shape.graphics.drawRoundRect(0, 0, text.width + 32, text.height + 32, 10, 10); // Plus padding 

    // Now adjust the text's position to the box's center 
    text.x = (s.width - text.width)/2; 
    text.y = (s.height - text.height)/2; 

    return s; 

De cette façon, la boîte s'adaptera au texte, quelle que soit sa taille.

Questions connexes