MovieClip disappear after changing width and height in AS3

匆匆过客 提交于 2019-12-01 08:51:46

问题


I am trying to load an image from an url in as3, as follows:

var myImageLoader:Loader = new Loader();
private var mcImage: MovieClip = new MovieClip();
var myImageLocation:URLRequest = new URLRequest("http://example.com/xyz.jpg");
myImageLoader.load(myImageLocation);
mcImage.addChild(myImageLoader);
mcImage.x = 100;
mcImage.y = 100;
//mcImage.width = 50;
//mcImage.height = 50;
addChild(mcImage);

The code above works fine, but since my desire image has a different size comparing to the original image, changing its size is necessary here. So after using the lines, which are commented in the code above, the mcImage disappear.

I tried to use mcImage.scaleX =myImageLoader.width/50 , but since myImageLoader is not loaded at the beginning, we cannot get the width of myImageLoader which is null.


回答1:


It's the often error with setting sizes, you can't set size of empty display object (object that width and height are zero). To set size of display object you need first to draw something on it's graphics (for example 1*1 px rectangle), but you should understand that after it you will just scale your display object relatively to it's original size, for example if you draw 1*1 px rectangle and set with=height=50, scaleX and scaleY for it will be 50, so your loaded image if we say about loader will be giant size :) It's all about sizing in flash.

What about your task: there is one common rule - don't resize loader, resize loaded image. As I said above resizing of loader will only scale your image rather than set it sizes. Add complete handler and resize loader.content as you want.

Example:

public function astest()
{
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplte);
    addChild(loader);

    loader.load(new URLRequest("https://www.google.ru/images/srpr/logo3w.png"));
}

protected function onComplte(event:Event):void
{
    EventDispatcher(event.target).removeEventListener(event.type, arguments.callee);
    var image:DisplayObject = (event.target as LoaderInfo).content;

    image.width = 50;
    image.height = 50;
}



回答2:


You need to get loadComplete event on your loader before playing with your MC

var myImageLoader:Loader = new Loader();
private var mcImage: MovieClip = new MovieClip();
var myImageLocation:URLRequest = new URLRequest("http://example.com/xyz.jpg");

myImageLoader.addEventListener(Event.COMPLETE, completeHandler);
myImageLoader.load(myImageLocation);

function completeHandler(e:Event){
    mcImage.x = 100;
    mcImage.y = 100;
    mcImage.width = 50;
    mcImage.height = 50;
    addChild(mcImage);
    mcImage.addChild(myImageLoader);
}



回答3:


try this:

var myImageLoader:Loader = new Loader();
//hide until loaded
myImageLoader.visible = false;
//listen for load completed
myImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
//...rest of your code (without setting width/height)

Then add this to resize and make visible when loaded:

function completeHandler(event:Event):void
{
    myImageLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
    myImageLoader.content.width = 50;
    myImageLoader.content.height = 50;
    myImageLoader.visible = true;
}


来源:https://stackoverflow.com/questions/14674991/movieclip-disappear-after-changing-width-and-height-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!