To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Actionscript 3.0

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 10-31-2009, 09:54 PM   #1
shabasky
Senior Member
 
Join Date: May 2009
Posts: 205
resolved [RESOLVED] movieclip help?

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?
shabasky is offline   Reply With Quote
Old 10-31-2009, 11:40 PM   #2
Awoogamuffin
Senior Member
 
Awoogamuffin's Avatar
 
Join Date: Nov 2008
Posts: 168
Arrays!

http://livedocs.adobe.com/flash/9.0/...fV3/Array.html
__________________
Check out my blog showing the development of my flash game, the Dregs of War
Awoogamuffin is offline   Reply With Quote
Old 11-01-2009, 02:53 AM   #3
shabasky
Senior Member
 
Join Date: May 2009
Posts: 205
Well, yeah...I figured as much. Imean, ...sigh yeah that will work i gues
shabasky is offline   Reply With Quote
Old 11-01-2009, 10:02 AM   #4
Awoogamuffin
Senior Member
 
Awoogamuffin's Avatar
 
Join Date: Nov 2008
Posts: 168
Sorry, I wasn't very helpful.

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());
}
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).

Are you unhappy about using arrays? If you give some more detail maybe we can give you a more satisfactory answer!
__________________
Check out my blog showing the development of my flash game, the Dregs of War
Awoogamuffin is offline   Reply With Quote
Old 11-01-2009, 02:08 PM   #5
cancerinform
Mod
 
cancerinform's Avatar
 
Join Date: Mar 2002
Location: press the picture...
Posts: 12,127
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);
}
__________________

- The right of the People to create Flash movies shall not be infringed. -
| www.Flashscript.biz | Help a little girl, Ana, who has cancer. | Flashscript Biz Classes/Components | Flash-Model-View-Controller-Modul |
cancerinform is offline   Reply With Quote
Old 11-03-2009, 01:01 PM   #6
shabasky
Senior Member
 
Join Date: May 2009
Posts: 205
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
}
shabasky is offline   Reply With Quote
Old 11-03-2009, 02:44 PM   #7
shabasky
Senior Member
 
Join Date: May 2009
Posts: 205
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...
        
}
shabasky is offline   Reply With Quote
Old 11-03-2009, 02:45 PM   #8
shabasky
Senior Member
 
Join Date: May 2009
Posts: 205
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"));
            }
        }
shabasky is offline   Reply With Quote
Old 11-03-2009, 04:23 PM   #9
shabasky
Senior Member
 
Join Date: May 2009
Posts: 205
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.
            
}
            
        }
shabasky is offline   Reply With Quote
Old 11-03-2009, 05:10 PM   #10
shabasky
Senior Member
 
Join Date: May 2009
Posts: 205
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...

{
shabasky is offline   Reply With Quote
Old 11-03-2009, 10:15 PM   #11
shabasky
Senior Member
 
Join Date: May 2009
Posts: 205
oh..e.target...ok..resolved...
shabasky is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Actionscript 3.0

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:30 AM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.