A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [RESOLVED] movieclip help?

  1. #1
    Senior Member
    Join Date
    May 2009
    Posts
    280

    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?

  2. #2
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    Check out my blog showing the development of my flash game, the Dregs of War

  3. #3
    Senior Member
    Join Date
    May 2009
    Posts
    280
    Well, yeah...I figured as much. Imean, ...sigh yeah that will work i gues

  4. #4
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    Sorry, I wasn't very helpful.

    PHP Code:
    myArray:Array = [];

    for (var 
    i:int 0movieClipNumi++)
    {
       
    //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

  5. #5
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    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. -

  6. #6
    Senior Member
    Join Date
    May 2009
    Posts
    280
    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(
    ii<numClips;i++)
    {
    var 
    myClip:MovieClip = new MovieClip();
    myClip.addEventListener(MouseEvent.CLICKcHandler);
    }
    function 
    cHandler(event:MouseEvent):void
    {
    //Determine wich movieclip is clicked
    //Load image number based on movieclip number


  7. #7
    Senior Member
    Join Date
    May 2009
    Posts
    280
    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 0numberOfProjectsi++)
                {
                    var 
    movieclip:MovieClip = new MovieClip(); //Creates the movieclip
                    
    movieclip.name "card" projectNumber//Gives a name card + the current project number
                    
    movieclip.addEventListener(MouseEvent.CLICKdisplayDataF); //Adds an event listener
                    
    movieclip.randomF(01200); 
                    
    movieclip.randomF(0800);
                    
                    
                    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.COMPLETEplaceLoadedImage); //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...
            


  8. #8
    Senior Member
    Join Date
    May 2009
    Posts
    280
    oh and randomF is a function used to generate a random number.
    PHP Code:
    private function randomF(lowVal:inthighVal:int):int //A function used to generate a random number
            
    {
                if (
    lowVal <= highVal)
                {
                    return(
    lowVal Math.floor(Math.random() * (highVal lowVal )));
                } else {
                    throw(new 
    Error("Fail. Wrong values passed to randomF"));
                }
            } 

  9. #9
    Senior Member
    Join Date
    May 2009
    Posts
    280
    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 0numberOfProjectsi++)
                {
                    var 
    movieclip:MovieClip = new MovieClip(); //Creates the movieclip
                    
    movieclip.name "card" projectNumber//Gives a name card + the current project number
                    
    movieclip.addEventListener(MouseEvent.CLICKdisplayDataF); //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.COMPLETEplaceLoadedImage); //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.randomF(01200); 
                        
    movieclip.randomF(0800);
                        
    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.
                
    }
                
            } 

  10. #10
    Senior Member
    Join Date
    May 2009
    Posts
    280
    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...

    {

  11. #11
    Senior Member
    Join Date
    May 2009
    Posts
    280
    oh..e.target...ok..resolved...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center