A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: calling functions from nested movie clips

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    17

    calling functions from nested movie clips

    Hi!

    I have a function:

    Actionscript Code:
    function changeCursor(Event:MouseEvent):void
    {
     trace("AS3 is imposible to understand");
    }

    And I have a mc on the stage that have a code inside to attach a scroll from the library. This scroll load a mc called "Rotuladores1", and inside this mc I have my button "r1".

    The "r1" button have this code:

    Actionscript Code:
    r1.addEventListener(MouseEvent.CLICK, r1Handler);

    function r1Handler(event:MouseEvent):void{
        Rotuladores1(parent).changeCursor();
        }

    This is the code I've used in the mc to attach the scroll to the stage:

    Actionscript Code:
    stop();
    //scroll
    import fl.controls.ScrollBar;
    import fl.events.ScrollEvent;

    var mc:Rotuladores1 = new Rotuladores1();
    mc.x = 7;
    mc.y = 360;

    var mcMask:MovieMaskMC = new MovieMaskMC();
    mcMask.x = mc.x;
    mcMask.y = mc.y;
    mc.mask = mcMask;

    var sb:ScrollBar = new ScrollBar();
    sb.x = mc.x + mc.width;
    sb.y = mc.y;
    sb.height = 164;
    sb.enabled = true;
    sb.setScrollProperties(mcMask.height, 0, (mc.height-mcMask.height));
    sb.addEventListener(ScrollEvent.SCROLL, scrollMC);

    parent.addChild(mc);
    parent.addChild(mcMask);
    parent.addChild(sb);

    function scrollMC(event:ScrollEvent):void
    {
        mc.y = -event.position + mcMask.y;
    }

    But when I press the button I get this error:

    TypeError: Error #1034: Type Coercion failed: cannot convert Main@31f590e1 to Rotuladores1.
    at Rotuladores1/r1Handler()
    How can I call the function in the main timeline from that button?

    Thanks.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your description is confusing, probably because you are confused.

    There is no loading going on here. You are simply instantiating a new Rotuladores1. Apparently the r1Handler is in Rotuladores1, so the parent will not be a Rotuladores1, it will be whatever you added the Rotuladores1 to. In this case, you added it to the stage. You should not do that. Add things to the root instead of the stage.

    Code:
    //Don't do this.  parent is the stage.
    //parent.addChild(mc);
    //parent.addChild(mcMask);
    //parent.addChild(sb);
    
    //Do this:
    addChild(mc);
    addChild(mcMask);
    addChild(sb);
    So you do not need to cast the parent to Rotuladores1, which doesn't even have a changeCursor function, you need to cast it to Main or MovieClip.

    Code:
    r1.addEventListener(MouseEvent.CLICK, r1Handler);
    
    function r1Handler(event:MouseEvent):void{
        MovieClip(root).changeCursor();
    }
    I used root here because your function is on the main timeline. Parent should also have worked, but only if your Rotuladores1 was directly on the root, which it is after fixing the way you added it.
    Last edited by 5TonsOfFlax; 04-29-2011 at 02:12 PM.

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