A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: How do I center (on stage) a loaded image inside a createEmptyMovieClip ?

  1. #1
    Junior Member
    Join Date
    Jan 2007
    Posts
    11

    How do I center (on stage) a loaded image inside a createEmptyMovieClip ?

    Hello all

    How do I center (on stage) a loaded image inside a createEmptyMovieClip ?

    I've read so many threads...can't seem to make it work.


    here's my script:


    button1.onPress = function(){

    _root.createEmptyMovieClip("imagecontainer", _root.imagecontainer.getNextHighestDepth());
    _root.imagecontainer.loadMovie("image18.jpg");
    }


    ps: I have tried to center everything on stage, even tried to center it by the registration point.

    please help
    Sandra

  2. #2
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    hi sandra,
    the reason you can´t center it with Actionscript right now is because at the moment you try to center it the picture is not yet loaded completely but this is required in order to calculate the offset to center it.

    As you might have noticed loaded images tend to be placed with its orgin in the upper left corner wich is of course not centered just like that. To center it we need to place it first in the center of the Stage Stage.width/2 but since the orgin is in the corner of the image movieClip we need to shift it to the left about 1/2 of the image width imagecontainer._width/2. Put together its like this:
    PHP Code:
    Stage.width/imagecontainer._width/2
    here is a example code with a preloader using the movieClipLoader (the better way to load images or SWF files in Flash Player 7+):
    PHP Code:
    _root.createEmptyMovieClip("imagecontainer"_root.imagecontainer.getNextHighestDepth());
    var 
    mclListener:Object = new Object();
    mclListener.onLoadInit = function(mc:MovieClip) {//is now loaded and initialized in flash,- ready to center
        
    mc._x Stage.width/mc._width/2;
        
    mc._y Stage.height/mc._height/2;
    }
    var 
    image_mcl:MovieClipLoader = new MovieClipLoader();
    image_mcl.addListener(mclListener);
    image_mcl.loadClip("image18.jpg"imagecontainer); 
    onLoadInit is a Event that occours if the image is loaded succesfully and finally has been initialized in flash (wich means that it is ready to be transformed). Since centering the image before it has been even loaded doesn´t make any sense the centering code is placed within that event. Note that within the event I accessed the very same MovieClip through the parameter "mc" wich is the same as _root.imagecontainer within that event.

  3. #3
    Junior Member
    Join Date
    Sep 2008
    Posts
    3

    How to edit for nested mc's and loaded movies?

    deleted
    Last edited by artderailed; 09-07-2008 at 01:23 PM.

  4. #4
    Junior Member
    Join Date
    Jul 2009
    Posts
    2
    thanks renderhjs, that worked great

  5. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Could i incorporate this in the code below? as i am having the same problem


    function initGallery()
    {
    function loadXML(loaded)
    {
    if (loaded)
    {
    xmlNode = this.firstChild;
    total = xmlNode.childNodes.length;
    for (i = 0; i < total; i++)
    {
    _root.small_image[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
    _root.big_image[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
    _root.description[i] = xmlNode.childNodes[i].childNodes[2].firstChild.nodeValue;
    if (i == 0)
    {
    _root.loadGImage(_root.description[i], _root.big_image[i]);
    } // end if
    ++_root.total_images;
    } // end of for
    createSmall();
    _root.downloadButton._visible = true;
    }
    else
    {
    content = "file not loaded!";
    } // end else if
    } // End of the function
    xmlData = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = loadXML;
    if (_root.xml_file == undefined)
    {
    _root.xml_file = "images1.xml";
    } // end if
    xmlData.load(xml_file);
    } // End of the function
    function createSmall()
    {
    _root.smallContainer.createEmptyMovieClip("smallIm ageContainer", 10);
    var _loc4 = 0;
    var _loc3 = 0;
    for (var _loc2 = 0; _loc2 < _root.small_image.length; ++_loc2)
    {
    _root.smallContainer.imageContainer.attachMovie("s mallImage", "smallImage_" + _loc2, 100 + _loc2);
    m = _root.smallContainer.imageContainer["smallImage_" + _loc2];
    m._x = _loc3 * 50;
    m._y = 0;
    m.imageContainer.loadMovie(_root.small_image[_loc2], 100);
    m.iData = Array();
    m.iData.big = _root.big_image[_loc2];
    m.iData.title = _root.description[_loc2];
    ++_loc3;
    } // end of for
    _root.smallImageContainer._x = 5;
    _root.smallImageContainer._y = 0;
    } // End of the function
    function loadGImage(title, bigImgURL)
    {
    _root.bigImage.imageContainer.loadMovie(bigImgURL, 100);
    _root.bigImage.imageContainer._x = 0;
    _root.bigImage.imageContainer._y = 0;
    _root.title.text = title;
    _root.downloadButton.onRelease = function ()
    {
    getURL(bigImgURL, "_blank");
    };
    } // End of the function
    function goFullScreen()
    {
    Stage.displayState = "fullScreen";
    } // End of the function
    function exitFullScreen()
    {
    Stage.displayState = "normal";
    } // End of the function
    function menuHandler(obj, menuObj)
    {
    if (Stage.displayState == "normal")
    {
    menuObj.customItems[0].enabled = true;
    menuObj.customItems[1].enabled = false;
    }
    else
    {
    menuObj.customItems[0].enabled = false;
    menuObj.customItems[1].enabled = true;
    } // end else if
    } // End of the function
    _root.description = new Array();
    _root.small_image = new Array();
    _root.big_image = new Array();
    _root.total_images = 0;
    initGallery();
    var fullscreenCM = new ContextMenu(menuHandler);
    fullscreenCM.hideBuiltInItems();
    var fs = new ContextMenuItem("Go Full Screen", goFullScreen);
    fullscreenCM.customItems.push(fs);
    var xfs = new ContextMenuItem("Exit Full Screen", exitFullScreen);
    fullscreenCM.customItems.push(xfs);
    _root.menu = fullscreenCM;
    downloadButton._visible = false;

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