If i make a whole bunch of movieclips using a for loop how would i beable to identify them if an event listener was added to them and i wanted each movieclip created to do something different?
Printable View
If i make a whole bunch of movieclips using a for loop how would i beable to identify them if an event listener was added to them and i wanted each movieclip created to do something different?
Well, yeah...I figured as much. Imean, ...sigh yeah that will work i gues
Sorry, I wasn't very helpful.
Now your question isn't very clear regarding event listeners, but the point is that now you can access all of the created movieclips by looping through the array (or accessing a specific movieclip directly if you know what index number is is at).PHP Code:myArray:Array = [];
for (var i:int = 0; i < movieClipNum; i++)
{
//the push command adds an object to an array
myArray.push(new MovieClip());
}
Are you unhappy about using arrays? If you give some more detail maybe we can give you a more satisfactory answer!
Give each MovieClip a name:
for(var i:int=0; i<numClips;i++)
{
var myClip:MovieClip = new MovieClip();
myClip.name = "myClip"+i;
myClip.addEventListener(MouseEvent.CLICK, cHandler);
}
function cHandler(event:MouseEvent):void
{
trace(event.currentTarget.name);
}
Basically im creating many movieclips on stage. All of them will esentially load the same thing (which is load the a bigger movieclip on stage to load a picture). The problem is which picture to load. I was thinking of something like this...
PHP Code:var i:int=0;
for(i; i<numClips;i++)
{
var myClip:MovieClip = new MovieClip();
myClip.addEventListener(MouseEvent.CLICK, cHandler);
}
function cHandler(event:MouseEvent):void
{
//Determine wich movieclip is clicked
//Load image number based on movieclip number
}
Ok so I have generated this. Now I am stuck. The code is heavily commented. It goes through all the trace statements, but I see nothing on stage, and if I click everywhere on the stage, nothing is traced...
PHP Code:private function createProjects():void
{
for (var i:int = 0; i < numberOfProjects; i++)
{
var movieclip:MovieClip = new MovieClip(); //Creates the movieclip
movieclip.name = "card" + projectNumber; //Gives a name card + the current project number
movieclip.addEventListener(MouseEvent.CLICK, displayDataF); //Adds an event listener
movieclip.x = randomF(0, 1200);
movieclip.y = randomF(0, 800);
var l:Loader = new Loader(); //Creates loader
var urlreq:URLRequest = new URLRequest("beach.jpg"); //Uses defult image for now
l.load(urlreq); //Loads
l.contentLoaderInfo.addEventListener(Event.COMPLETE, placeLoadedImage); //Adds an event listener for when the image is finished loading
function placeLoadedImage(e:Event):void
{
movieclip.addChild(l); //Adds the child
movieclip.width = 150;
movieclip.height = 100;
trace("image loaded and placed");
}
container.addChild(movieclip); //Adds child to a movieclip already on stage
trace("Working on project " + projectNumber);
projectNumber++; //Before the loop is complete, the number is increased by one so it will work on the next number.
}
}
private function displayDataF(e:MouseEvent):void
{
trace(e.target.name); //If someone clicks the movieclip, it will trace the name. Nothing is traced, and the movielips aren't seen...
}
oh and randomF is a function used to generate a random number.
PHP Code:private function randomF(lowVal:int, highVal:int):int //A function used to generate a random number
{
if (lowVal <= highVal)
{
return(lowVal + Math.floor(Math.random() * (highVal - lowVal + 1 )));
} else {
throw(new Error("Fail. Wrong values passed to randomF"));
}
}
ok...if you alter the for statement like this, you see a movieclip but only one movieclip...
and i click on it...its card60....i guess its somehow looping through it 3 times...and only creating one....help?
PHP Code:private function createProjects():void
{
for (var i:int = 0; i < numberOfProjects; i++)
{
var movieclip:MovieClip = new MovieClip(); //Creates the movieclip
movieclip.name = "card" + projectNumber; //Gives a name card + the current project number
movieclip.addEventListener(MouseEvent.CLICK, displayDataF); //Adds an event listener
var l:Loader = new Loader(); //Creates loader
var urlreq:URLRequest = new URLRequest("http://www.mountainguidesinternational.com/images_1/world.gif"); //Uses defult image for now
l.load(urlreq); //Loads
l.contentLoaderInfo.addEventListener(Event.COMPLETE, placeLoadedImage); //Adds an event listener for when the image is finished loading
function placeLoadedImage(e:Event):void
{
movieclip.addChild(l); //Adds the child
movieclip.width = 150;
movieclip.height = 100;
movieclip.x = randomF(0, 1200);
movieclip.y = randomF(0, 800);
addChild(movieclip); //Adds child to a movieclip already on stage
trace("image loaded and placed");
}
trace("Working on project " + projectNumber);
projectNumber++; //Before the loop is complete, the number is increased by one so it will work on the next number.
}
}
ok i adjusted the code so that I could identify each movieclips number. Every time the movieclip is added to stage it is triggered by an event listener... how would I call it? using an Event.ADDED_TO_STAGE, how would i call it? i try e.target but it doesnt work. So when I try e.target.x, i cant change it....
private function addedToStageF(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, addedToStageF);
trace(e.target.name);
e.target.width = 12211;
trace(e.target.width);//I get 0.....as for all of them...
{
oh..e.target...ok..resolved...