Skip to main content
Jens Peermann
Inspiring
July 5, 2010
Question

Use a function to trigger a function?

  • July 5, 2010
  • 1 reply
  • 723 views

Is that possible?

I have an event listener attached to a movie clip. The corresponding function has an instance of the loader class. So I am getting error #1034, coercion failed.

My thought is to have the event listener trigger a neutral function which itself will trigger the function with the loader class instance.

Any thought appreciated.

Thanks.

This topic has been closed for replies.

1 reply

Inspiring
July 5, 2010

Show some code.

Jens Peermann
Inspiring
July 5, 2010

As requested:

stop();

//These variables (containers) hold data extracted from the scroll_xperi.xml by the processXML function.

var columns:Number

var my_x:Number

var my_y:Number

var thumb_width:Number

var thumb_height:Number

var images:XMLList

var total:Number

//This container is a MovieClip and holds the thumbs

var container_mc:MovieClip;

//this container is used to create rows:

var x_counter:Number = 0;

//this container is used to place the rows vertically:

var y_counter:Number = 0;

//create an instance of the URLLoader Class and use the .load method to load the scroll_xperi.xml file.

var scrollLoader:URLLoader = new URLLoader();

scrollLoader.load(new URLRequest("scroll_xperi.xml"));

//Event Listener to trigger processing of scroll.xml file:

scrollLoader.addEventListener(Event.COMPLETE, processXML);

//Process XML function:

function processXML(e:Event):void

{

//Instance of the XML Class to process scroll_xperi.xml:

var myXML:XML = new XML(e.target.data);

//This places the data from scroll_xperi.xml into the variables it belongs.

columns = myXML.@COLUMNS;

my_x = myXML.@XPOSITION;

my_y = myXML.@YPOSITION;

thumb_width = myXML.@WIDTH;

thumb_height = myXML.@HEIGHT;

images = myXML.IMAGE;

total = images.length();

//This triggers the createContainer function:

createContainer();

//This triggers displaying the thumbs:

callThumbs();

}

//Creating a container MovieClip for the thumbs

function createContainer():void

{

container_mc = new MovieClip();

//positioning the MovieClip:

container_mc.x = my_x;

container_mc.y = my_y;

//placing MovieClip on stage;

addChild(container_mc);

//event listener to trigger loading of full images:

container_mc.addEventListener(MouseEvent.MOUSE_UP, callFull);

container_mc.buttonMode = true;

}

//Loading the thumbs:

function callThumbs():void

{

//Loop to create multiple instances of the Loader Class:

for (var i:Number = 0; i < total; i++)

{

  //Retrieve URL of thumb an store it in a temporary variable:

  var thumb_url = images.@THUMB;

  //create temporary instance of Loader Class and load thumb instantly:

  var thumb_loader = new Loader();

  thumb_loader.load(new URLRequest(thumb_url));

  //to trigger display of thumbs after they are fully loaded:

  thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded);

  //to assign the value of a thumb's as its name;

  thumb_loader.name = i;

  /*To place thumbs in a single row, "i" represents the place of an individual thumb

  in a sequence, starting with zero. For multiple rows replace "i" with the variable

  "x_counter"*/

  thumb_loader.x = thumb_width*x_counter;

  //to place the rows vertically:

  thumb_loader.y = thumb_height*y_counter;

 

  //Combining a conditional statement with a loop creates new rows:

  if (x_counter + 1 < columns)

  {

  x_counter++ //loop for columns

  }

  else

  {

  x_counter = 0;

  //loop for rows:

  y_counter++;

  }

}

}

//to display the loaded thumbs:

function thumbLoaded(e:Event):void

{

var thumb:Loader = Loader(e.target.loader);

container_mc.addChild(thumb);

}

//function to load full images:

function callFull(e:MouseEvent):void

{

var full_loader:Loader = new Loader();

var full_url = images[e.target.name].@FULL;

full_loader.load(new URLRequest(full_url));

full_loader.contentLoaderInfo.addEventListener(Event.INIT, fullLoaded);

//ths removes the event listener that triggers the "callFull" function:

container_mc.removeEventListener(MouseEvent.MOUSE_UP, callFull);

}

//placing full images:

function fullLoaded(e:Event):void

{

var image_loader:Loader = Loader(e.target.loader);

addChild(image_loader);

image_loader.x = ((stage.stageWidth - 304) - image_loader.width)/2;

image_loader.y = (stage.stageHeight - image_loader.height)/2;

trace("Shows");

//to remove the full image:

image_loader.addEventListener(MouseEvent.MOUSE_UP, removeFull);

}

btn.addEventListener(MouseEvent.MOUSE_UP, removeFull);

function removeFull(e:MouseEvent):void

{

var image_loader:Loader = Loader (e.currentTarget);

image_loader.unload();

removeChild(image_loader);

trace("Gone");

//This re-installs the eventlistener that triggers the "callFull" function

container_mc.addEventListener(MouseEvent.MOUSE_UP, callFull);

}

____________________________________________

It's a slideshow that is fed by a xml file. The MovieClip (container_mc) holds all the thumbnails. When clicked, the thumbnail calls the full size image via the "callFull" and "fullLoaded" functions.

As the code is right now, the full size image has to be clicked to remove it. I want it to disappear when another thumbnail is clicked. I tried to do that by removing the "callFull" event listener and replacing it with a "removeFull" event listener in the "callFull" function, while doing the opposite in the "removeFull" function.

That's when I get the error message.

Thanks for you help.

Inspiring
July 5, 2010

First, when you post code next time, please do yourself and other people a favor and format (indent) it so it can be read easier. In addition, you can use this forum UI syntax highlight feature that will make code even more readable.

Second, when posting errors it is very helpful to know a code line number - it points right to the core of the issue. Basically full error text is what is needed.

With that said, so far I see that problematic lines are:

function removeFull(e:MouseEvent):void
{
     var image_loader:Loader = Loader(e.currentTarget);
     image_loader.unload();
     removeChild(image_loader);
     trace("Gone");
     //This re-installs the eventlistener that triggers the "callFull" function
     container_mc.addEventListener(MouseEvent.MOUSE_UP, callFull);
}

You cannot cast DisaplyObject to Loader! Loader of a DisplayObject is a property of this object's LoaderInfo.

In addition, you attempt to remove Loader but not the object itself.

So, your function should be something like this (I did not test it - just a concept):

function removeFull(e:MouseEvent):void
{
     var target:DisplayObject = DisplayObject(e.currentTarget);
     var image_loader:Loader = target.loaderInfo.loader;
     image_loader.unload();
     removeChild(target);
     trace("Gone");
     //This re-installs the eventlistener that triggers the "callFull" function
     container_mc.addEventListener(MouseEvent.MOUSE_UP, callFull);
}