A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: [RESOLVED] Initializing MovieClip Instances

  1. #1
    Born Again Flasher
    Join Date
    Sep 2000
    Location
    Oahu, Hawaii
    Posts
    58

    resolved [RESOLVED] Initializing MovieClip Instances

    Okay, I'm not sure this is my problem, but this is definitely the part I don't understand. How do I initialize an instance of a MovieClip?

    I created a movie clip inside a fla, using the normal "New Symbol" gui commands. Then I added this movie clip to my frame and gave it an instance name in its properties.

    So the movie clip is in my library as: GalleryBack

    On the frame, in properties for this movie clip, it is labeled: backcontrol

    So when I go into the class I have assigned to my fla file, I added this code to have EventListeners on this MC instance:

    Code:
    import flash.utils.*;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.display.Sprite;
    	
    public class hawaii extends MovieClip{		
    	public function hawaii() {
    		setupPicture();
    		setupGalleryBack();
             }
    
            public function setupPicture() {
    	        var picbutton:Sprite = new Sprite();
    	        picbutton.buttonMode = true;
                    trace(pictures);
    	        picbutton.addChild(pictures);
    	        addChild(picbutton);
    
                    pictures.addEventListener(MouseEvent.MOUSE_OVER,doOver);
    	        pictures.addEventListener(MouseEvent.MOUSE_OUT,doOut);
    	        pictures.addEventListener(MouseEvent.CLICK,doClick);
    	
    	        function doOver(evt:MouseEvent) {
    		        // some action.
    	        }
    	        function doOut(evt:MouseEvent) { 
    		        // Some action.
    	        }
    	        function doClick(evt:MouseEvent) {
    	                // Some action.
    	        }
            }
    
            public function setupGalleryBack() {
    	        var backbutton:Sprite = new Sprite();
            	backbutton.buttonMode = true;
    	        trace(pictureFrame.backcontrol);
            	backbutton.addChild(pictureFrame.backcontrol);
    	        pictureFrame.addChild(backbutton);
    
    	        pictureFrame.backcontrol.addEventListener(MouseEvent.MOUSE_OVER,backOver);
                    pictureFrame.backcontrol.addEventListener(MouseEvent.MOUSE_OUT,backOut);
            	pictureFrame.backcontrol.addEventListener(MouseEvent.CLICK,backClick);
    
    	        function backOver(evt:MouseEvent) {
    	               // Some Action.
            	}
            	function backOut(evt:MouseEvent) { 
            		// Some Action.
            	}
            	function backClick(evt:MouseEvent) {
            		// Some Action.
            	}
            }
    }
    The error I get is:
    Code:
    [object PicturesFader_17]
    null
    TypeError: Error #2007: Parameter child must be non-null.
    	at flash.display::DisplayObjectContainer/addChild()
    	at hawaii/setupGalleryBack()
    	at hawaii()
    The object PicturesFader_17 is the trace for "pictures", indicating it has been initalized with an object PicturesFader. Perfect.
    The top "null" being the trace for "pictureFrame.backcontrol"

    So my main question is this: What actually initializes my MovieClip instance? How did "pictures" get initialzed but "pictureFrame.backcontrol" did not? Is there a command I can use to force initialization? Is there something I'm suppose to do in the Flash GUI to make sure this happens?

    I have a lot of other Sprite buttons to make just like this. They all give me the same problem though. This error also breaks my whole presentation, so I can't even skip it without removing all the code.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Are you sure that backcontrol is a child of pictureFrame? Is it on the frame in which this code runs?

  3. #3
    Born Again Flasher
    Join Date
    Sep 2000
    Location
    Oahu, Hawaii
    Posts
    58
    Aha. It seems I made a bad troubleshooting assumption. It was giving me "undefined" as a trace if I pointed in the wrong direction. I thought once I got a "null" trace I had found it, but it wasn't initialized.

    My assumptions about the hierarchy of objects in a fla file seem to be wrong. I'm reading more about it right now.

    I have my main timeline with the blank canvas. On this canvas I placed a movie clip called GalleryFrame with the instance name "pictureFrame". I selected this MC and hit edit in place. Once on the GalleryFrame's timeline, I added another MC called GalleryBack with the instance name "backcontrol".

    Main Timeline > MC: GalleryFrame ("pictureFrame") > MC: GalleryBack ("backcontrol")

    I made the assumption this makes backcontrol a child of pictureFrame. Now that I explained it, I see how this could be erroneous. So I'm looking up more about children.

    For your second question, all of the code in my original post are in a package in an .as file which is imported by my fla file. I have no code on my timeline frames except movie controls like stop() and gotoAndPlay() and a couple method calls from my .as file to change variables.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I typically don't deal with the IDE, so I don't know for sure but I think that that process SHOULD make backcontrol a child of pictureframe. And flash should create the classes behind the scenes with appropriate properties to match.

    My question about the frames was not about where the code is, but rather whether backcontrol is on pictureFrame at the time the code is executed.

  5. #5
    Born Again Flasher
    Join Date
    Sep 2000
    Location
    Oahu, Hawaii
    Posts
    58
    Oh Well the code is executed in my constructor, and after the method that turns my "pictures" MC into a ButtonMode Sprite with EventListeners. This "pictures" MC works great. The "pictures" MC is on the timeline before "backcontrol" and also displays in the project before "backcontrol".

    The backcontrol movie only comes up after the user clicks the before-mentioned pictures movie_clip (which has been turned into a Sprite with ButtonMode and EventListeners).

  6. #6
    Born Again Flasher
    Join Date
    Sep 2000
    Location
    Oahu, Hawaii
    Posts
    58
    ... ever read your own sentence and realize how silly you are? The code executes before the MC appears on the screen. So therefore it hasn't been initialized at the time the code is run.

    I need to run my setupGalleryBack() method AFTER the backcontrol MC has hit the project, not in the constructor.

    The reason the "pictures" MC works is because its on the main timeline, and therefore happens immediately. I'm guessing before the class is loaded.

    I'm going to go make sure this is true! Thanks 5Tons

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If backcontrol is not yet on the displaylist, then there's a really good chance that's the issue. You might try putting it on the displaylist from the beginning, but positioned off stage, or just invisible.

  8. #8
    Born Again Flasher
    Join Date
    Sep 2000
    Location
    Oahu, Hawaii
    Posts
    58
    Hmm alright... well I ran into complications but your solution of putting the MC on the displaylist in the beginning, yet hidden appeared to work with a little tweaking. I placed backcontrol on the first frame of the entire project so that it can receive code in the contructor, and just made it visible when I wanted it to be. That worked, although its a little messy and won't work all the time.

    The other solution I had was to just call my code when the MC hits the displaylist. I had a problem with this though. It said my function had to be static (since I was calling it from a static class, which I assume is the MC I'm calling from). So I changed my function to static, but then it could no longer find backcontrol (nor pictureFrame).

    I got these errors:
    Code:
    1120: Access of undefined property backcontrol.  backbutton.addChild(backcontrol);
    
    1120: Access of undefined property pictureFrame.  backbutton.addChild(pictureFrame.backcontrol);
    I'm not entirely certain why being static would suddenly make my console unable to see the backbutton.

    For now the first solution works. I'm going to mark this resolved and search for a solution for my static issue in the forums. If I find something, I'll post it here just in case someone else stumbled on this thread with the same problem.

    Thanks again 5Tons

  9. #9
    Born Again Flasher
    Join Date
    Sep 2000
    Location
    Oahu, Hawaii
    Posts
    58
    Solutions to follow-up static problem:

    Here's why I can't see instances if my method is static: Can't call instances from static functions
    Another example of the same problem: class problem

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