PDA

View Full Version : How to change the parameter of a function in a loop



kitsunegari
January 23rd, 2008, 11:22 PM
Hi

my project is to do a loop in order to show movieclips and to apply them a function that will change the size of another movieclip on the click event

The thing is that each button should be able to change the size of the clip with its proper value.

Here is my code


// function to change the size
function adjustSize(eventObject:MouseEvent){
imgDisplay.scaleX += scaleFactor ;
}

// the loop
for (var i = 0; i < 10; i++){
var imgDisplay:MovieClip = new MovieClip();
var scaleFactor = i;
imgDisplay.addEventListener(MouseEvent.CLICK, adjustSize);
addChild(imgDisplay);
} Here is my problem: I don't know how to apply a new parameter to each movieclip so that this parameter will be taken into account by the function
For example, if i=1, scaleX should be equal to 1, if i = 2, scaleX should be equal to 2, etc...

What should I modify in my code?
many thanks for your help

aBnest
January 24th, 2008, 05:50 AM
Hi

my project is to do a loop in order to show movieclips and to apply them a function that will change the size of another movieclip on the click event

The thing is that each button should be able to change the size of the clip with its proper value.

Here is my code


// function to change the size
function adjustSize(eventObject:MouseEvent){
imgDisplay.scaleX += scaleFactor ;
}

// the loop
for (var i = 0; i < 10; i++){
var imgDisplay:MovieClip = new MovieClip();
var scaleFactor = i;
imgDisplay.addEventListener(MouseEvent.CLICK, adjustSize);
addChild(imgDisplay);
} Here is my problem: I don't know how to apply a new parameter to each movieclip so that this parameter will be taken into account by the function
For example, if i=1, scaleX should be equal to 1, if i = 2, scaleX should be equal to 2, etc...

What should I modify in my code?
many thanks for your help

You should extend the MoviClip Class into a new Class that contains a id property.

Something like this:


package{
import flash.display.MovieClip;
public class EnhancedMovieClip extends MovieClip{
private var id:int;
public function EnhancedMovieClip(){

}
public function get id():int{
return this.id;
}
public function set id(id:int):void{
this.id = id;
}
}
}


then your code will look something like this:


// function to change the size
function adjustSize(event:MouseEvent){
// here you can have access to the id property of the eobejct you clicked on
trace(event.currentTarget.id);
}

// the loop
for (var i:int = 0; i < 10; i++){
var imgDisplay: EnhancedMovieClip = new EnhancedMovieClip();
imgDisplay.id = i;
imgDisplay.addEventListener(MouseEvent.CLICK, adjustSize);
addChild(imgDisplay);
}


Didn't have time to test this code, but this would be the idea.
Let me know if it worked out for you.

kitsunegari
January 24th, 2008, 09:20 AM
I came up with the following solution that works

var imgDisplay:MovieClip = new MovieClip();
imgDisplay.scaleFactor = i

// Then in the function

function adjustSize(eventObject:MouseEvent){
eventObject.target.scaleX += eventObject.target.scaleFactor ;
}

thanks for your help