A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: attachMovie and remove all clips functions

  1. #1
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357

    attachMovie and remove all clips functions

    Code:
    attClip = new Array();
    MovieClip.prototype.attMovie = function(clip, newName, attDepth) {
    	attachMovie(clip, newName, attDepth);
    	attClip.push(newName);
    };
    //
    MovieClip.prototype.clrMovie = function() {
    	for (var i = 0; i<=attClip.length; i++) {
    		removeMovieClip(attClip[i]);
    	}
    	attClip = [];
    };
    attMovie
    Usage: attMovie([name,newName,depth]);
    - Attaches movies to the stage

    clrMovie
    Usage: clrMovie();
    - Removes all clips attached on the stage

    RipX

  2. #2
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    Just a quick example of usage:

    Code:
    attClip = new Array();
    MovieClip.prototype.attMovie = function(clip, newName, attDepth) {
    	attachMovie(clip, newName, attDepth);
    	attClip.push(newName);
    };
    //
    MovieClip.prototype.clrMovie = function() {
    	for (var i = 0; i<=attClip.length; i++) {
    		removeMovieClip(attClip[i]);
    	}
    	attClip = [];
    };
    //
    for (var i = 0; i<50; i++) {
    	attMovie("box", "box"+i, d++);
    	_root["box"+i]._x = random(550);
    	_root["box"+i]._y = random(400);
    }
    //
    onEnterFrame = function () {
    	if (Key.isDown(Key.ENTER)) {
    		clrMovie();
    	}
    };
    RipX

  3. #3
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    Would it be better if you passed the array to the prototype as an argument ?
    And perhaps give the mc pointer as a return value ?

    Just some food for thought

    Squize.

  4. #4
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    RipX, i see you always use that type of AS when you post some of your sample codes:
    code:
    MovieClip.prototype.attMovie = function(clip, newName, attDepth) { 
    attachMovie(clip, newName, attDepth);
    attClip.push(newName);
    };



    sorry to be that ignorant, but what's the advantage of this? because i always use *classical* code like:
    code:
    function attMovie (clip, newName, attDepth) { 
    attachMovie(clip, newName, attDepth);
    attClip.push(newName);
    }


  5. #5
    Yes we can tomsamson's Avatar
    Join Date
    Sep 2001
    Location
    Team Titan Secret Lair
    Posts
    4,666
    by using that syntax rip adds the function to all mcs (as he adds it to the prototype of the movieclip class).

    its good to do so if you have a function which you really want to have in all mcs,otherwise (if you maybe just want it in some mcs),you should better create a new class,let that class inherit from movieclip (so it has all methods/properties of the movieclip class),then add the function to the prototype of that class and then register an mc from your library with that class.

    that way all instances of that registered mc will automatically act like movieclip and also have that extra function right from the moment on in which you attach them.


    (the advantage is you don´t waste some bytes in each instance of movieclip on the stage though you only need that function in some movielips and not all on the stage,doesn´t make a big difference,only matters if you have lots of mcs on the satge).

  6. #6
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    ah, i understand now...

    plus i think it's faster that way than having pointers like _parent._parent.function()...?

  7. #7
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    It's quicker, but if you wanna speed up a function call ( Without it being a prototype ) in a mc a couple of levels down just do:
    code:

    onClipEvent(load){
    funcName=_parent._parent.funcName;
    }


    and that will save a little bit.

    Squize.

  8. #8
    Yes we can tomsamson's Avatar
    Join Date
    Sep 2001
    Location
    Team Titan Secret Lair
    Posts
    4,666
    yeah squize,you´re right,but using the prototype method is much faster.
    (you could also use _global.functionname=function()...
    if you want to access a function from everywhere without having to type the path but i think _global just copies the path of a function into each instance of other objects..)

  9. #9
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    Originally posted by Squize funcName=_parent._parent.funcName;
    yes, i constantly write shortcuts like that... i read somewhere it's fatser than having each time a pointer like the one i wrote. OR use a tellTarget when possible...

  10. #10
    Yes we can tomsamson's Avatar
    Join Date
    Sep 2001
    Location
    Team Titan Secret Lair
    Posts
    4,666
    yeah shortcuts are faster than using direct pathing each time you want to call the function
    telltarget was only faster in flash5,its not in mx

  11. #11
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    You sure about that ( tellTarget ) Tom ? I still use it whenever possible and I'm sure it's meant to be quicker.

    marmotte I also use var p=_parent in my (load) events to shorten the reference ( All helps ).

    Sorry bout the hijack Rip ( You may want to think about using typeof in a for-in-loop to clear all the sprites rather than your array.push method )

    Squize.

  12. #12
    Yes we can tomsamson's Avatar
    Join Date
    Sep 2001
    Location
    Team Titan Secret Lair
    Posts
    4,666
    Originally posted by Squize
    You sure about that ( tellTarget ) Tom ? I still use it whenever possible and I'm sure it's meant to be quicker.

    marmotte I also use var p=_parent in my (load) events to shorten the reference ( All helps ).

    Sorry bout the hijack Rip ( You may want to think about using typeof in a for-in-loop to clear all the sprites rather than your array.push method )

    Squize.
    yeah,ilooks like this is totally turning into a neat knowledge thread,i like that
    yeah,typeof check isn´t a bad idea and yep,i´m quite sure that direct dot referincing has been speeded up in flash mx enough so that telltarget isn´t faster anymore

  13. #13
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    hey, with regard to using prototype, this is actually only the second/third time I've used it and dont understand many of the things that are related to 'class built coding' such as the typeof implimentation which Squize is refering too. Maybe you could elaborate and provide some code based on these classes, and perhaps a small overview of the advantages of it.

    I've always had trouble with class coding, so I know its better like this, I just dont fully understand why since the typeof things are not obvious to me

    RipX

  14. #14
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    code:

    cls=function(){
    for (var cnt in _root){
    if(typeof _root[cnt]=="movieclip"){
    _root[cnt].removeMovieClip();
    }
    }
    }


    Written cold, but should work ( If anyone tries it let me know ).

    I've not defined it as a prototype because it's not suitable for a command like this, it'll only need calling at the end of a level, game over etc.

    Prototypes are handy for adding extra functionality to an object, whether it be a movieclip or whatever. Not needed, you can write any game you want without using a single prototype ( If anyone wants I'll post all the ones that I used in the trilogy, I think there are about 4, 1 by token3 and the others by Robert Penner ), but they are just a more structured way of coding.

    My current game is being developed in an OOP manner, just cause I don't really rate myself with Actionscript so I wanna improve. It's the exact opposite of my usual coding style

    Squize.

  15. #15
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    did you guys notice that with the attachMovie() for flash6 they've added another argument whereby you can set the initial properties of the mc ie;
    Code:
    attachMovie("link", "name", depth, { _x: 20, _y: 20, _xscale: 150, _yscale: 100 });
    any property that you can set, you can include in your object
    pretty handy eh!
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  16. #16
    Senior Member mbenney's Avatar
    Join Date
    Mar 2001
    Posts
    2,744
    great idea rip!

    thats very handy blinco, thanks!

  17. #17
    this whole prototype thing is new to me. I used it in a script, but how do I turn it off when i want to use new instances of these MC's without inheriting all the functions?

    Sorry for my ignorance! But I'm getting really frustrated.
    thanks!
    -
    Jake

  18. #18
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    telltarget is still a bit faster than dot-syntax, even in Flash Player 7 (!). At least when I've compared them.

    And the for.. in loop Squize posted is a nice solution. A good coding practice is to only have movie clips in the MC you attach movies in. Then you won't have to have the typeof check. Of course, if you have to attach to the root then you'll probably want to have that check there.

    Of course, the fastest way is to attach all movie clips in a MC container you have created using createEmptyMovieClip. To clear all attached MCs, just remove the container and create it again. Can't be much faster than that.

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