Skip to main content
Participating Frequently
May 23, 2013
Question

delete child of movieclip

  • May 23, 2013
  • 1 reply
  • 1158 views

Dear masters,

I need your help, I want to delete a movieclip when I press delete on keyboard. but only delete an active movieclip. I use this code before but didn't work.

import flash.display.MovieClip;

import flash.events.MouseEvent;

import flash.events.Event;

import flash.printing.*;

import fl.transitions.*;

function item_onMouseDown(event:MouseEvent):void {

event.currentTarget.startDrag();

}

function item_onMouseUp(event:MouseEvent):void {

event.currentTarget.stopDrag();

}

process_btn.addEventListener(MouseEvent.CLICK, process);

function process(event:MouseEvent):void

{

   

    panjang = Number(panjang_txt.text);

    lebar = Number(lebar_txt.text);

    var mc3:MovieClip = new MovieClip();

    mc3.buttonMode = true;

    mc3.addEventListener(MouseEvent.MOUSE_DOWN, tool.select);

    var randomColours2:int = Math.floor(Math.random() * 0xFFFFFF);

    mc3.graphics.lineStyle(4, 0x000000, .5);

    mc3.graphics.beginFill(randomColours2, .2);

    mc3.graphics.drawRect(0, 0,(panjang/10)/1.3, lebar/10);

    mc3.graphics.endFill();

    mc3.x = 20;

    mc3.y = 250;

    addChild(mc3);

    mc3.addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);

    mc3.addEventListener(MouseEvent.MOUSE_UP, item_onMouseUp);

   

    //action for delete active movieclip

    mc3.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);

    function onKeyPressed(event:KeyboardEvent):void

    {

           if (event.keyCode==Keyboard.DELETE) {

               removeChild(mc3);

       }

    }

   

}


please help me to solve this problem,

regards,

omhanggar.com

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
May 23, 2013

use:

import flash.display.MovieClip;

import flash.events.MouseEvent;

import flash.events.Event;

import flash.printing.*;

import fl.transitions.*;

var mc3:MovieClip;

function item_onMouseDown(event:MouseEvent):void {

event.currentTarget.startDrag();

}

function item_onMouseUp(event:MouseEvent):void {

event.currentTarget.stopDrag();

}

process_btn.addEventListener(MouseEvent.CLICK, process);

function process(event:MouseEvent):void

{

    if(!mc3){

    panjang = Number(panjang_txt.text);

    lebar = Number(lebar_txt.text);

   mc3 = new MovieClip();

    mc3.buttonMode = true;

    mc3.addEventListener(MouseEvent.MOUSE_DOWN, tool.select);

    var randomColours2:int = Math.floor(Math.random() * 0xFFFFFF);

    mc3.graphics.lineStyle(4, 0x000000, .5);

    mc3.graphics.beginFill(randomColours2, .2);

    mc3.graphics.drawRect(0, 0,(panjang/10)/1.3, lebar/10);

    mc3.graphics.endFill();

    mc3.x = 20;

    mc3.y = 250;

    addChild(mc3);

    mc3.addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);

    mc3.addEventListener(MouseEvent.MOUSE_UP, item_onMouseUp);

    //action for delete active movieclip

    mc3.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);

}

    function onKeyPressed(event:KeyboardEvent):void

    {

           if (event.keyCode==Keyboard.DELETE) {

               removeChild(mc3);

       }

    }


Participating Frequently
May 23, 2013

sorry kglad, it still cannot solve the problem.

after I take the variable mc3 in the outside, the function cannot delete the child of mc3 and cannot draw more than one movieclip like before.

Inspiring
May 23, 2013

Keyboard events shouldn`t be added to MovieClips but to the stage.

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);

you could create a dynamic property for your MovieClip, that stores an activated state

mc3 = new MovieClip();

mc3.active = false;

then setting the property to true, if it is dragged

back to false, when it is dropped and then in the Listener

    function onKeyPressed(event:KeyboardEvent):void

    {          

         for(var i:uint= 0;i<this.numChildren-1;i++){

                 if(this.getChildAt(i) .active){

                     removeChild(getChildAt(i));

                 }

             }

       }

another Problem is that you add 2 conflicting functions

mc3.addEventListener(MouseEvent.MOUSE_DOWN, tool.select);

...

mc3.addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);

Why do you do that and what does tool.select?