A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Urgent! understanding addChild and Display Objects

  1. #1

    Exclamation Urgent! understanding addChild and Display Objects

    Hi!

    I've been a flash AS developer for many years. Once AS3 came out I was pretty exited, but I just couldn't handle it at that moment, so I just kept writing in AS2. Didn't have much time to learn how to migrate. But now I'm forced to work with AS3 and AIR and I've run into some problems. I've been looking for answers and I was looking for so long that I've just run out of time and I need your assistance immediately my friends.

    Short project description: 2D tracker application.
    Stage of development: Importing image sequence by drag&drop

    Problem description:

    I have a regular slider component as a timeline. Max value of the slider is being set to the amount of files dropped. Heres the code:

    Code:
    function doDragDrop(e:NativeDragEvent):void{
    	var fileList:Object = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT);
    	objProject.fileList = fileList;
    	movSeekBar.maximum = fileList.length-1;
    }
    Easy so far. Now I add an event listener to the slider to determine which file of the objProject.fileList should be displayed. The event calls renderFrame() function. Heres the code.

    Code:
    function renderFrame(){
    	var currentFrame:Number = new Number(movSeekBar.value);
    	var currentFileName:String = new String(objProject.fileList[currentFrame].url)
    	var currentFile:File = new File(currentFileName);
    	
    	var loadImage:Loader = new Loader();
    	loadImage.load(new URLRequest(currentFileName));
    
    	addChild(loadImage);
    }
    This code works, but not totally the way I'd like it to. When invoking addChild it adds the image file to the "_root". I want it to be put into one of the movieclips that are placed on the stage during design-time. The instance name for that movieclip is movDisplay.

    When I use movDisplay.addChild(loadImage) something happens. The movieclip becomes black, but I don't see the image.

    That's not the only problem. I understand that invoking addChild when I
    scrub through the timeline (slider) it constantly adds new childs.
    That's a huge memory leak. The current frame (displayed image) could
    be replaced with the new one once I scrub.

    Also, I need access to pixel informations of the current rendered frame, so I could perform the 2d point tracking.

    Summary:
    1) how do I load and external image file to a movieclip that is placed on the stage during design-time?
    2) how do I keep replacing the image instead of creating new ones on top of it?
    3) how do I gain easy access to the pixel informations?

    I once did something simmilar (in terms of loading image sequences) in AS2,
    but moving that to AS3 is a bit over my head at this point. AS3 is generally a bit over my head. At least for now. I hope that someone could explain this all to me in simplest ways possible. Ready-to-use code would be much appreciated.

    Thanks in advance
    All the best
    EXP

  2. #2
    Palindrome emordnilaP Diniden's Avatar
    Join Date
    Feb 2008
    Posts
    230
    I'm sure you have already thought and seen/heard this, but I recommend a book. The Asctionscript 3.0 Bible has a very good explanation of Adding children to things...so if you have a barnes and noble nearby you can read up on it there >.>

    Also, just keep in mind 3.0 doesn't really have the _root heirarchy anymore. If you figured out the debugger I recommend placing a break right after (or on) your addChild piece of script and see where and how the child is being added.

  3. #3
    Yes I do realize all this, but like I said... I'm short on time, and I don't have access to any book (or at least any good one) on AS3. I'm in Poland and it's hard to find a good book on that subject, so what I'm really hoping for is just a ready-do-use code that I can just paste into my renderFrame function.

    I wrote that long introduction to show you that I'm not a noob looking for an answer to "what does this button do" question. I'm pretty smart and I'm sure I can figure out how the code works once I see in, but thanks for your suggestions and time anyway.

    So.... anyone can help me on this? Please?
    EXP

  4. #4
    Palindrome emordnilaP Diniden's Avatar
    Join Date
    Feb 2008
    Posts
    230
    Yeah sorry...I could help you in 2.0 and lower XP but you're probably plenty knowledgeable on it...I'm working on getting used to 3.0 here myself by reading the board and trying to answer questions....


    I'll see if I can find you something...

  5. #5
    Senior Member
    Join Date
    Jan 2001
    Posts
    567
    Add your images to a container movie clip. If you're only showing them one at a time you could just check to see if the container contains a child and remove it if it does when you scrub. I'm not sure how it'll work if your images ain't cached. Sorry, this is a bit clunky, I don't have flash to test:

    //-------------- Load Picture ------------------------//
    if (container.numChildren != 0) {
    container.removeChild(); //test if container holds a child, if it does remove child
    }

    var img1ToLoad:String = loadImage; //Edit: 'loadImage' is the path to your image file, right?
    var img1Loader:Loader = new Loader();
    var img1URL:URLRequest = new URLRequest(img1ToLoad);
    img1Loader.load(img1URL);
    var img1:Sprite = new Sprite();
    img1.addChild(img1Loader);
    container.addChild(img1);
    //-------------- Load Picture ------------------------//
    Last edited by eggler; 02-12-2008 at 09:06 PM.

  6. #6
    Senior Member
    Join Date
    Jan 2008
    Posts
    150
    I think the problem is that you're trying to add the image before it's loaded all the way.
    You can use a listener to fire off some little function that will do the addChild() part once it's finished.

  7. #7
    I don't think so, because when I use addChild instead of movDisplay.movChild it works fine, just shows up not where I want it. Still I can't make it work.
    EXP

  8. #8
    Anyone? Please?
    EXP

  9. #9
    Senior Member
    Join Date
    Jan 2008
    Posts
    150
    I think I was reading a thread where someone had the same problem. I thought the solution was adding it after it loaded but if not I'm still pretty sure it wasn't anything complicated.

  10. #10
    Still... that doesn't help a lot.
    EXP

  11. #11
    Senior Member
    Join Date
    Jan 2008
    Posts
    150
    You could look for other threads about image loading problems.

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