A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [RESOLVED] Control of dynamic, nested MovieClips

  1. #1
    karldesign
    Guest

    resolved [RESOLVED] Control of dynamic, nested MovieClips

    I have the following AS:

    Code:
    function regionRollOver(evt:MouseEvent):void
    {
    	var mc:String = evt.target.name;
    	mc = mc.replace("target_", "");
    	mc = mc.replace("_mc", "");
    	map_mc.MovieClip("target_" + mc + "_mc").alpha = .5;
    } //regionRollOver()
    The idea is to basically allow a name of the evt to be stripped of "target_" and "_mc" so it can be used multiple times in different MovieClip variations.

    The mc MovieClip is nested in map_mc. I get the following error when trying to run the code:

    Code:
    TypeError: Error #1006: MovieClip is not a function.
    	at private_fla::MainTimeline/regionRollOver()
    Can anybody help me to be able to control nested MovieClips that are referenced on the fly?

    Thanks in advance,

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Maybe, just maybe, the error message might be telling you what is wrong. MovieClip is not a function.

    You want getChildByName.

    Actually, looking at that code, I have no idea why you're messing with the name at all.
    Code:
    function regionRollOver(evt:MouseEvent):void
    {
      MovieClip(evt.target).alpha = .5;
    } //regionRollOver()

  3. #3
    karldesign
    Guest
    I would normally use this approach, but I have two movieclips that I want to control with dynamic reference:

    Code:
    target_variable_mc
    text_variable_mc
    ... where "variable" is the stripped term. Both clips are within map_mc.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Then you want getChildByName.

    Code:
    function regionRollOver(evt:MouseEvent):void
    {
    	var mc:String = evt.target.name;
    	mc = mc.replace("target_", "");
    	mc = mc.replace("_mc", "");
    	map_mc.getChildByName("target_" + mc + "_mc").alpha = .5;
    	map_mc.getChildByName("text_" + mc + "_mc").alpha = .5;
    } //regionRollOver()
    <Insert standard rant about how naming conventions are awful.>

  5. #5
    karldesign
    Guest
    I am also trying to target a MovieClip inside the given clip - using the above code does not work:

    Code:
    map_mc.getChildByName("target_" + mc + "_mc").target_mc.gotoAndPlay("over");
    Is this approach incorrect?

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If it does not work, then it is incorrect. But since you didn't provide any details about HOW it doesn't work, I have to guess.

    I guess that it's complaining that there is no target_mc property on DisplayObject. You need to cast to MovieClip in order to access dynamic properties. Or, you can use getChildByName again if that's more appropriate.

    Code:
    MovieClip(map_mc.getChildByName("target_" + mc + "_mc")).target_mc.gotoAndPlay("over");
    Or
    Code:
    MovieClip(DisplayObjectContainer(map_mc.getChildByName("target_" + mc + "_mc")).getChildByname("target_mc")).gotoAndPlay("over");

  7. #7
    karldesign
    Guest
    Yes, sorry for not providing more details; the above codes produce the following error:

    Code:
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    	at private_fla::MainTimeline/regionRollOver()

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Ah. Well that means you don't actually have a child being returned from the getChildByName statement. So either things are not structured as you think they are, or I misunderstood. Is there a direct child with the name target_variable_mc on map_mc?

  9. #9
    karldesign
    Guest
    The following structure applies:

    ROOT > map_mc > target_variable_mc > target_mc

    What was interesting, is that I targeted the target_variable_mc and made the alpha = .5; which worked!

    But when I try to target the target_mc and play the target_mc frame labeled "over", it does not work...

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Debug time.
    Code:
    var d:DisplayObjectContainer = DisplayObjectContainer(map_mc.getChildByName("target_" + mc + "_mc"));
    trace("target_"+mc+"_mc is:"+d);
    
    var tmc:MovieClip = d.getChildByName("target_mc");
    trace ("target_mc is:"+tmc);
    
    tmc.gotoAndPlay("over");
    I'm guessing you will get "target_mc is: null" and then an error.

  11. #11
    karldesign
    Guest
    Sorry, your initial code was correct, I had incorrectly targeted the MovieClip - thank you very much for your help!

Tags for this Thread

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