A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: Attach Movieclip and delete it

  1. #1
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404

    Attach Movieclip and delete it

    How can I click anywhere on stage to attach a movieclip to the x and y coords of the mouse, Thanks

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    This should get you started, with a new *.fla, create a moveiclip and give linkage name of newClip, do not put it on the stage.

    and in the same fla use this code
    PHP Code:
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    var 
    addedClip:MovieClip;

    stage.addEventListener(MouseEvent.MOUSE_DOWNaddClip,false,0,true);
    function 
    addClip(e:MouseEvent):void
    {
        
    trace("Clicked");
        
    addedClip = new newClip();
        
    addedClip.stage.mouseX;
        
    addedClip.stage.mouseY;
        
    addChild(addedClip);


  3. #3
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    I think you said something about removing it too, so.

    ideally, you would put the new clips inside another clip on the stage rather than adding them to the stage directly.
    PHP Code:
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    var 
    addedClip:MovieClip;
    var 
    containerClip:MovieClip = new MovieClip();

    stage.addEventListener(MouseEvent.CLICKaddClip,false,0,true);

    addChild(containerClip);

    function 
    addClip(e:MouseEvent):void
    {
        
    trace("Stage clicked");

        
    addedClip = new newClip();
        
    containerClip.addChild(addedClip);
        
    with (addedClip)
        {
            
    stage.mouseX;
            
    stage.mouseY;
            
    addEventListener(MouseEvent.MOUSE_DOWN,removeMe,false,0,true);
        }
    }

    function 
    removeMe(e:MouseEvent):void
    {
        
    trace("Removed me");
        
    e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN,removeMe);
        
    e.currentTarget.parent.removeChild(e.currentTarget);

    Last edited by fruitbeard; 07-11-2014 at 01:18 PM.

  4. #4
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Thanks, Check your paypal can you comment the code though, trying to actually learn as3 since I guess im moving up here and abandoning as2
    Last edited by AS3.0; 07-11-2014 at 01:30 PM.

  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    And if you get truly stuck, here is the fla, CS5

    Thanks for the donation Alloy, I shall spend it wisely.

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    can do later alloy if somebody else doesn't, about to eat now...

  7. #7
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Its cool, ill figure it out

  8. #8
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    How can I change that removeme listener to be a hitest, im trying to get gravity to work on the movieclips so they fall down as you click, and they will hit some other movieclip under em to delete them

  9. #9
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Quickly though Alloy as the girlfriend wants to watch movie together, something like so
    PHP Code:
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    var 
    addedClip:MovieClip;
    var 
    containerClip:MovieClip = new MovieClip();

    stage.addEventListener(MouseEvent.CLICKaddClip,false,0,true);

    addChild(containerClip);

    function 
    addClip(e:MouseEvent):void
    {
        
    trace("Stage clicked - clip added");

        
    addedClip = new newClip();
        
    containerClip.addChild(addedClip);
        
    with (addedClip)
        {
            
    stage.mouseX;
            
    stage.mouseY;
            
    addEventListener(Event.ENTER_FRAME,removeMe,false,0,true);
        }
    }

    function 
    removeMe(e:Event):void
    {
        
    e.currentTarget.+=  5;
        if (
    e.currentTarget.hitTestObject(someOtherMovieClip))
        {
            
    e.currentTarget.removeEventListener(Event.ENTER_FRAME,removeMe);
            
    e.currentTarget.parent.removeChild(e.currentTarget);
            
    trace("Removed me");
        }


  10. #10
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    thx

  11. #11
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    TypeError: Error #1034: Type Coercion failed: cannot convert someOtherMovieClip$ to flash.display.DisplayObject.
    at gravitydrops_fla::MainTimeline/removeMe()

    wont remove

  12. #12
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    The error code is telling you that it is coming from the removeMe() function, obviously it wont remove it.
    you can either look up the error codes and work it out for yourself or attach your *.fla file, even a skeleton version if needed, as it works with my test example.

  13. #13
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Try to fix it, I will fund you some more
    Attached Files Attached Files

  14. #14
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    You need to do either one of two things here.

    Firstly give the bluebox a name and remove the linkage name from the library of the bluebox.

    or

    manually remove the bluebox from the stage and attach it using code usuing the linkage name, same way as addedClip is done.

    I think most people would say try not to put things directly on the stage.

  15. #15
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Thanks, Check your paypal

  16. #16
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Also if you need any art work for your projects I got you, I make really good 8bit

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