Submit Your Article Forum Rules

Results 1 to 2 of 2

Thread: as3 "switch" question..

  1. #1
    Senior Member ristenk1's Avatar
    Join Date
    Apr 2009
    Posts
    122

    as3 "switch" question..

    I haven't been on this board in awhile but recently I've been dabbling in actionscript and I'm trying to figure out switch statments.. I'm trying to remove four images from the screen with a mouseclick on a button called red (the four instances as well as the red remove button were all added to the stage with actionscript and I want to remove the four green instances one at a time by clicking the remove button - I'd like to using a switch statment if that is possible)... I'm sure this isn't too hard but if anyone could help me with the solution that'd be super cool


    var red:changeColor = new changeColor();
    addChild(red);
    red.addEventListener(MouseEvent.CLICK, onClick);
    function onClick(event:MouseEvent):void
    {

    /* switch ("green" + i) //my first attempt which obviously didn't work..
    {
    case "green0":
    removeChild(getChildByName("green0"));
    break;
    case "green1":
    removeChild(getChildByName("green1"));
    break;
    case "green2":
    removeChild(getChildByName("green2"));
    break;
    case "green3":
    removeChild(getChildByName("green3"));
    break;
    } */

    for (var i:int = 0; i < 4; ++i)
    {
    switch (i)
    {
    case "green" + 0:

    removeChild(getChildByName("green0"));
    break;

    case "green" + 1:

    removeChild(getChildByName("green1"));
    break;

    }
    }
    }


    var green:SnowBoard;
    var greenX:Number = 100;
    var greenY:Number = 300;
    var greenR:Number = 0;
    var greenA:Number = 1;
    for (var i:Number = 0; i < 4; i++)
    {
    green = new SnowBoard();
    addChild(green);
    green.x = greenX;
    green.y = greenY;
    green.rotation = greenR;
    green.alpha = greenA;
    greenY -= 75;// each time runs the y position is 75 pixels higher then current (last) y position
    greenX += 75;
    greenR -= 25;
    //greenA - .25; not working correctly..
    green.name = "green" + i;
    }


    thanks for any help you can spare!

  2. #2
    Senior Member ristenk1's Avatar
    Join Date
    Apr 2009
    Posts
    122
    hmm, I'm disapointed... I know I can remove all of the instances as follows:
    removeChild(getChildByName("green0"));
    removeChild(getChildByName("green1"));
    removeChild(getChildByName("green2"));
    removeChild(getChildByName("green3"));

    but this removes them all at once when I click the remove button that has the event handler. I want to remove the instances one at a time.... and I'm also curious about using the switch statment because I've never been able to put it into a real life scenario before and I know that's the only way I'm going to be able to wrap my head fully around switches... to finally use one in the code I'm writing....

    I'm sure this is really not complicated... are you sure nobody has any thoughts on this???

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •