A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: How to run some actionscript inside a dynamically created movie clip?

  1. #1
    Member
    Join Date
    Apr 2003
    Location
    detroit, mi usa
    Posts
    53

    How to run some actionscript inside a dynamically created movie clip?

    Hey Flashkit,

    First off forgive me if someone has posted a message on this subject already because I haven't found it, yet.

    I would like to create some dynamically created movie clips in a main movie that absorb, receive, or have embedded in them (I do not know the word to use) some actionscript that can be called over and over while the newly created movie clips play through their own timelines.

    Is this possible? How can I put some actionscript inside a dynamically created movie clip? I’m interested in using the code on the first frame, perhaps on enter frame or actually anywhere in the move clip.

    Thanks,
    - Jimmy

  2. #2
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    Here's a quick example of how to dynamically add code to your clips...

    code:

    function createClips(){
    for(i=1;i<=10;i++){
    mc = _root.createEmptyMovieClip("mc"+i,i);
    mc.lineStyle(1,0x000000,100);
    mc.beginFill(0x666666,100);
    mc.lineTo(0,20);
    mc.lineTo(20,20);
    mc.lineTo(20,0);
    mc.lineTo(0,0);
    mc.endFill();
    mc._x = 20*i;
    mc._y = 50;
    mc.counter = i*5;
    mc.onEnterFrame = sineMe;
    mc.onRollOver = showMe;
    }
    }
    function sineMe(){
    this._y = 50 + Math.sin(this.counter++/5)*40;
    }
    function showMe(){
    trace(this._name);
    }
    createClips();

    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  3. #3
    Member
    Join Date
    Apr 2003
    Location
    detroit, mi usa
    Posts
    53

    inside a dynamically created movie clip!

    Hello Lexicon,

    Thanks for the quick reply. Ah I see clearly I can use an instance name and any of these on event handlers to call the code inside a movie clip.

    mc.onData
    mc.onDragOut
    mc.onDragOver
    mc.onEnterFrame
    mc.onKeyDown
    mc.onKeyUp
    mc.onKillFocus
    mc.onLoad
    mc.onMouseDown
    mc.onMouseMove
    mc.onMouseUp
    mc.onPress
    mc.onRelease
    mc.onReleaseOutside
    mc.onRollOut
    mc.onRollOver
    mc.onSetFocus
    mc.onUnload

    What I would like is for the code to be repeatedly called. At first I thought the timeline of the new movie clip would be best to continually call the code. Do you know if there is a way to make the code persistent because these on event handlers seem to be just a one shot event. It is not completely necessary that the timeline calls the code or rather shall I use a setInterval type here?

    Thanks,
    - Jimmy

  4. #4
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    onEnterFrame repeats the code repeatedly. Thats how in my example all the boxes keep moving. And the roll over events are persistent for each movieClip. I'm slightly confused as to what you are asking
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  5. #5
    Member
    Join Date
    Apr 2003
    Location
    detroit, mi usa
    Posts
    53

    persistent inside a dynamically created movie clip!

    Hello Lexicon,

    I apologize for the confusion, you are completely correct mc.onEnterFrame does it. I posted the earlier message before I actually tried adapting your example to the scripts I have been writing. It works great!

    Thanks again,
    - Jimmy

  6. #6
    Member
    Join Date
    Apr 2003
    Location
    detroit, mi usa
    Posts
    53

    inside a dynamically created movie clip!

    Hello Lexicon,

    I have been trying to make a dynamically created movieclip dragable. Do you know if the following added to your code above should work?

    mc.onMouseDown = dragMe;
    mc.onMouseUp = stopMe;

    function dragMe() {
    startDrag(this);
    }
    function StopMe() {
    stopDrag();
    }

    Thanks,
    - Jimmy

  7. #7
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    Using onMouseDown and onMouseUp will not give you the desired result. You will find that the actions act on all the movieClips at once, no matter where you click the mouse and as you can only drag 1 mc at a time only the first mc will be dragged.

    You need to use onPress and onRelease so the action acts on each movieClip seperatly....

    code:

    function createClips(){
    for(i=1;i<=10;i++){
    mc = _root.createEmptyMovieClip("mc"+i,i);
    mc.lineStyle(1,0x000000,100);
    mc.beginFill(0x666666,100);
    mc.lineTo(0,20);
    mc.lineTo(20,20);
    mc.lineTo(20,0);
    mc.lineTo(0,0);
    mc.endFill();
    mc._x = 20*i;
    mc._y = 50;
    mc.counter = i*5;
    mc.onPress = dragMe;
    mc.onRelease = mc.onReleaseOutside = stopMe;
    }
    }
    function dragMe() {
    startDrag(this);
    }
    function stopMe() {
    stopDrag();
    }

    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  8. #8
    Member
    Join Date
    Apr 2003
    Location
    detroit, mi usa
    Posts
    53

    inside a dynamically created movie clip!

    Hey Lexicon,

    This tip helps me very much. Thanks.

    - Jimmy

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