A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: Attach/Unload MC

  1. #1
    Member
    Join Date
    Nov 2006
    Posts
    93

    Attach/Unload MC

    If I had an mc which is already on stage, and moves after a certain time, how could I attach it so that I can unload it after it collides with another MC?

    Would I have to delete it off the stage and code the x,y where it should appear? Where would I put the attach code, in a seperate symbol somewhere?

    Basically I'm confused as to how the whole concept works, what code to use etc...

    Any help would be appreciated, thanks.

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    I would first recommend reading Flash Help about attachMovie and removeMovieClip commands. There are explanations with examples.

    The movie clips already on stage are not attachable. You attach movie clips from your library, meaning they do not appear on stage when you design the game, only once you run it. To attach movie clip from library you need to give it linkage name, right click on mc in the library and choose linkage, then check "Export for Actionscript" and write linkage name in the Identifier box.

    Lets suppose you gave mc linkage name "mymc". You can now attach it on the stage with:
    _root.attachMovie("mymc", "newname", 1);
    where mymc is the linkage name you wrote in identifier box, newname is the name of new instance of that mc appearing on stage and 1 is depth where new mc is created.

    Now you will have new movie clip on stage with full name
    _root.newname
    you can move it like you would move any other movie clip.

    To remove this mc you use:
    _root.newname.removeMovieClip();

  3. #3
    Member
    Join Date
    Nov 2006
    Posts
    93
    Ah I see, thanks. I've gotten the MC to load and it starts to move etc as it should, however it no longer collides with anything, hitTest doesnt seem to be working. Do I have to use a different method? Thanks.

  4. #4
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Depends how you are doing the hittest. Attached movie clips can be used to check for collision exactly same way as movie clips you place on stage by hand. You just need to use correct instance names.

  5. #5
    Banned XareoX's Avatar
    Join Date
    Aug 2006
    Posts
    462
    can you attach mcs that are not in the libary like ones you created using api??

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

    Look at the Flash help files, all this is covered there.

    Squize.

  7. #7
    Member
    Join Date
    Nov 2006
    Posts
    93
    Yeah it is, but when it doesnt work I ask why here

    this.removeMovieClip; doesnt seem to work. I have a hitTest, where when the MC hits another MC it gets removed, however it doesnt get removed, it just stops moving and goes stretched for some reason o.o

    I want it to be completely removed from the stage, what do you think is going wrong? =/

  8. #8
    383,890,620 polygons nGFX's Avatar
    Join Date
    Oct 2002
    Location
    Germany / Ruhrgebiet
    Posts
    902
    that *weird*

    as a hint (if it's not a typo) you need to add "()" to the remove command:
    Code:
    this.removeMovieClip();
    which should work then ...

    for the stretching ... hell knows ... a piece of code would help (a stripped down to the barest needs version, please)

    nGFX

  9. #9
    Member
    Join Date
    Nov 2006
    Posts
    93
    Yeah I used removeMovieClip() sorry...it doesnt work, it goes all stretchy, very weird indeed lolol...

    However this.unloadMovie(); does work, perfectly it seems. Is it wise to use unload instead? Does it perhaps stop another instance of that MC being loaded later?

    Also, I've been trying to make it attach a movie clip inside a timer:

    Code:
     
    var startTime = getTimer();
    this.onEnterFrame = function() {
    	if (getTimer()-startTime>50) {
    		attachMovie("downarrows", "downarrows1", 1, {_x:0, _y:275.0});
    	}
    };
    However it doesnt seem to work. I can attach an arrow fine by just putting it in a frame, but if I try to attach more than 1, they dont appear. Only one can be attached at a time, surely thats not right? I tried to use duplicate too but that failed too..
    Last edited by xtrava; 11-20-2006 at 02:01 PM.

  10. #10
    Zombie Coder EvilKris's Avatar
    Join Date
    Jun 2006
    Location
    Fukuoka, Japan.
    Posts
    796
    It's not a nested movieclip is it?
    If so you'll have to use removeMovieClip(_parent);


    Your code doesn't work because you're using the same depth for every arrow.

    attachMovie("downarrows", "downarrows1", 1, {_x:0, _y:275.0});

    try doing something like;


    d=0;

    var startTime = getTimer();
    this.onEnterFrame = function() {
    if (getTimer()-startTime>50) {
    attachMovie("downarrows", "downarrows1",d++ , {_x:0, _y:275.0});
    }
    };

  11. #11
    Member
    Join Date
    Nov 2006
    Posts
    93
    Ah I see thanks. I've managed to get multiple arrow sets now appearing, i.e. downarrows, leftarrows, uparrows by using different depths. However your code creates hundreds of arrows non stop, I know you were just giving me an example...

    I tried a variation of what you were trying to explain using for:

    Code:
    for (d=0; d<50; d++) {
    But that had even stranger results. I'll forget that for now, at worst I'll have to write it manually instead of using some sort of loop, I'll come back to it. For now I need to fix my next problem...

    The arrow movie clips I'm attaching have their own action script, which tells them to move. If I use simply:

    Code:
    attachMovie("downarrows", "downarrows1",1 , {_x:0, _y:275.0});
    The arrow appears and moves, and functions as it should. However I want a new arrow to appear depending on the timer I'm using. The problem is, when I use the attach function inside a timer, the arrow just appears after the set time, but doesnt move. It stays put, which is wrong

    Any ideas? Hopefully soon I'll fix this and I'll leave you guys alone for a bit as I do other parts which I can do myself :P

  12. #12
    Zombie Coder EvilKris's Avatar
    Join Date
    Jun 2006
    Location
    Fukuoka, Japan.
    Posts
    796
    Just post the fla file dude, we'll get it working the way you want.

  13. #13
    Member
    Join Date
    Nov 2006
    Posts
    93
    University would get me prosecuted if I had someone else work on a dissertation project, and they know the sites I use lol

    I managed to get an arrow attaching on a timer, which is just in the frame (tried it on an object too and it does the same thing) but when I try to make two arrows appear at different intervals, only the later one ever appears...two never spawn.

    Code:
     var leftstartTime = getTimer();
    this.onEnterFrame = function() {
    	if (getTimer()-leftstartTime>1800) {
    		trace("1st");
    		attachMovie("leftarrows", "leftarrows1", 100, {_x:0, _y:275});
    		delete this.onEnterFrame;
    	}
    };
    var leftstartTime = getTimer();
    this.onEnterFrame = function() {
    	if (getTimer()-leftstartTime>3000) {
    		trace("2nd");
    		attachMovie("leftarrows", "leftarrows2", 101, {_x:0, _y:275});
    		delete this.onEnterFrame;
    	}
    };
    Thats the code I'm using, at the start the arrow appears after 3 secs and the trace says "2nd". If the 2nd timer isnt there, the first arrow appears. How can I get it so multiple arrows appear at manually set intervals? >_< Thanks for trying to help..

  14. #14
    383,890,620 polygons nGFX's Avatar
    Join Date
    Oct 2002
    Location
    Germany / Ruhrgebiet
    Posts
    902
    oh, that's an easy one ...
    you can have only one onEnterFrame attached to an MC (this in this case)

    so you overwrite the first onEnterFrame with the second ...

    after a minor rewrite ...:
    Code:
    var leftstartTime = getTimer();
    var iCount:Number = 0;
    this.onEnterFrame = function() {
    	if (getTimer()-leftstartTime>1800 && iCount==0) {
    		trace("1st");
                    iCount++;
    		attachMovie("leftarrows", "leftarrows1", 100, {_x:0, _y:275});
    	}
            if (getTimer()-leftstartTime>3000 && iCount==1) {
    		trace("2nd");
    		attachMovie("leftarrows", "leftarrows2", 101, {_x:0, _y:275});
    		delete this.onEnterFrame;
    	}
    };
    not what I would have coded, and far from what I would recomend to do, but it gets the job done ...

    nGFX

  15. #15
    Member
    Join Date
    Nov 2006
    Posts
    93
    Ah I see thanks...That works.

    Yeah I realise its probably a sloppy way to do it, obviously as I'm very new to actionscript I'll be doing things like using too many if statements and not loops or things like that...

    What other method is there to do what I'm doing?

    Thanks for the help

    Also how do I stop those MC's from spawning once the player has died and gone to the Game Over screen? I think I can probably work that out but anyway thanks again.
    Last edited by xtrava; 11-21-2006 at 11:47 AM.

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