Skip to main content
Inspiring
October 21, 2009
Question

changing text of a text field within a sprite?

  • October 21, 2009
  • 1 reply
  • 658 views

Hi,

Is it possible to change the text property of a text field that is contained within a sprite? I've been trying but can't come up with the right syntax. Here is what I'm doing:

I've created a sprite named "lessonSprite" and added a text field to it in one function. Now, in another function, I'd like to change the text in the field.

I've tried to use this:

lessonSprite.titleField.text = xmlLsnContent.attribute("title");

where "titleField" is the name of the field added to the sprite "lessonSprite" in an earlier function.

TIA

PeterH

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
October 21, 2009

How was the sprite created/named?  Can you show the function?

Pete47Author
Inspiring
October 21, 2009

Hi there and thanks for the reply :-)

I am creating the lessonSprite in the class constructor (lines 2 and 3 below). After the XML data is loaded, I call another function that uses the XML data to build a "page" in a lesson. Today's effort is my first step to making the class a bit more modular.

This is the contructor:

public function SimpleLesson() {

   lessonSprite = new Sprite();
   addChild(lessonSprite);
  
   textField1Format=new TextFormat("Arial",14,0xFFFFFF,true,false,false,null,null,"left");
   textField2Format=new TextFormat("Verdana",10,0xFFFFFF,true,false,false,null,null,"left");
   questionField1Format=new TextFormat("Arial",18,0xFFFFFF,true,false,false,null,null,"left");
   feedback1Format=new TextFormat("Arial",18,0xFFFFFF,true,false,false,null,null,"center");
  
   // add the background image to the lessonSprite so it appears on
   // every page of the lesson
   var backLoader:Loader = new Loader();
   backLoader.load( new URLRequest( "media/background.png"));
   lessonSprite.addChild(backLoader);
     
   // lesson title field
   var titleField:TextField = new TextField();
   titleField.x=320;
   titleField.y=35;
   titleField.width=320;
   titleField.defaultTextFormat=textField1Format;
   titleField.selectable=false;
   titleField.multiline=false;
   //titleField.text="General Information on Clearances"; //xmlLsnContent.page[pNum].text;
   lessonSprite.addChild(titleField);

   //nav buttons
   nextButton=new Next_btn  ;
   nextButton.name="Next";
   nextButton.x=750;
   nextButton.y=550;
   nextButton.addEventListener(MouseEvent.CLICK,navButtonClicked);
   lessonSprite.addChild(nextButton);
   //
   backButton=new Back_btn  ;
   backButton.name="Back";
   backButton.x=50;
   backButton.y=550;
   backButton.addEventListener(MouseEvent.CLICK,navButtonClicked);
   lessonSprite.addChild(backButton);

  
   // LsnPageNumber contains the element number of the page to be built. If starting outside of an
   // LMS this will be 0. If starting under an LMS, the value should be pulled from the LMS.
   LsnPageNumber=savedPageNumber;
   grabXML("Lesson1.xml");
  }

Ned Murphy
Legend
October 21, 2009

If you declare the TextField outside of the function (var titleField:TextField;), the same way I assume you did for the Sprite, then it will have a broader scope and you should be able to target it directly to assign text to it...  titleField.text = "...";