[COLOR=Red]SOLVED![/COLOR]
Just wanted to say that as a Flash/AS newbie this website and forum has really been a font of knowledge and thanks to everyone.
Anyway…
I’m VERY new to this whole loading and calling objects in AS3 rather than putting it all together visually on the stage and then coding it.
I have completed a tutorial to make the XML Grid Gallery but I want to change the code so that I can have a close button that closes the large image that pops up rather than clicking directly on the picture to close it.
An example of the gallery I have made is here: http://www.republicofcode.com/tutorials/flash/as3gridgallery/
I introduced a little closebtn_mc to the stage but when publish it I just get an error when the button is clicked
Error message:
TypeError: Error #1034: Type Coercion failed: cannot convert closebtn_mc@21fd79c1 to flash.display.Loader.
at trial2_fla::MainTimeline/removeFull()
this is the code I tried (error section in [COLOR=Red]red[/COLOR]):
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var columns:Number;
var my_x:Number;
var my_y:Number;
var my_thumb_width:Number;
var my_thumb_height:Number;
var my_images:XMLList;
var my_total:Number;
var container_mc:MovieClip;
var full_mc:MovieClip;
var full_caption:TextField = new TextField();
var full_description:TextField = new TextField();
var x_counter:Number = 0;
var y_counter:Number = 0;
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("gallery.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML (e:Event):void{
var myXML:XML = new XML(e.target.data);
columns = myXML.@COLUMNS;
my_x = myXML.@XPOSITION;
my_y = myXML.@YPOSITION;
my_thumb_width = myXML.@WIDTH;
my_thumb_height = myXML.@HEIGHT;
my_images = myXML.IMAGE;
my_total = my_images.length();
createContainer();
callThumbs();
}
function createContainer():void{
container_mc = new MovieClip();
container_mc.x = my_x;
container_mc.y = my_y;
addChild(container_mc);
container_mc.addEventListener(MouseEvent.CLICK, callFull);
container_mc.buttonMode = true;
}
function callThumbs():void{
for (var i:Number = 0; i < my_total; i++){
var thumb_url = my_images*.@THUMB;;
var thumb_loader = new Loader();
thumb_loader.load(new URLRequest(thumb_url));
thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded);
thumb_loader.name = i;
thumb_loader.x = (my_thumb_width)*x_counter;
thumb_loader.y = (my_thumb_height)*y_counter;
if (x_counter+1 < columns){
x_counter++;
} else {
x_counter = 0;
y_counter++;
}
}
}
function thumbLoaded(e:Event):void{
var my_thumb:Loader = Loader(e.target.loader);
container_mc.addChild(my_thumb);
new Tween(my_thumb, "alpha", Strong.easeIn, 0,1,0.5, true);
}
function callFull(e:MouseEvent):void{
var full_loader:Loader = new Loader();
var full_url = my_images[e.target.name].@FULL;
full_loader.load(new URLRequest(full_url));
full_loader.contentLoaderInfo.addEventListener(Event.INIT, fullLoaded);
full_caption.text = my_images[e.target.name].@NAME;
full_description.text = my_images[e.target.name].@DESCRIPTION;
container_mc.removeEventListener(MouseEvent.CLICK, callFull);
container_mc.buttonMode = false;
}
function fullLoaded(e:Event):void{
full_mc = new MovieClip();
full_mc.buttonMode = true;
addChild (full_mc);
var my_loader:Loader = Loader(e.target.loader);
full_mc.addChild(my_loader);
new Tween(my_loader, "alpha", Strong.easeIn, 0,1,0.5, true);
my_loader.x = 0;
my_loader.y = 60;
var myCloseBtn:MovieClip = new closebtn_mc();
addChild(myCloseBtn);
myCloseBtn.x = 660;
myCloseBtn.y = 485;
myCloseBtn.buttonMode = true;
[COLOR=Red]myCloseBtn.addEventListener(MouseEvent.CLICK,removeFull); //here's the code that doesn't work
[/COLOR]
addChild(full_caption);
full_caption.textColor = 0xFFFBF0;
var format:TextFormat = new TextFormat();
format.font = "Arial";
format.bold = true;
format.color = 0xFFFFFF;
format.size = 18;
format.align = "left";
full_caption.x = 620;
full_caption.y = 120;
addChild(full_description);
var format2:TextFormat = new TextFormat();
format2.font = "Verdana";
format2.color = 0xFFFFFF;
format2.size = 14;
format2.align = "left";
full_description.textColor = 0xFFFBF0;
full_description.x = 620;
full_description.y = 140;
full_description.width = 150;
full_description.height = 300;
full_description.border = false;
full_description.multiline = true;
full_description.wordWrap = true;
full_description.embedFonts = false;
}
function removeFull(e:MouseEvent):void{
var my_loader:Loader = Loader (e.currentTarget);
var fade_out:Tween = new Tween(my_loader, "alpha", Strong.easeOut, 1,0,0.5, true);
fade_out.addEventListener(TweenEvent.MOTION_FINISH, tweenFinished);
new Tween(container_mc, "alpha", Strong.easeOut, 0.5,1,0.5, true)
removeChild(full_description);
removeChild(full_caption);
}
function tweenFinished (e:TweenEvent):void{
var my_loader:Loader = Loader (e.target.obj);
my_loader.unload();
full_mc.removeChild(my_loader);
removeChild(full_mc);
full_mc = null;
container_mc.addEventListener(MouseEvent.CLICK, callFull);
container_mc.buttonMode = true;
}
I guess my way of adding the button is a little naive and simple and thats why it doesn’t work in conjunction with the other (more complex) code.
Any ideas anyone? Thanks