mercredi 23 décembre 2015

How can I correctly position a loaded image in AS3

I am having heaps of problems trying to reposition an image which is loaded to a movieclip using Actionscript. here is what I am trying to do:

for (var i:int=0; i<roleHeadData[pNum].length; i++) {
        var gap = (sWidth/(roleHeadData[pNum].length+1))*(i+1)-sWidth/2;
        var roleBtn:roleBtn_mc = new roleBtn_mc();
        roleBtn.name = "role"+roleHeadData[pNum][i].shortName+"_mc";
        roleBtn.x = gap;
        roleBtn.buttonMode = true;
        rolePane.addChild(roleBtn);
        var imgPath = roleHeadData[pNum][i].imgRef;
        var roleImg = loadMovie(roleBtn.roleImg, imgPath);
        rolePane.roleBtn =  roleBtn;
        roleBtn.text_txt.text = roleHeadData[pNum][i].title;
        roleImg.x = -(roleImg.width/2);
        roleFunc(roleBtn,i, isHome);
    }

The images don't move as there is no width. I am assuming it is because it hasn't loaded yet. For that reason I tried loaders to re-position the items. This didn't work either, as by the time the image is loaded, the var used is no longer available, so only the last item gets re-positioned:

for (var i:int=0; i<roleHeadData[pNum].length; i++) {
        var gap = (sWidth/(roleHeadData[pNum].length+1))*(i+1)-sWidth/2;
        var roleBtn:roleBtn_mc = new roleBtn_mc();
        roleBtn.name = "role"+roleHeadData[pNum][i].shortName+"_mc";
        roleBtn.x = gap;
        roleBtn.buttonMode = true;
        rolePane.addChild(roleBtn);
        var imgPath = roleHeadData[pNum][i].imgRef;
        var roleLoader:Loader;
        roleLoader = new Loader();
        roleLoader.load(new URLRequest(imgPath));
        var roleImg = roleBtn.roleImg;
        roleBtn.roleImg.addChild(roleLoader);
        roleLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void{roleLoaded(roleImg)});
        function roleLoaded(mc):void {
            // Load Image
            mc.x = -(mc.width/2);
        }
    }

Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire