A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: dynamically loading and using links...

  1. #1
    Member
    Join Date
    Dec 2005
    Posts
    99

    dynamically loading and using links...

    I can't figure this out! I have an XML document that has the name of my navigation links and the URL links in it for my flash to read. All works, except that the links seem to be read in correctly , but then I call them on a button click, the loop I have created just runs to the last URL. So, essentially all of the button s link to the same URL.

    I am baffled as to what is wrong here. Any help would be awesome!!

    code:


    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, handleComplete);
    loader.load( new URLRequest("Nav.xml") );

    function handleComplete (event:Event) {
    var _tabs:XML = new XML(event.target.data);
    trace("x: " + _tabs.tabs.tab.length());
    for (i=0;i<_tabs.tabs.tab.length();i++) {
    this["Tab" + i].link_mc.tabtitle.text = _tabs.tabs.tab[i].tabtitle;
    targetURL = _tabs.tabs.tab[i].link;
    trace("URL: " + targetURL);
    this["Tab" + i].addEventListener(MouseEvent.CLICK, onClick);
    var test:MovieClip = new MovieClip();
    trace(_tabs.tabs.tab[i].link);
    }
    }

    function onClick(event:MouseEvent):void
    {
    trace("URL: " + targetURL);
    navigateToURL( new URLRequest(targetURL));
    }




    Code:
    <?xml version="1.0" encoding="utf-8"?>
    
    <nav>
    	
    	<tabs>
    
    		<tab>
    		
    	 		<tabtitle> TripTiks1 </tabtitle> 
    			
    			<link> http://www.msn.com </link>
    		
    		</tab>
    
    		<tab>
    		
    	 		<tabtitle> TripTiks2 </tabtitle> 
    			
    			<link> http://www.xbox.com </link>
    		
    		</tab>
    		
    		<tab>
    		
    	 		<tabtitle> TripTiks3 </tabtitle> 
    			
    			<link> http://www.cnn.com </link>
    		
    		</tab>
    		
    		<tab>
    		
    	 		<tabtitle> TripTiks4 </tabtitle> 
    			
    			<link> http://www.myway.com </link>
    		
    		</tab>
    
    	</tabs>
    
    </nav>

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You need to associate the mc with the url:

    targetURL = _tabs.tabs.tab[i].link;
    this["Tab" + i].targetURL = targetURL;
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Member
    Join Date
    Dec 2005
    Posts
    99
    I think that you are right..the mc and the URL are not synced up. The solution you suggest does not work, it is still going to the last URL in the XML file.

    hmmmmm....any other suggestions?

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Because within your onClick function you are not referring to the mcs.

    trace("URL: " + event.target.targetURL);
    navigateToURL( new URLRequest(event.target.targetURL));
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    Member
    Join Date
    Dec 2005
    Posts
    99
    When I add that in I get "ReferenceError: Error #1069: Property targetURL not found on flash.text.TextField and there is no default value.
    at RoadTripsNav_fla::MainTimeline/onClick()"

    So here is my script now:
    code:


    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, handleComplete);
    loader.load( new URLRequest("Nav.xml") );

    function handleComplete (event:Event) {
    var _tabs:XML = new XML(event.target.data);
    trace("x: " + _tabs.tabs.tab.length());
    for (i=0;i<_tabs.tabs.tab.length();i++) {
    this["Tab" + i].link_mc.tabtitle.text = _tabs.tabs.tab[i].tabtitle;
    targetURL = _tabs.tabs.tab[i].link;
    this["Tab" +i].targetURL = targetURL;
    trace("URL: " + targetURL);
    this["Tab" + i].addEventListener(MouseEvent.CLICK, onClick);
    var test:MovieClip = new MovieClip();
    trace(_tabs.tabs.tab[i].link);
    }
    }

    function onClick(event:MouseEvent):void
    {
    trace("URL: " + event.target.targetURL);
    navigateToURL( new URLRequest(event.target.targetURL));
    }




    Am I missing something? I am still confused. Thanks for looking into this with me!!

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    I thought Tab + i were MovieClips. You can only associate variables dynamically with MovieClips. So you need to place the textfield into a MovieClip. I wrote a little script, which shows that.
    PHP Code:
    var myString:String "Hello";
    var 
    mc:MovieClip = new MovieClip();
    addChild(mc);
    var 
    a:TextField = new TextField();
    a.text myString;
    mc.addChild(a);
    var 
    targetURL:String "www.flashscript.biz";
    mc.targetURL targetURL;
    mc.addEventListener (MouseEvent.CLICKonClick);

    function 
    onClick (event:MouseEvent):void
    {
        
    trace ("URL: " event.currentTarget.targetURL);

    - The right of the People to create Flash movies shall not be infringed. -

  7. #7
    Member
    Join Date
    Dec 2005
    Posts
    99
    Holy cow! It works!!! Tab + i is a movieclip. So, I'm not exactly sure what is making the difference. Here is what I have ended up with:

    code:


    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, handleComplete);
    loader.load( new URLRequest("Nav.xml") );

    function handleComplete (event:Event) {
    var _tabs:XML = new XML(event.target.data);
    trace("x: " + _tabs.tabs.tab.length());
    for (i=0;i<_tabs.tabs.tab.length();i++) {
    this["Tab" + i].link_mc.tabtitle.text = _tabs.tabs.tab[i].tabtitle;
    var targetURL:String = _tabs.tabs.tab[i].link;
    this["Tab" + i].targetURL = targetURL;
    this["Tab" + i].addEventListener(MouseEvent.CLICK, onClick);
    trace(_tabs.tabs.tab[i].link);
    }
    }

    function onClick(event:MouseEvent):void {
    trace("URL clicked: " + event.currentTarget.targetURL);
    navigateToURL( new URLRequest(event.currentTarget.targetURL));
    }




    I think it is that now targetURL is getting the link and then the next line is assigning the variable targetURL to the movieclip? Or is it the event.currentTarget.targetURL? I'm not sure what this means exactly.

    Thank you so much for the help!!! Any help in understanding this would be greatly appreciated. Always trying to learn

  8. #8
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    event.currentTarget is referring to the actual target, which triggers the event listener, which is this["Tab" + i].
    - The right of the People to create Flash movies shall not be infringed. -

  9. #9
    Member
    Join Date
    Dec 2005
    Posts
    99
    Thanks! You're awesome!

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