A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Code not working

  1. #1
    Moderator enpstudios's Avatar
    Join Date
    Jun 2001
    Location
    Tampa, Fl.
    Posts
    11,282

    Code not working

    I am creating a XML driven thumbnail gallery. Which works fine with this code:

    Code:
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    
    var  fadeTween:Tween;
    
    var imageText:TextField = new TextField();
    
    var imageLoader:Loader;
    var xml:XML;
    var xmlList:XMLList;
    var xmlLoader:URLLoader = new URLLoader();
    xmlLoader.load(new URLRequest("data.xml"));
    
    xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
    
    function xmlLoaded(event:Event):void
    {
    	xml = XML(event.target.data);
    	xmlList = xml.children();
    	
    	for(var i:int = 0; i < xmlList.length(); i++)
    	{
    		imageLoader = new Loader();
    		imageLoader.load(new URLRequest(xmlList[i].attribute("thumb")));
    		imageLoader.x = 25;
    		imageLoader.y = i * 200 + 25;
    		imageLoader.name = xmlList[i].attribute("source");
    		addChild(imageLoader);
    		imageLoader.addEventListener(MouseEvent.CLICK, showPicture);
    		
    	}
    }
    
    function showPicture(event:MouseEvent):void
    {
    	imageLoader = new Loader();
    	imageLoader.load(new URLRequest(event.target.name));
    	imageLoader.x = 250;
    	imageLoader.y = stage.stageHeight / 4;
    	addChild(imageLoader);
    	imageText.x = imageLoader.x + 200;
    	imageText.y = 600;
    	for(var j:int = 0; j < xmlList.length(); j++)
    	{
    		if(xmlList[j].attribute("source") == event.target.name)
    		{
    			imageText.text = xmlList[j];
    		}
    	}
    	
    	fadeTween = new Tween(imageLoader, "alpha",None.easeNone,0,1,1,true);
    	
    }
    
    imageText.autoSize = TextFieldAutoSize.LEFT;
    addChild(imageText);

    What my problem is, is I am trying to format the text in the dynamically created text field by doing this:

    Code:
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    
    var  fadeTween:Tween;
    
    var imageText:TextField = new TextField();
    
    var myFormat:TextFormat = new TextFormat();
    myFormat.font = "Helvetica";
    myFormat.color = "0xFF0000";
    myFormat.bold = true;
    myFormat.size = 25;
    myFormat.kerning = 2;
    
    
    var imageLoader:Loader;
    var xml:XML;
    var xmlList:XMLList;
    var xmlLoader:URLLoader = new URLLoader();
    xmlLoader.load(new URLRequest("data.xml"));
    
    xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
    
    function xmlLoaded(event:Event):void
    {
    	xml = XML(event.target.data);
    	xmlList = xml.children();
    	
    	for(var i:int = 0; i < xmlList.length(); i++)
    	{
    		imageLoader = new Loader();
    		imageLoader.load(new URLRequest(xmlList[i].attribute("thumb")));
    		imageLoader.x = 25;
    		imageLoader.y = i * 200 + 25;
    		imageLoader.name = xmlList[i].attribute("source");
    		addChild(imageLoader);
    		imageLoader.addEventListener(MouseEvent.CLICK, showPicture);
    		
    	}
    }
    
    function showPicture(event:MouseEvent):void
    {
    	imageLoader = new Loader();
    	imageLoader.load(new URLRequest(event.target.name));
    	imageLoader.x = 250;
    	imageLoader.y = stage.stageHeight / 4;
    	addChild(imageLoader);
    	imageText.x = imageLoader.x + 200;
    	imageText.y = 600;
    	for(var j:int = 0; j < xmlList.length(); j++)
    	{
    		if(xmlList[j].attribute("source") == event.target.name)
    		{
    			imageText.text = xmlList[j];
    		}
    	}
    	
    	fadeTween = new Tween(imageLoader, "alpha",None.easeNone,0,1,1,true);
    	
    }
    
    imageText.autoSize = TextFieldAutoSize.LEFT;
    addChild(imageText);
    
    imageText.setTextFormat(myFormat);
    And the text doesn't format, what am I doing wrong?

  2. #2
    I believe you have to set the format after you change what is in the textField. You should put your setting of the the text format at the end of the showPicture event like this:

    Code:
    function showPicture(event:MouseEvent):void
    {
    	imageLoader = new Loader();
    	imageLoader.load(new URLRequest(event.target.name));
    	imageLoader.x = 250;
    	imageLoader.y = stage.stageHeight / 4;
    	addChild(imageLoader);
    	imageText.x = imageLoader.x + 200;
    	imageText.y = 600;
    	for(var j:int = 0; j < xmlList.length(); j++)
    	{
    		if(xmlList[j].attribute("source") == event.target.name)
    		{
    			imageText.text = xmlList[j];
    		}
    	}
    	
    	fadeTween = new Tween(imageLoader, "alpha",None.easeNone,0,1,1,true);
    	imageText.setTextFormat(myFormat);
    }
    I think that should work.

  3. #3
    Moderator enpstudios's Avatar
    Join Date
    Jun 2001
    Location
    Tampa, Fl.
    Posts
    11,282
    That worked great, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center