PDA

View Full Version : v2 component creation


stretchriri
11-29-2005, 01:21 PM
hi,
I'm trying to do a v2 component with his class. It's a slideshow component which will display picture with automatic border and a legend.
I have some problem with the use of var in a class file, for exemple I have this function that draw the border:

function drawBorders() {
//Creation of a listener to check if the image is completely loaded
//the currentImageClip has no width and height before the image is fully loaded and initialized
var loaderListener:Object = new Object();

//Create the border clip and give __imgWidth and _imgHeight the _width and _height of the current clip
loaderListener.onLoadInit = function(currentImageClip) {
currentImageClip.createEmptyMovieClip("border_mc", clipDepth + 1);
imgWidth = currentImageClip._width;
imgHeight = currentImageClip._height;

//Draw the border of the clip
currentImageClip.border_mc.lineStyle(1,0x000000);//probleme d'init de la bordure et de la couleur
currentImageClip.border_mc.moveTo (0,0);
currentImageClip.border_mc.lineTo (imgWidth,0);
currentImageClip.border_mc.lineTo (imgWidth,imgHeight);
currentImageClip.border_mc.lineTo (0,imgHeight);
currentImageClip.border_mc.lineTo (0,0);

}

//Add the listener to the MovieClipLoader used to display the image
myImageLoader.addListener(loaderListener);
}

Now I want to use the dimension of the image (imgWidth and imgHeight) in other functions of the class, but no way I can't find a solution to call those variables in an other function.
Anybody could help me?

thanks

spon349
11-29-2005, 03:40 PM
Hi stretchriri,

To make imgWidth and imgHeight accessible outside the onLoadInit event handler, you need to assign them to class variables - you have to make sure that onLoadInit has access to the class scope so that it can see these class variables.

The problem is that the scope within an event handler is the scope of the object to which the handler is assigned - in your case, loaderListener. You cannot access the class variables from this scope.

There are several ways to fix this, but here are two. The first way is to create a class function called onLoadInit, and then assign the class scope when using myImageLoader.addListener:
class MyClass {

private var myImageLoader:MovieClipLoader;

// Class variables to hold width and height
private var pImageWidth:Number;
private var pImageHeight:Number;

// This function now has class scope
function onLoadInit(currentImageClip) {
imgWidth = currentImageClip._width;
pImageWidth = imgWidth;

imgHeight = currentImageClip._height;
pImageHeight = imgHeight;

// Code to draw borders
}

function drawBorders() {
// Assign scope of this class (MyClass) - onLoadInit will be called
myImageLoader.addListener(this);
}
}
The second way is better and uses the Delegate class to assign the correct scope to the event handler. You must be using Flash MX 2004 version 7.2, or Flash 8, to be able to use the Delegate class:
// Import the delegate class
import mx.utils.Delegate;

class MyClass {

private var myImageLoader:MovieClipLoader;

// Class variables to hold width and height
private var pImageWidth:Number;
private var pImageHeight:Number;

function drawBorders() {
var loaderListener:Object = new Object();

// Define a function to assign to onLoadInit
var fLoad:Function = function(currentImageClip) {
imgWidth = currentImageClip._width;
pImageWidth = imgWidth;

imgHeight = currentImageClip._height;
pImageHeight = imgHeight;

// Code to draw borders
}

// Use a delegate to assign the correct scope to the fLoad function
loaderListener.onLoadInit = Delegate.create(this, fLoad);

myImageLoader.addListener(loaderListener);
}
}
As I say, there are additional ways of doing this, but they all involve calling onLoadInit within the correct scope.

stretchriri
11-29-2005, 04:22 PM
big thanks, I'll try the delegate way and see what happens....