PDA

View Full Version : Clickable Custom Class



DeaconDesperado
January 19th, 2009, 05:41 PM
Hey all,

Just started tackling AS3 and I've run into some difficulty with custom classes.

I'm trying to modify a custom class I found on the internet for an "Image Container" to allow for an animation on rollover and a link that loads when clicked, both dynamically loaded from an external XML file.

Attached are the two sets of code, first from the containing movieclip and the second for my custom class.



var bannerLoader:URLLoader = new URLLoader();
var bannerURL:URLRequest = new URLRequest("links.xml");
import image.imageContainer;



bannerLoader.addEventListener(Event.COMPLETE, bannerLoaded);

bannerLoader.load(bannerURL);

var bannersXML:XML = new XML();
bannersXML.ignoreWhitespace = true;


function bannerLoaded(evt:Event):void{
bannersXML = XML(bannerLoader.data);
var banImages:Array = new Array();
var banRollovers:Array = new Array();
var banLinks:Array = new Array();

for (var i=0; i<5;i++){
banImages.push(bannersXML.banner[i].image);
banRollovers.push(bannersXML.banner[i].rollover);
banLinks.push(bannersXML.banner[i].link);
var firstBanner:imageContainer=new imageContainer(145,161,banImages[0],"loader.swf", banLinks[0]);
addChild(firstBanner)
firstBanner.x = .5;
firstBanner.y = 35;
var secondBanner:imageContainer = new imageContainer(145,161,banImages[0],"loader.swf", banLinks[0]);
addChild(secondBanner)
secondBanner.x = 168;
secondBanner.y = 35;
var thirdBanner:imageContainer = new imageContainer(145,161,banImages[0],"loader.swf", banLinks[0]);
addChild(thirdBanner)
thirdBanner.x = 341;
thirdBanner.y = 35;
var fourthBanner:imageContainer = new imageContainer(145,161,banImages[0],"loader.swf", banLinks[0]);
addChild(fourthBanner)
fourthBanner.x = 515;
fourthBanner.y = 35;
var fifthBanner:imageContainer = new imageContainer(145,161,banImages[0],"loader.swf", banLinks[0]);
addChild(fifthBanner)
fifthBanner.x = 685;
fifthBanner.y = 35;
}
firstBanner.addEventListener(MouseEvent.CLICK, getfirst)
function getfirst(evt:Event){
//firstBanner.linkClick(banImages[0]);
trace(banImages[0]);
}
}

And then the class...

package image{

import flash.display.MovieClip;
import flash.events.ProgressEvent;
import flash.events.Event;
import flash.display.Loader;
import flash.net.URLRequest;
import fl.transitions.*;
import fl.transitions.easing.*;
import flash.net.navigateToURL;
import flash.events.MouseEvent;


public class imageContainer extends MovieClip {
public var imageURL:String;
public var urlRequest:URLRequest;
public var imageLoader:Loader;
public var imageWidth:Number;
public var imageHeight:Number;
public var preLoader:Loader;
public var imageLink:URLRequest
var mcImage:MovieClip=new MovieClip();
public function imageContainer(Imagewidth:Number,Imageheight:Numbe r,url:String,path:String, link:String) {

preLoader=new Loader();
preLoader.load(new URLRequest(path));
preLoader.scaleX=2;
preLoader.scaleY=2;
preLoader.x=Imagewidth/2-preLoader.width/2;
preLoader.y=Imageheight/2-preLoader.height/2;
addChild(preLoader);

imageURL=url;
imageWidth=Imagewidth;
imageHeight=Imageheight;
loadThumb(url);
imageLink = new URLRequest(link);


}
public function loadThumb(url:String) {
urlRequest=new URLRequest(url);
imageLoader=new Loader();
imageLoader.load(urlRequest);
imageLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE,funcComplete);

function funcComplete(e:Event) {
removeChild(preLoader);
//trace("Successfully Loaded");

imageLoader.content.width=imageWidth;
imageLoader.content.height=imageHeight;
mcImage.addChild(imageLoader.content);
TransitionManager.start(mcImage,{type:Fade, direction:Transition.IN, duration:3, easing:Regular.easeIn});
addChild(mcImage);

}

}
public function linkClick(evt:Event):void {
navigateToURL(imageLink);
}

}
}

I've been working on the links to no avail... Haven't even attempted the rollover.

I've watched numerous tutorials on the subject, but none can offer definitive answers. It seems the situation is complicated by fact that it's dynamic content.

The farthest I got was pushing the links into an array... this passes successfully to the class, but the it told me that it could not coerce the type:URLRequest upon the loaded data from the XML... and then the weird thing... the data clearly was a URL! Totally stumped.

Perhaps I'm going the roundabout way at the problem?

Thanks!