A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Please help with XML to buttons (Code posted)

  1. #1
    Secret Agent
    Join Date
    Sep 2000
    Location
    San Francisco, CA
    Posts
    169

    Please help with XML to buttons (Code posted)

    Hello there,

    I was wondering if someone could help me with the following problem. I'm finally making the transition to AS3, and am not sure if it's my logic or what, but I keep getting a "TypeError: Error #1034: Type Coercion failed: cannot convert "Stills_Work/Alice_Commercial_001.png" to net.saw.loaderForPics."

    Please see my code... To me it sounds like I'm mistyping the image pathname as string, or somehow it's expecting to see a different type there. But I just don't see it. Here's the deal:

    I have a site which loads an external XML file, then builds buttons for a menu based on that. The buttons each have AS on them, to show the particular picture when rolled over.

    Here's an excerpt from the main timeline AS3 in a Layer:
    Code:
    function ParsePics(wInput:XML):void{
    	var s_total:Number = 0;
    	wChildren = wInput.Work.children();
    	wPics = wInput.children();
    	// Count the nodes
    	for each (var wWorks:XML in wPics){
    		// make button for each node
    		var my_name:String = "work" + p_total.toString() + "_button";
    		//trace (my_name);
    		makeButton (wWorks, p_total, my_name);
    		p_total++;
    	}
    }
    
    function makeButton(wSub:XML, p_total:Number, my_name:String):void{
    	// Create button title text
    	var my_title:String = wSub.clientname + " – " + wSub.ptitle;
    	// find name of picture
    	var my_picture:String = wSub.picture;
    	// call the TextButton class
    	var work_btn:TextButton = new TextButton(my_title, my_picture);
    	// placement
    	var my_x = 326;
    	var my_y = 110+(p_total*16);
    	work_btn.x = my_x;
    	work_btn.y = my_y;
    	// put it on the display list
    	content_work.addChild(work_btn);
    }
    
    function loadPic(pNum:Number):void {
    	// make the path for the picture (String)
    	var picPath:String = "Stills_Work/" + wPics[pNum].picture + ".png";
    	//trace ("pic num " + p_current + " \r & " + picPath);
    	loaderForPics(picPath);
    }
    Where it calls the TextButton class, I have a button in the library, with the following class attached to it:
    Code:
    package net.saw {
    	
    	import flash.display.Sprite;
    	import flash.text.TextField;
    	import flash.events.MouseEvent;
    	import net.saw.loaderForPics;
    	
    	public class TextButton extends Sprite {
    		
    		private var _title:String;
    		private var _picture:String;
    		public var _label:TextField;
    		
    		public function TextButton(my_title:String, my_picture:String) {
    			_title = my_title;
    			trace ("Original Text: " + _label.text);
    			trace ("My Future Label Text: " + _title)
    			_label.text = _title;
    			_label.mouseEnabled = false;
    			buttonMode = true;
    			useHandCursor = true;
    			_picture = my_picture;
    			trace ("My Picture: " + _picture);
    			addEventListener (MouseEvent.MOUSE_OVER, onMouseOver, false, 0, true);
    			addEventListener (MouseEvent.MOUSE_OUT, onMouseOut, false, 0, true);
    		}
    		private function onMouseOver(evt:MouseEvent):void {
    			trace("From click Handler once again: " + _picture);
    			// load the picture for the button
    			var picPath:String = "Stills_Work/" + _picture + ".png";
    			loaderForPics(picPath);
    		}
    		private function onMouseOut(evt:MouseEvent):void {
    			
    		}
    	}
    	
    }
    And finally the LoaderForPics class (which I believe needs to be its own class, since I'm calling from a few different spots):

    Code:
    package net.saw {
    	
    	import flash.events.*
    	import flash.display.*;
    	import flash.net.URLRequest;
    	
    	public class loaderForPics {
    		var imageLoader:Loader;
    
    		public function loaderForPics(picPath:String) {
    			imageLoader = new Loader();
    			imageLoader.load(new URLRequest(picPath));
    			imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageLoading);
    			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
    		}
    		public function imageLoaded(evt:Event):void {
    			trace ("Loading: " + imageLoader);
    			content_work.placeholder.addChild(imageLoader);
    		}
    		public function imageLoading(evt:ProgressEvent):void {
    		}
    
    	}
    }
    Thanks so much in advance for your help.

    Cheers,
    aldorr

  2. #2
    you have loaderForPics as a class, but in your code you are just trying to cast the string as a loaderForPics. i would do one of two things:

    1) take the code from your loaderForPics class and move it to the main class.
    2) modify the class to extend sprite, add it to the stage and instantiate it like var lfp:loaderForPics = new loaderForPics(str);

  3. #3
    Secret Agent
    Join Date
    Sep 2000
    Location
    San Francisco, CA
    Posts
    169

    Thanks for the two solutions

    I've tried both ways.
    When loaderForPics extends Sprite and I use var lfp:loaderForPics = new loaderForPics(picPath) then I still get the same error.
    And when I put loaderForPics into the Main timeline, I wasn't sure how to access the function. Maybe I'll do it that way, if you'd give me a tip for the syntax.
    Cheers,
    aldorr

  4. #4
    Secret Agent
    Join Date
    Sep 2000
    Location
    San Francisco, CA
    Posts
    169

    Question

    Hi again cultcreative,

    Many thanks for your reply. I'm still struggling here a little. I wish I could figure it out on my own, but alas, I must post again.

    The reason that I have a separate loaderForPics class, is that I'd like to be able to call it from more than one spot:
    a. when user first clicks on the particular section of the site, it should load the first picture
    b. when user rolls over each button of that section, it should load each related picture

    loaderForPics now looks like this:
    Code:
    package net.saw {
    	
    	import flash.display.Sprite;
    	import flash.display.Loader;
    	import flash.events.Event;
    	import flash.net.URLRequest;
    	import flash.display.Bitmap;
    	import flash.display.BitmapData;
    	import flash.events.ProgressEvent;
    	
    	public class loaderForPics extends Sprite {
    		private var imageLoader:Loader = new Loader();
    
    		public function loaderForPics(picPath:String) {
    			trace ("I wish I could load: " + picPath);
    			/**/
    			imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageLoading);
    			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
    			imageLoader.load(new URLRequest(picPath));
    		}
    		public function imageLoaded(evt:Event):void {
    			trace ("Loading: " + imageLoader.content);
    			var image:Bitmap = Bitmap(imageLoader.content)
    			//var bitmap:BitmapData = image.bitmapData;
    			//Sprite(parent).content_work.placeholder.
    			addChild(image);
    		}
    		public function imageLoading(evt:ProgressEvent):void {
    		}
    
    	}
    
    }
    Now when I roll over each picture I need to be able to add the image to the placeholder, so it always loads in the same place and replaces the current bitmap. The placeholder image is in a MovieClip on the root timeline and inside a "placeholder" MovieClip. But how do I reference this from my class?

    I admit I'm probably still thinking in terms of AS2 here, but I'm slowly figuring it out.

    Thanks again, and hope I can fix this so I can begin to sleep well again.

    Cheers,
    aldorr

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