A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: XML/AS3 help creating a url link

  1. #1
    Senior Member
    Join Date
    Jul 2009
    Posts
    167

    XML/AS3 help creating a url link

    hi all

    would anyone beable to help me with this : ( ?

    I have been trying to understand how to create a url link with the xml file that my menu is driven by. what I have is a menu with text amd images reading from an xml file, I have created a third text field which I would like to use as a button/link that when click it goes to a url.

    does this make any sense,mmmhhh!!! he! he!

    this is what I have for my XML:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <data>
    		<image name="image 1" path="img/img1.jpg" 
    		title="test 1" 
    		text="INFORMATION"
    		button="Go to URL 1" href="http://www.url1.com" />
    		
    		<image name="image 2" path="img/img2.jpg" 
    		title="test 2" 
    		text="INFORMATION" 
    		button="Go to URL 2" href="http://www.url2.com" />
    		
    		<image name="image 3" path="img/img3.jpg" 
    		title="test 3" 
    		text="INFORMATION" 
    		button="Go to URL 3" href="http://www.url3.com" />
    
    </data>
    I have tried to do something as simple as adding

    href="www.mywebaddress"


    but this doesn't seem to work, am I missing something within the flash movie. Am a newbie so please brake down little by little,

    many thanks for any help, I have attached a demo file link for example, you will see that I have created 3 url links, each would have a different url?


    http://art.clubworldgroup.com/menu/menu_19_oct.zip

  2. #2
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Attach your source files and I'd be happy to take a look.

    n/m I see it now.

  3. #3
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    I added the following to your "main.as" class in the function that makes all the items:

    Code:
    holder.mouseChildren = false;
    holder.urlLink = images[i].@href;
    Then I added the following to give me feedback in the function that handles the clicks:

    Code:
    trace("click");
    trace(event.currentTarget.urlLink);
    Basically what I've done is defined a property on each movieclip holder to hold the URL specified. Then said to make no mouseChildren, which means, that the click is heard by the entire object in a nutshell.

    Then I call the urlLink property when the click is called.

    To navigate to it, you could use:

    navigateToURL(url);

    but I just have it tracing out right now.

    Hope this helps.

  4. #4
    Senior Member
    Join Date
    Jul 2009
    Posts
    167
    hi there

    thanks for that

    yeah I thnk I no what you mean, but am newbie so is it possible you can explain with example with my script I already have, doh!

    thanks for your help


  5. #5
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    I'm not quite sure what your asking, but if your asking for the code after my edits, here it is.

    This is your main.as file

    Code:
    package {
    	import flash.display.DisplayObject;
    	import flash.display.MovieClip;
    	import flash.display.Loader;
    	import flash.display.Sprite;
    	import flash.events.Event;
    	import flash.events.MouseEvent;
    	import flash.net.URLLoader;
    	import flash.net.URLRequest;
    	import flash.net.URLLoaderDataFormat;
    	import CircleMenu;
    
    
    
    	public class Main extends Sprite {
    
    		public var circleMenu:CircleMenu;
    		public var xmlLoader:URLLoader;
    
    		public function Main() {
    			//circleMenu = new CircleMenu( 340, 88, 12);
    			circleMenu = new CircleMenu(620,2000,25);
    			circleMenu.x = -300;
    			circleMenu.y = 300;
    			addChildAt( circleMenu, 0 );
    
    			// Use URLLoader to load XML
    
    
    			xmlLoader = new URLLoader();
    			xmlLoader.dataFormat = URLLoaderDataFormat.TEXT;
    
    			// Listen for the complete event
    			xmlLoader.addEventListener(Event.COMPLETE, onXMLComplete);//related to line 86
    			xmlLoader.load(new URLRequest("data.xml"));//related to line 87
    
    
    
    			stage.addEventListener( MouseEvent.MOUSE_WHEEL, onMouseWheel );
    
    
    
    
    		}
    
    		//___________________________________________________________
    		//———————————————————————————————————————————— EVENT HANDLERS
    
    		private function onXMLComplete(event:Event):void {
    			// Create an XML Object from loaded data
    			var data:XML = new XML(xmlLoader.data);
    
    			// Now we can parse it
    			var images:XMLList = data.image;
    
    
    
    
    
    			for (var i:int = 0; i < images.length(); i++) {
    				// Get info from XML node
    				var imageName:String = images[i]. @ name;
    				var imagePath:String = images[i]. @ path;
    				var titles:String = images[i]. @ title;
    				var texts:String = images[i]. @ text;
    				var buttons:String = images[i]. @ button;// <<<<<<NEW 14TH OCTOBER 2009
    
    
    				// Load images using standard Loader
    				var loader:Loader = new Loader();
    
    				// Listen for complete so we can center the image
    				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onImageComplete);//related to line 43
    				loader.load(new URLRequest(imagePath));//related to line 44
    
    				// Create a container for the loader (image)
    				var holder:MovieClip = new MovieClip();
    				holder.addChild(loader);
    
    				//var preloader:Preloader = new Preloader();  
    				//                holder.addChild(preloader); 
    				var button_main:Button_mr = new Button_mr();//this loads button onto the stage
    				holder.addChild(button_main);//this loads in the button into container 'holder'
    
    
    
    
    				var tooltip:ToolTip = new ToolTip();
    				tooltip.field.text = titles;//loads tooltip 1
    				tooltip.field2.text = texts;//loads tool tip 2
    				tooltip.field3.text = buttons;//loads tool tip 3    // <<<<<NEW 14th OCTOBER 2009
    
    				tooltip.x = -350;// where to have the tooltip on x axis in the holder
    				tooltip.y = 0;// where to have the tooltip on y axis in the holder
    				holder.addChild(tooltip);
    
    
    				// Same proceedure as before
    				holder.buttonMode = true;
    				holder.addEventListener( MouseEvent.CLICK, onMenuItemClick );
    				holder.mouseChildren = false;
    				holder.urlLink = images[i].@href;
    
    
    
    
    				// Add it to the menu
    				circleMenu.addChild(holder);
    
    			}
    		}
    
    		private function onImageComplete(event:Event):void {
    			var img:Loader = event.currentTarget.loader;
    			img.content["smoothing"] = true;
    			img.x = -(img.width/2);
    			img.y = -(img.height/2);
    		}
    
    		private function onMouseWheel( event:MouseEvent ):void {
    			event.delta < 0 ? circleMenu.next():circleMenu.prev();//changing the < to > dipicts the way the mouse scrolls the menu
    		}
    
    		private function onMenuItemClick( event:MouseEvent ):void {
    			trace("click");
    			trace(event.currentTarget.urlLink);
    			circleMenu.scrollToItem( event.currentTarget as DisplayObject );
    		}
    	}
    }
    Is that what you were asking? If not, let me know.

  6. #6
    Senior Member
    Join Date
    Jul 2009
    Posts
    167
    hi there

    yes, thanks for that, that was what Imean, is difficult for me to understand aas a newbie so I need explantion in script so I can see where to put things. thank you!

    ok, I copy your script and made it my main.as file, when I click its traces it and shows what url, example i get in the output window

    click
    http://www.adobe.com

    this is what I have now in my XML file

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <data>
    		<image name="image 1" path="img/img1.jpg" 
    		url="http://www.google.com" 
    		title="test 1" 
    		text="INFORMATION" 
    		button="Go to URL 1" href="http://www.flashkit.com" />
    
    		
    		<image name="image 2" path="img/img2.jpg" 
    		title="test 2" 
    		text="INFORMATION" 
    		button="Go to URL 2" href="http://www.adobe.com" />
    		
    		<image name="image 3" path="img/img3.jpg" 
    		title="test 3" 
    		text="INFORMATION" 
    		button="Go to URL 3" href="http://www.google.com" />
    
                             and so on . . .

    is this correct???


    many thanks for your time : )

  7. #7
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    No Prob, and it looks correct to me.

  8. #8
    Senior Member
    Join Date
    Jul 2009
    Posts
    167
    sorry the XML should be


    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <data>
    		<image name="image 1" path="img/img1.jpg" 
    		url="http://www.google.com" 
    		title="test 1" 
    		text="INFORMATION" 
    		button="Go to URL 1" 
    
    		
    		<image name="image 2" path="img/img2.jpg" 
                              url="http://www.adobe.com" 
    		title="test 2" 
    		text="INFORMATION" 
    		button="Go to URL 2" 
    		
    		<image name="image 3" path="img/img3.jpg" 
                             url="http://www.flashkit.com" 
    		title="test 3" 
    		text="INFORMATION" 
    		button="Go to URL 3"
    is this correct

  9. #9
    Senior Member
    Join Date
    Jul 2009
    Posts
    167
    ok, know, sorry it was the other way like this


    Code:
       <image name="image 1" path="img/img1.jpg" 
    		title="test 1" 
    		text="INFORMATION" 
    		button="Go to URL 1" href="http://www.adobe.com" />
    
    		
    		<image name="image 2" path="img/img2.jpg" 
    		title="test 2" 
    		text="INFORMATION" 
    		button="Go to URL 2"  href="http://www.google.com" />
    		
    		<image name="image 3" path="img/img3.jpg" 
    		title="test 3" 
    		text="INFORMATION" 
    		button="Go to URL 3" href="http://www.kirupa.com" />

    quick question:

    for the url link to work does it need to be in a browser as when publish via flash it doesn't seem to go anywhere??

    many thanks

  10. #10
    Senior Member
    Join Date
    Jul 2009
    Posts
    167
    hi there

    I uploaded the menu to see if that was the problem but it still doesn't go to the links when click.......can you thing of anything I have missed


    this is the link

    http://art.clubworldgroup.com/menu/tests/


  11. #11
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Shouldn't need to be, the syntax for navigateToURL is:

    navigateToURL(new URLRequest("http://www.google.com"));

  12. #12
    Senior Member
    Join Date
    Jul 2009
    Posts
    167
    hi there

    sorry getting a little confused think I missed something out, is that for the XML file or for the main.as file, if for main.as file, where do I need to insert.............aaarrrgghhhhh!!!!

  13. #13
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Sorry, my mistake, it is for the main.as file:

    Code:
    private function onMenuItemClick( event:MouseEvent ):void {
    			trace("click");
    			trace(event.currentTarget.urlLink);
                            navigateToURL(new URLRequest(event.currentTarget.urlLink));
    			circleMenu.scrollToItem( event.currentTarget as DisplayObject );
    		}

  14. #14
    Senior Member
    Join Date
    Jul 2009
    Posts
    167
    hi there

    ok i added that but I get an error that says


    1180: Call to a possibly undefined method navigateToURL.

    : (

  15. #15
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    At the top of your class you need to import it's class.

    I believe it's
    import flash.display.Sprite;
    import flash.net.navigateToURL;
    import flash.net.URLRequest;

    That should do it.

  16. #16
    Senior Member
    Join Date
    Jul 2009
    Posts
    167
    ha! ha!

    thats works, so many different things you have to add : 0

    the only thing I have noticed is that it makes the images as the button and not the text 'get url 1' which should be the button. so if you rotate around the menu by clicking the images it goes to the url straight away, doh!

    can we make it so that it is only the text button 'Go to URl ' using the script we have just added...or some of it ;

    in the XML it is

    Code:
     <image name="image 1" path="img/img1.jpg" 
    title="Food Fight" 
    text="Information" 
    button="Go to URL 1" href="http://www.adobe.com" />
    can we use the 'button="Go to URL" rather than the images on the wheel?

    aaargrgghhh!!

  17. #17
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Okay, well first question after examining your source FLA is this. Is there any reason that the go to button is a simplebutton? Does it have to be?

  18. #18
    Senior Member
    Join Date
    Jul 2009
    Posts
    167
    hi there

    well, so long as it does what I am trying to acheive it can be really what ever is easiest and/or works best. again am a newbie so don't really know what might be the best/easiest way......what do you think?

    thanks for your help!!

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