A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: AS3 frustration

  1. #1
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,930

    AS3 frustration

    Alright, should be easy, maybe I've been looking at it too long. I'm letting the user place a certain number of "flags" on an image (content_mc). I'm creating them with:

    function newFlag(){
    if(flagNum<flagTotal){
    flagNum++; //this keeps track of the number of "flags" that are placed
    var newFlag:flag = new flag();
    contentPH_mc.addChild(newFlag);
    }
    }

    Now, I need to be able to let the user remove a flag by clicking on it, and, when they're done, I need to test each of the flags to see if they're over another clip (target_mc) on the screen, but I'm not sure how to reference the flags...

    Thanks...
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    it sounds like you're making minesweeper. Are you not? Well anyway
    addChild can be difficult to work with. The main problem is that you want to have many interactive flags on the screen at once, that are all defined by a single variable. Also (Very Unfortunately ) you cannot addChild within another movieclip. The most annoying part about it is that somewhere along the lines of your code, you are going to have to reference every child individually if you want each one to be interactive. I took some of my time into coming up with a solution and I have an .fla for you. Creating it, actually helped me out with a similar issue of my own. Although, this should not be your solution, if you create 25 flags with the mouse in my solution and then delete them all and start creating again, you will have problems deleting them the second time. Good Luck.
    Attached Files Attached Files
    Last edited by Deejayo; 12-02-2011 at 04:09 PM.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Deejayo, your reply is very misleading and outright incorrect in places. You absolutely can add a child to any DisplayObjectContainer (all MovieClips are DisplayObjectContainers) even if it's already a child of another. And that has no bearing on the problem at hand.

    I cannot open your fla to see how you solved it, but there is a very simple approach to organizing these instances: use an array.
    Code:
    var flags:Array = [];
    
    function newFlag():void{
      if (flags.length < flagTotal){
        var newFlag:flag = new flag(); //classes should begin with an Uppercase letter. :(
        flags.push(newFlag);
        contentPH_mc.addChild(newFlag);
      }
    }
    Then later you can iterate through all your flags easily:
    Code:
    for (var i:int = 0; i < flags.length; i++){
      var f:flag = flags[i];
      //do something with f
    }
    When removing, you'll want to use the event target or current target property to get a reference to the flag that was clicked on, then splice that flag out of the array:
    Code:
    function removeFlag(f:flag):void{
      var idx:int = flags.indexOf(f);
      if (idx != -1){
        if (f.parent){
          contentPH_mc.removeChild(f);
        }
        flags.splice(idx, 1);
      }
    }

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    I had no idea, well I tried

  5. #5
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,930
    5TonsOfFlax, perfect, once again, you solved it. Works great.

    Deejayo, thanks for trying, I appreciate it.

    Thanks!
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

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